Redisの性能測定


- ※セキュリティソフトが悪さしてたので再測定しました。

- MacのFlaskからRedisを呼び出してみました。
- redisもflaskもapachebenchもすべて同一マシンで動かしてます。
## 書き込み処理

- 同時100で5000回 … 早いです 11msec

```sh
sh-3.2$ ab -n5000 -c100 -r "http://127.0.0.1/set"
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests


Server Software:        Apache/2.4.25
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /set
Document Length:        201 bytes

Concurrency Level:      100
Time taken for tests:   0.595 seconds
Complete requests:      5000
Failed requests:        0
Non-2xx responses:      5000
Total transferred:      1895000 bytes
HTML transferred:       1005000 bytes
Requests per second:    8403.46 [#/sec] (mean)
Time per request:       11.900 [ms] (mean)
Time per request:       0.119 [ms] (mean, across all concurrent requests)
Transfer rate:          3110.27 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       3
Processing:     1   11   3.4     11      21
Waiting:        1   11   3.4     11      21
Total:          4   12   3.5     12      21

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     13
  75%     14
  80%     15
  90%     16
  95%     17
  98%     19
  99%     20
 100%     21 (longest request)
```

- 同時100で10000回 … 早いです 15msec

```sh
sh-3.2$ ab -n10000 -c100 -r "http://127.0.0.1/set"
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        Apache/2.4.25
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /set
Document Length:        201 bytes

Concurrency Level:      100
Time taken for tests:   1.475 seconds
Complete requests:      10000
Failed requests:        0
Non-2xx responses:      10000
Total transferred:      3790000 bytes
HTML transferred:       2010000 bytes
Requests per second:    6777.55 [#/sec] (mean)
Time per request:       14.755 [ms] (mean)
Time per request:       0.148 [ms] (mean, across all concurrent requests)
Transfer rate:          2508.49 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.6      1      24
Processing:     1   14   5.2     15      47
Waiting:        1   14   5.2     15      47
Total:          4   15   5.4     15      48

Percentage of the requests served within a certain time (ms)
  50%     15
  66%     17
  75%     18
  80%     19
  90%     20
  95%     21
  98%     22
  99%     24
 100%     48 (longest request)
```

## 読み込み処理

- 同時100で5000回 … 早いです！ 11msec

```
sh-3.2$ ab -n5000 -c100 -r "http://127.0.0.1/get"
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests


Server Software:        Apache/2.4.25
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /get
Document Length:        201 bytes

Concurrency Level:      100
Time taken for tests:   0.556 seconds
Complete requests:      5000
Failed requests:        0
Non-2xx responses:      5000
Total transferred:      1895000 bytes
HTML transferred:       1005000 bytes
Requests per second:    9000.20 [#/sec] (mean)
Time per request:       11.111 [ms] (mean)
Time per request:       0.111 [ms] (mean, across all concurrent requests)
Transfer rate:          3331.13 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.3      1       3
Processing:     1   10   3.5     10      19
Waiting:        1   10   3.4     10      19
Total:          3   11   3.7     11      20

Percentage of the requests served within a certain time (ms)
  50%     11
  66%     13
  75%     14
  80%     14
  90%     16
  95%     17
  98%     18
  99%     19
 100%     20 (longest request)
```

- 同時200で5000回 … エラーorz

```sh
sh-3.2$ ab -n5000 -c200 "http://127.0.0.1/get"
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
apr_socket_recv: Connection reset by peer (54)
```

- 同時100で10000回 … 同時100なら動きます。

```sh
sh-3.2$ ab -n10000 -c100 "http://127.0.0.1/get"
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        Apache/2.4.25
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /get
Document Length:        201 bytes

Concurrency Level:      100
Time taken for tests:   1.339 seconds
Complete requests:      10000
Failed requests:        0
Non-2xx responses:      10000
Total transferred:      3790000 bytes
HTML transferred:       2010000 bytes
Requests per second:    7467.42 [#/sec] (mean)
Time per request:       13.392 [ms] (mean)
Time per request:       0.134 [ms] (mean, across all concurrent requests)
Transfer rate:          2763.82 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.6      1      11
Processing:     1   12   4.3     11      22
Waiting:        1   12   4.3     11      22
Total:          3   13   4.8     12      23

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     15
  75%     17
  80%     19
  90%     20
  95%     22
  98%     22
  99%     22
 100%     23 (longest request)
```

## 参考：空処理

- なにもしない場合の速度（Flaskだけの速度）も測っておきます。
- 13msecです。

```sh
sh-3.2$ ab -n10000 -c100 -r "http://127.0.0.1/"
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        Apache/2.4.25
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /
Document Length:        1799 bytes

Concurrency Level:      100
Time taken for tests:   1.354 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      20460000 bytes
HTML transferred:       17990000 bytes
Requests per second:    7387.12 [#/sec] (mean)
Time per request:       13.537 [ms] (mean)
Time per request:       0.135 [ms] (mean, across all concurrent requests)
Transfer rate:          14759.82 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.8      1      14
Processing:     1   12   3.8     12      30
Waiting:        1   12   3.8     12      30
Total:          3   13   4.2     14      32

Percentage of the requests served within a certain time (ms)
  50%     14
  66%     15
  75%     16
  80%     17
  90%     19
  95%     19
  98%     23
  99%     27
 100%     32 (longest request)
```

## Apache Benchでエラーに…

- 性能面はともかく、apachebench自体がエラーになるようです。
- ググると結構でてきますね、なんでだろ？？？

## テストプログラム

- 今回のテストに利用したプログラムです。

```python:app.py
import os
import sys
import redis
import random
from flask import Flask
from datetime import datetime as dt

app = Flask(__name__)

# 接続パス、環境変数にあればそれ優先
REDIS_URL = os.environ.get('REDIS_URL') if os.environ.get(
    'REDIS_URL') != None else 'redis://localhost:6379'
# データベースの指定
DATABASE_INDEX = 1  # 0じゃなくあえて1
# コネクションプールから１つ取得
pool = redis.ConnectionPool.from_url(REDIS_URL, db=DATABASE_INDEX)
# コネクションを利用
r = redis.StrictRedis(connection_pool=pool)

@app.route('/')
def root():
    return '{"status":"OK"}'

@app.route('/get')
def get():
    n = random.randrange(len(r.keys()))
    key = r.keys()[n]
    return str(r.get(key))

@app.route('/set')
def set():
    tdatetime = dt.now()
    key = tdatetime.strftime('%Y-%m-%d %H:%M:%S')
    r.set(key, {'val': key})
    return '{"action":"set"}'

if __name__ == '__main__':
    app.run()
```