【Redis】Redis 性能测试(redis-benchmark)

Posted by 西维蜀黍 on 2020-05-09, Last Modified on 2021-09-21

redis-benchmark

Redis includes the redis-benchmark utility that simulates running commands done by N clients at the same time sending M total queries (it is similar to the Apache’s ab utility). Below you’ll find the full output of a benchmark executed against a Linux box.

The following options are supported:

Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]

 -h <hostname>      Server hostname (default 127.0.0.1)
 -p <port>          Server port (default 6379)
 -s <socket>        Server socket (overrides host and port)
 -a <password>      Password for Redis Auth
 -c <clients>       Number of parallel connections (default 50)
 -n <requests>      Total number of requests (default 100000)
 -d <size>          Data size of SET/GET value in bytes (default 2)
 --dbnum <db>       SELECT the specified db number (default 0)
 -k <boolean>       1=keep alive 0=reconnect (default 1)
 -r <keyspacelen>   Use random keys for SET/GET/INCR, random values for SADD
  Using this option the benchmark will expand the string __rand_int__
  inside an argument with a 12 digits number in the specified range
  from 0 to keyspacelen-1. The substitution changes every time a command
  is executed. Default tests use this to hit random keys in the
  specified range.
 -P <numreq>        Pipeline <numreq> requests. Default 1 (no pipeline).
 -q                 Quiet. Just show query/sec values
 --csv              Output in CSV format
 -l                 Loop. Run the tests forever
 -t <tests>         Only run the comma separated list of tests. The test
                    names are the same as the ones produced as output.
 -I                 Idle mode. Just open N idle connections and wait.

Parameters

-n - 这次测试包括多少次 requests

$ redis-benchmark -q -n 100000

-q - 只基于每个command显示 performance (query/sec)

$ redis-benchmark -q -n 100000
PING_INLINE: 124378.11 requests per second
PING_BULK: 124533.01 requests per second
SET: 125470.52 requests per second
GET: 126582.27 requests per second
INCR: 127064.80 requests per second
LPUSH: 114155.25 requests per second
RPUSH: 116959.06 requests per second
...

-t - 指定要测试的command set

$ redis-benchmark -t set,lpush -n 100000 -q
SET: 74239.05 requests per second
LPUSH: 79239.30 requests per second

In the above example we asked to just run test the SET and LPUSH commands, in quiet mode (see the -q switch).

It is also possible to specify the command to benchmark directly like in the following example:

$ redis-benchmark -n 100000 -q script load "redis.call('set','foo','bar')"
script load redis.call('set','foo','bar'): 69881.20 requests per second

-r - key 的数量

By default the benchmark runs against a single key.

In Redis the difference between such a synthetic benchmark and a real one is not huge since it is an in-memory system, however it is possible to stress cache misses and in general to simulate a more real-world work load by using a large key space.

理论上说,Redis是一个基于内存的缓存系统,所以一个 GET 操作理论上都是O(1)。而事实上,由于CPU L1、L2、L3的存在,仍然会有 cache miss出现。因此,当key的数量变多时,仍然有可能出现 query QPS降低的情况。

This is obtained by using the -r switch. For instance if I want to run one million SET operations, using a random key for every operation out of 100k possible keys, I’ll use the following command line:

$ redis-benchmark flushall
OK
$ redis-benchmark -t set -r 1 -n 1000000
====== SET ======
  1000000 requests completed in 7.94 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 1 milliseconds
100.00% <= 1 milliseconds
125897.02 requests per second
$ redis-benchmark flushall
OK
$ redis-benchmark -t set -r 10000 -n 1000000
====== SET ======
  1000000 requests completed in 8.01 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 1 milliseconds
100.00% <= 1 milliseconds
124828.37 requests per second
$ redis-benchmark flushall
OK
$ redis-benchmark -t set -r 100000000 -n 1000000
====== SET ======
  1000000 requests completed in 8.06 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

99.92% <= 1 milliseconds
99.93% <= 2 milliseconds
100.00% <= 3 milliseconds
100.00% <= 4 milliseconds
100.00% <= 4 milliseconds
124007.94 requests per second
$ redis-benchmark flushall
OK
$ redis-benchmark -t set -r 100000000000 -n 1000000
====== SET ======
  1000000 requests completed in 8.10 seconds
  50 parallel clients
  3 bytes payload
  keep alive: 1

99.86% <= 1 milliseconds
99.88% <= 2 milliseconds
100.00% <= 4 milliseconds
100.00% <= 5 milliseconds
100.00% <= 5 milliseconds
123456.78 requests per second

-Q - 使用 pipeline

$ redis-benchmark -n 1000000 -t set,get -q
SET: 123854.34 requests per second
GET: 122865.22 requests per second

$ redis-benchmark -n 1000000 -t set,get -P 16 -q
SET: 825082.50 requests per second
GET: 1063829.75 requests per second

Using pipelining results in a significant increase in performance.

Demo

$ # client num = 100, -t set, key number = 1, key len 4000 bytes
redis-benchmark -h 192.168.18.128 -c 100 -r 1 -l -t set -d 4000