VirtualBox上のCentOS7にRedis4入れてPtyhonから呼んでみたときの性能回り。

VirtualBoxはRAM4GB、CPU:1コア,2.9G。

まず、接続。コネクションプール使うのがあるので比較。

import time
import redis

#コネクションプール使用で1万回接続⇒0.05472540855407715
t1 = time.time()
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password='hogepass')
for i in range(10000):
    r = redis.Redis(connection_pool=pool)
print(time.time() - t1)

#普通に1万回接続⇒0.2304081916809082
t1 = time.time()
for i in range(10000):
    r = redis.Redis(host='127.0.0.1', port=6379, password='hogepass')
print(time.time() - t1)

#4.5倍くらいプールの方が早い?

で、なんでか普通に接続の方を先にやるとプールの方が遅くなる。なんじゃらほい。

キーバリュの更新。ランダム文字の10万文字をバリューにして10000回更新

import time
import redis
import string
import random

def randomCharacter(n):
    c = string.ascii_lowercase + string.ascii_uppercase + string.digits
    return ''.join([random.choice(c) for i in range(n)])

databody = randomCharacter(100000)


t1 = time.time()
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password='hogepass')
r = redis.Redis(connection_pool=pool)
for i in range(10000):
    r.set(i,databody)
print(time.time() - t1)

#1回目:3.6718790531158447
#2回目:3.044043779373169
#3回目:2.0042741298675537
#4回目:2.015092134475708

初回(追加)ベースで考えると、10万文字のデータ更新で1件0.3msってところ。

索引。1万文字のバリューで1万件のデータをを1000回1件索引

t1 = time.time()
for i in range(1000):
    r.get(i)
print(time.time() - t1)

# 0.10848879814147949

1回あたり、0.1msと言った感じ。

おまけ

コネクションプールと普通の接続でmonitorの結果

import redis

pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password='hogepass')
#プール1回目
r = redis.Redis(connection_pool=pool)
r.set('pool1','hoge1')#プールの場合はここでAuthのコマンド送ってる
r.get('pool1')
#プール2回目
r = redis.Redis(connection_pool=pool)
r.set('pool2','hoge2')
r.get('pool2')
#普通の1回目
r = redis.Redis(host='127.0.0.1', port=6379, password='hogepass')
r.set('not pool1','fuga1')
r.get('not pool1')
#普通の2回目
r = redis.Redis(host='127.0.0.1', port=6379, password='hogepass')
r.set('not pool2','fuga2')
r.get('not pool2')

上のmonitor結果

[0 127.0.0.1:58564] "AUTH" "hogepass"
[0 127.0.0.1:58564] "SET" "pool1" "hoge1"
[0 127.0.0.1:58564] "GET" "pool1"
[0 127.0.0.1:58564] "SET" "pool2" "hoge2"
[0 127.0.0.1:58564] "GET" "pool2"
[0 127.0.0.1:58566] "AUTH" "hogepass"
[0 127.0.0.1:58566] "SET" "not pool1" "fuga1"
[0 127.0.0.1:58566] "GET" "not pool1"
[0 127.0.0.1:58568] "AUTH" "hogepass"
[0 127.0.0.1:58568] "SET" "not pool2" "fuga2"
[0 127.0.0.1:58568] "GET" "not pool2"

コネクションプール使ってる方はやっぱ、つなぎ直しはしてませんね。つか繋ぎっぱなしになるんだろか。接続解除的な何かあったかな。あと、プールの場合は最初のSetの時にAuthコマンドはいてるっぽいですね。。。