42

我想在 python 中创建一个 redis 缓存,作为任何有自尊的科学家,我做了一个基准测试来测试性能。

有趣的是,redis 的表现并不好。要么 Python 正在做一些神奇的事情(存储文件),要么我的 redis 版本非常慢。

我不知道这是否是因为我的代码的结构方式,或者是什么,但我期待 redis 比它做得更好。

为了制作 redis 缓存,我将二进制数据(在本例中为 HTML 页面)设置为从文件名派生的密钥,有效期为 5 分钟。

在所有情况下,文件处理都是使用 f.read() 完成的(这比 f.readlines() 快约 3 倍,我需要二进制 blob)。

我的比较中是否缺少某些东西,或者 Redis 真的无法与磁盘匹配?Python 是否将文件缓存在某处,并且每次都重新访问它?为什么这比访问redis快那么多?

我在 64 位 Ubuntu 系统上使用 redis 2.8、python 2.7 和 redis-py。

我不认为 Python 做了什么特别神奇的事情,因为我创建了一个函数,将文件数据存储在 python 对象中并永远产生它。

我有四个分组的函数调用:

读取文件 X 次

调用一个函数来查看 redis 对象是否仍在内存中、加载它或缓存新文件(单个和多个 redis 实例)。

一个创建生成器的函数,该生成器从 redis 数据库中产生结果(具有单个和多个 redis 实例)。

最后,将文件存储在内存中并永久生成。

import redis
import time

def load_file(fp, fpKey, r, expiry):
    with open(fp, "rb") as f:
        data = f.read()
    p = r.pipeline()
    p.set(fpKey, data)
    p.expire(fpKey, expiry)
    p.execute()
    return data

def cache_or_get_gen(fp, expiry=300, r=redis.Redis(db=5)):
    fpKey = "cached:"+fp

    while True:
        yield load_file(fp, fpKey, r, expiry)
        t = time.time()
        while time.time() - t - expiry < 0:
            yield r.get(fpKey)


def cache_or_get(fp, expiry=300, r=redis.Redis(db=5)):

    fpKey = "cached:"+fp

    if r.exists(fpKey):
        return r.get(fpKey)

    else:
        with open(fp, "rb") as f:
            data = f.read()
        p = r.pipeline()
        p.set(fpKey, data)
        p.expire(fpKey, expiry)
        p.execute()
        return data

def mem_cache(fp):
    with open(fp, "rb") as f:
        data = f.readlines()
    while True:
        yield data

def stressTest(fp, trials = 10000):

    # Read the file x number of times
    a = time.time()
    for x in range(trials):
        with open(fp, "rb") as f:
            data = f.read()
    b = time.time()
    readAvg = trials/(b-a)


    # Generator version

    # Read the file, cache it, read it with a new instance each time
    a = time.time()
    gen = cache_or_get_gen(fp)
    for x in range(trials):
        data = next(gen)
    b = time.time()
    cachedAvgGen = trials/(b-a)

    # Read file, cache it, pass in redis instance each time
    a = time.time()
    r = redis.Redis(db=6)
    gen = cache_or_get_gen(fp, r=r)
    for x in range(trials):
        data = next(gen)
    b = time.time()
    inCachedAvgGen = trials/(b-a)


    # Non generator version    

    # Read the file, cache it, read it with a new instance each time
    a = time.time()
    for x in range(trials):
        data = cache_or_get(fp)
    b = time.time()
    cachedAvg = trials/(b-a)

    # Read file, cache it, pass in redis instance each time
    a = time.time()
    r = redis.Redis(db=6)
    for x in range(trials):
        data = cache_or_get(fp, r=r)
    b = time.time()
    inCachedAvg = trials/(b-a)

    # Read file, cache it in python object
    a = time.time()
    for x in range(trials):
        data = mem_cache(fp)
    b = time.time()
    memCachedAvg = trials/(b-a)


    print "\n%s file reads: %.2f reads/second\n" %(trials, readAvg)
    print "Yielding from generators for data:"
    print "multi redis instance: %.2f reads/second (%.2f percent)" %(cachedAvgGen, (100*(cachedAvgGen-readAvg)/(readAvg)))
    print "single redis instance: %.2f reads/second (%.2f percent)" %(inCachedAvgGen, (100*(inCachedAvgGen-readAvg)/(readAvg)))
    print "Function calls to get data:"
    print "multi redis instance: %.2f reads/second (%.2f percent)" %(cachedAvg, (100*(cachedAvg-readAvg)/(readAvg)))
    print "single redis instance: %.2f reads/second (%.2f percent)" %(inCachedAvg, (100*(inCachedAvg-readAvg)/(readAvg)))
    print "python cached object: %.2f reads/second (%.2f percent)" %(memCachedAvg, (100*(memCachedAvg-readAvg)/(readAvg)))

if __name__ == "__main__":
    fileToRead = "templates/index.html"

    stressTest(fileToRead)

现在结果:

