正文
不同网络情况对测试脚本的影响
测试脚本:
原生:
import redis
import timeit
r = redis.Redis(host='10.110.x.x', port=36379, password='xxxx',db=0)
def func() :
for i in range(1000) :
r.set('foo1', 'bar')
r.set('foo2', 'bar')
r.set('foo3', 'bar')
r.set('foo4', 'bar')
r.set('foo5', 'bar')
for i in range(3) :
print(timeit.timeit(stmt=func, number=1))
r.close()
lua:
import redis
import timeit
r = redis.Redis(host='10.110.x.x', port=36379, password='xxxx',db=0)
keys = ['key1', 'key2', 'key3', 'key4', 'key5']
values = ['value1', 'value2', 'value3', 'value4', 'value51241234']
script = """
for i=1,#KEYS do redis.call('SET', KEYS[i], ARGV[i]) end
"""
compiled_script = r.register_script(script)
def func() :
for i in range(1000) :
result = compiled_script(keys=keys, args=values)
for i in range(3) :
print(timeit.timeit(stmt=func, number=1))
print(r.get('key5'))
r.close()
VPN
Redis原生命令:
51.1680905
50.8700407
52.46671039999998
Redis Lua脚本
9.800387899999999
11.110442400000002
10.610073499999999
公司无线
Redis原生命令
32.1694002
30.7110252
35.90106370000001
Redis Lua脚本
4.7636107999999995
4.8524428
4.843981100000001
注意 公司无线网络一开始很不稳定, 第一次测试结果为:
17.1589119
9.218861799999999
4.9797340000000005
公司有线
Redis原生命令
4.9240028
4.730359600000001
4.9162759000000005
Redis Lua脚本
0.9259070999999999
0.8689242000000001
0.9006865
不通网络情况的性能对比