Redis 自带了一个叫 redis-benchmark
的工具来模拟 N 个客户端同时发出 M 个请求。 (类似于 Apache ab 程序)。你可以使用 redis-benchmark -h 来查看基准参数。
以下参数被支持:
Usage: redis-benchmark [-h ] [-p ] [-c ] [-n [-k ] -h Server hostname (default 127.0.0.1) -p Server port (default 6379) -s Server socket (overrides host and port) -a Password for Redis Auth -c Number of parallel connections (default 50) -n Total number of requests (default 100000) -d Data size of SET/GET value in bytes (default 2) -dbnum SELECT the specified db number (default 0) -k 1=keep alive 0=reconnect (default 1) -r 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 Pipeline requests. Default 1 (no pipeline). -q Quiet. Just show query/sec values --csv Output in CSV format -l Loop. Run the tests forever -t 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.
你需要在基准测试之前启动一个 Redis 实例。
一般这样启动测试:
redis-benchmark -q -n 100000
这个工具使用起来非常方便,同时你可以使用自己的基准测试工具, 不过开始基准测试时候,我们需要注意一些细节。
只运行一些测试用例的子集
你不必每次都运行 redis-benchmark 默认的所有测试。 使用 -t 参数可以选择你需要运行的测试用例,比如下面的范例:
$ redis-benchmark -t set,lpush -n 100000 -q SET: 74239.05 requests per second LPUSH: 79239.30 requests per second
在上面的测试中,我们只运行了 SET 和 LPUSH 命令, 并且运行在安静模式中(使用 -q 参数)。 也可以直接指定命令来直接运行,比如下面的范例:
$ redis-benchmark -n 100000 -q script load "redis.call('set','foo','bar')" script load redis.call('set','foo','bar'): 69881.20 requests per second
选择测试键的范围大小
默认情况下面,基准测试使用单一的 key。在一个基于内存的数据库里, 单一 key 测试和真实情况下面不会有巨大变化。当然,使用一个大的 key 范围空间, 可以模拟现实情况下面的缓存不命中情况。
这时候我们可以使用 -r 命令。比如,假设我们想设置 10 万随机 key 连续 SET 100 万次,我们可以使用下列的命令:
$ redis-cli flushall
OK $ redis-benchmark -t set -r 100000 -n 1000000
====== SET ======
1000000 requests completed in 13.86 seconds
50 parallel clients
3 bytes payload
keep alive: 1 99.76% `