10000 file reads: 30971.94 reads/second

Yielding from generators for data:
multi redis instance: 8489.28 reads/second (-72.59 percent)
single redis instance: 8801.73 reads/second (-71.58 percent)
Function calls to get data:
multi redis instance: 5396.81 reads/second (-82.58 percent)
single redis instance: 5419.19 reads/second (-82.50 percent)
python cached object: 1522765.03 reads/second (4816.60 percent)

结果很有趣,因为 a) 生成器每次都比调用函数快,b) redis 比从磁盘读取要慢,c) 从 python 对象读取速度快得离谱。

为什么从磁盘读取比从 redis 读取内存文件要快得多?

编辑:更多信息和测试。

我将功能替换为

data = r.get(fpKey)
if data:
    return r.get(fpKey)

结果相差不大

if r.exists(fpKey):
    data = r.get(fpKey)


Function calls to get data using r.exists as test
multi redis instance: 5320.51 reads/second (-82.34 percent)
single redis instance: 5308.33 reads/second (-82.38 percent)
python cached object: 1494123.68 reads/second (5348.17 percent)


Function calls to get data using if data as test
multi redis instance: 8540.91 reads/second (-71.25 percent)
single redis instance: 7888.24 reads/second (-73.45 percent)
python cached object: 1520226.17 reads/second (5132.01 percent)

在每个函数调用上创建一个新的 redis 实例实际上对读取速度没有明显影响,从测试到测试的变化大于增益。

Sripathi Krishnan 建议实施随机文件读取。正如我们从这些结果中看到的那样,这就是缓存开始真正发挥作用的地方。

Total number of files: 700

10000 file reads: 274.28 reads/second

Yielding from generators for data:
multi redis instance: 15393.30 reads/second (5512.32 percent)
single redis instance: 13228.62 reads/second (4723.09 percent)
Function calls to get data:
multi redis instance: 11213.54 reads/second (3988.40 percent)
single redis instance: 14420.15 reads/second (5157.52 percent)
python cached object: 607649.98 reads/second (221446.26 percent)

文件读取存在巨大的可变性,因此百分比差异并不是加速的良好指标。

Total number of files: 700

40000 file reads: 1168.23 reads/second

Yielding from generators for data:
multi redis instance: 14900.80 reads/second (1175.50 percent)
single redis instance: 14318.28 reads/second (1125.64 percent)
Function calls to get data:
multi redis instance: 13563.36 reads/second (1061.02 percent)
single redis instance: 13486.05 reads/second (1054.40 percent)
python cached object: 587785.35 reads/second (50214.25 percent)

我使用 random.choice(fileList) 在每次通过函数时随机选择一个新文件。

如果有人想尝试一下,完整的要点就在这里 - https://gist.github.com/3885957

编辑编辑:没有意识到我正在为生成器调用一个文件(尽管函数调用和生成器的性能非常相似)。这也是来自生成器的不同文件的结果。

Total number of files: 700
10000 file reads: 284.48 reads/second

Yielding from generators for data:
single redis instance: 11627.56 reads/second (3987.36 percent)

Function calls to get data:
single redis instance: 14615.83 reads/second (5037.81 percent)

python cached object: 580285.56 reads/second (203884.21 percent)
4

1 回答 1

41

这是一个苹果与橘子的比较。见http://redis.io/topics/benchmarks

Redis 是一种高效的远程数据存储。每次在 Redis 上执行命令时,都会向 Redis 服务器发送一条消息,如果客户端是同步的,则阻塞等待回复。因此,除了命令本身的成本之外,您还将支付网络往返或 IPC 的费用。

在现代硬件上,网络往返或 IPC 与其他操作相比非常昂贵。这是由于几个因素:

  • 媒体的原始延迟(主要用于网络)
  • 操作系统调度程序的延迟(在 Linux/Unix 上不保证)
  • 内存缓存未命中是昂贵的,并且当客户端和服务器进程被调度进/出时,缓存未命中的概率会增加。
  • 在高端盒子上,NUMA 副作用

现在,让我们回顾一下结果。

比较使用生成器的实现和使用函数调用的实现,它们不会生成相同数量的 Redis 往返。使用生成器,您只需:

    while time.time() - t - expiry < 0:
        yield r.get(fpKey)

所以每次迭代1次往返。使用该功能,您可以:

if r.exists(fpKey):
    return r.get(fpKey)

所以每次迭代 2 次往返。难怪发电机更快。

当然,您应该重用相同的 Redis 连接以获得最佳性能。运行系统地连接/断开连接的基准是没有意义的。

最后,关于 Redis 调用和文件读取之间的性能差异,您只是将本地调用与远程调用进行比较。文件读取由 OS 文件系统缓存,因此它们是内核和 Python 之间的快速内存传输操作。这里不涉及磁盘 I/O。使用 Redis,您必须支付往返的费用,因此速度要慢得多。

于 2012-10-13T07:44:39.287 回答