前言
Redis上线前,需要对Redis单机/Redis哨兵/Redis Cluster进行压力测试,测试结果满意后上线心里更踏实。
1 redis-benchmark
1.1 介绍
redis-benchmark是Redis自带的压力测试工具。
1.2 帮助手册
查看reids-benchmark帮助手册
点击查看代码
[root]# redis-benchmark --help
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 3)
--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).
-e If server replies with errors, show them on stdout.
(no more than 1 error per second is displayed)
-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.
Examples:
Run the benchmark with the default configuration against 127.0.0.1:6379:
$ redis-benchmark
Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:
$ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20
Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:
$ redis-benchmark -t set -n 1000000 -r 100000000
Benchmark 127.0.0.1:6379 for a few commands producing CSV output:
$ redis-benchmark -t ping,set,get -n 100000 --csv
Benchmark a specific command line:
$ redis-benchmark -r 10000 -n 10000 eval 'return redis.call("ping")' 0
Fill a list with 10000 random elements:
$ redis-benchmark -r 10000 -n 10000 lpush mylist __rand_int__
On user specified command lines __rand_int__ is replaced with a random integer
with a range of values selected by the -r option.
常用参数:
参数 | 说明 |
-h | 连接的主机名/IP(默认是127.0.0.1) |
-p | 连接的端口(默认是6379) |
-s | socket连接方式 |
-c | 并发连接数(默认是50) |
-n | 请求总数(默认是100000) |
-d | 以字节单位指定SET/GET值的长度 |
--dbnum | 指定连接库ID(默认是0) |
-k | 是否保留测试键值对,1是保留,0是不保留(默认值是1) |
-r | SET/GET/INCR使用随机key,SADD使用随机值 |
-P | 通过管道符传输请求 |
-e | 如果返回错误,显示在标准输出上 |
-q | 退出Redis,仅仅显示query/sec值 |
--csv | 以CSV格式输出 |
-l | 循环永久执行测试 |
-t | 仅运行以逗号分隔的测试命令列表 |
-I | idle模式,打开N个idle连接并等待 |
1.3 压力测试
使用方法:
redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests>] [-k <boolean>]
20个并发连接,100000次请求
点击查看代码
[root@]# redis-benchmark -h 127.0.0.1 -p 6379 -n 100000 -c 20
====== PING_INLINE ======
100000 requests completed in 0.96 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
104602.52 requests per second
====== PING_BULK ======
100000 requests completed in 0.95 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
105485.23 requests per second
153594:M 26 Nov 2021 23:55:07.901 * 10000 changes in 60 seconds. Saving...
153594:M 26 Nov 2021 23:55:07.901 * Background saving started by pid 2546
2546:C 26 Nov 2021 23:55:07.902 * DB saved on disk
2546:C 26 Nov 2021 23:55:07.902 * RDB: 0 MB of memory used by copy-on-write
153594:M 26 Nov 2021 23:55:08.001 * Background saving terminated with success
====== SET ======
100000 requests completed in 0.96 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
104058.27 requests per second
====== GET ======
100000 requests completed in 0.96 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
104166.67 requests per second
====== INCR ======
100000 requests completed in 0.95 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
105042.02 requests per second
====== LPUSH ======
100000 requests completed in 0.93 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
107411.38 requests per second
====== RPUSH ======
100000 requests completed in 0.96 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
104275.29 requests per second
====== LPOP ======
100000 requests completed in 0.95 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
105263.16 requests per second
====== RPOP ======
100000 requests completed in 0.95 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
105596.62 requests per second
====== SADD ======
100000 requests completed in 0.95 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
105596.62 requests per second
====== HSET ======
100000 requests completed in 0.95 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
105152.48 requests per second
====== SPOP ======
100000 requests completed in 0.98 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
102564.10 requests per second
====== LPUSH (needed to benchmark LRANGE) ======
100000 requests completed in 0.97 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
103199.18 requests per second
====== LRANGE_100 (first 100 elements) ======
100000 requests completed in 1.49 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
67249.50 requests per second
====== LRANGE_300 (first 300 elements) ======
100000 requests completed in 3.16 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
31665.61 requests per second
====== LRANGE_500 (first 450 elements) ======
100000 requests completed in 4.25 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
23507.29 requests per second
====== LRANGE_600 (first 600 elements) ======
100000 requests completed in 5.30 seconds
20 parallel clients
3 bytes payload
keep alive: 1
99.97% <= 1 milliseconds
99.99% <= 2 milliseconds
100.00% <= 2 milliseconds
18850.14 requests per second
====== MSET (10 keys) ======
100000 requests completed in 0.91 seconds
20 parallel clients
3 bytes payload
keep alive: 1
100.00% <= 0 milliseconds
109529.02 requests per second
SET操作,1000000次请求
点击查看代码
[root]# redis-benchmark -t set -n 1000000 -r 100000000
====== SET ======
1000000 requests completed in 9.03 seconds
50 parallel clients
3 bytes payload
keep alive: 1
99.93% <= 1 milliseconds
99.95% <= 2 milliseconds
100.00% <= 2 milliseconds
110791.05 requests per second
2 memtier_benchmark
2.1 介绍
memtier_benchmark是基于Redis Labs推出的多线程压测工具。
2.2 安装
安装memtier_benchmark
[root]# yum install -y autoconf automake make gcc-c++
[root]# yum install -y pcre-devel zlib-devel libmemcached-devel openssl-devel
[root]# git clone https://github.com/RedisLabs/memtier_benchmark.git
[root]# cd memtier_benchmark
[root]# autoreconf -ivf
[root]# ./configure
[root]# make && make install
2.3 帮助手册
查看memtier_benchmark帮助手册
点击查看代码
[root]# memtier_benchmark --help
Usage: memtier_benchmark [options]
A memcache/redis NoSQL traffic generator and performance benchmarking tool.
Connection and General Options:
-s, --server=ADDR Server address (default: localhost)
-p, --port=PORT Server port (default: 6379)
-S, --unix-socket=SOCKET UNIX Domain socket name (default: none)
-P, --protocol=PROTOCOL Protocol to use (default: redis). Other
supported protocols are memcache_text,
memcache_binary.
-a, --authenticate=CREDENTIALS Authenticate using specified credentials.
A simple password is used for memcache_text
and Redis <= 5.x. <USER>:<PASSWORD> can be
specified for memcache_binary or Redis 6.x
or newer with ACL user support.
--tls Enable SSL/TLS transport security
--cert=FILE Use specified client certificate for TLS
--key=FILE Use specified private key for TLS
--cacert=FILE Use specified CA certs bundle for TLS
--tls-skip-verify Skip verification of server certificate
--sni=STRING Add an SNI header
-x, --run-count=NUMBER Number of full-test iterations to perform
-D, --debug Print debug output
--client-stats=FILE Produce per-client stats file
-o, --out-file=FILE Name of output file (default: stdout)
--json-out-file=FILE Name of JSON output file, if not set, will not print to json
--hdr-file-prefix=FILE Prefix of HDR Latency Histogram output files, if not set, will not save latency histogram files
--show-config Print detailed configuration before running
--hide-histogram Don't print detailed latency histogram
--print-percentiles Specify which percentiles info to print on the results table (by default prints percentiles: 50,99,99.9)
--cluster-mode Run client in cluster mode
-h, --help Display this help
-v, --version Display version information
Test Options:
-n, --requests=NUMBER Number of total requests per client (default: 10000)
use 'allkeys' to run on the entire key-range
-c, --clients=NUMBER Number of clients per thread (default: 50)
-t, --threads=NUMBER Number of threads (default: 4)
--test-time=SECS Number of seconds to run the test
--ratio=RATIO Set:Get ratio (default: 1:10)
--pipeline=NUMBER Number of concurrent pipelined requests (default: 1)
--reconnect-interval=NUM Number of requests after which re-connection is performed
--multi-key-get=NUM Enable multi-key get commands, up to NUM keys (default: 0)
--select-db=DB DB number to select, when testing a redis server
--distinct-client-seed Use a different random seed for each client
--randomize random seed based on timestamp (default is constant value)
Arbitrary command:
--command=COMMAND Specify a command to send in quotes.
Each command that you specify is run with its ratio and key-pattern options.
For example: --command="set __key__ 5" --command-ratio=2 --command-key-pattern=G
To use a generated key or object, enter:
__key__: Use key generated from Key Options.
__data__: Use data generated from Object Options.
--command-ratio The number of times the command is sent in sequence.(default: 1)
--command-key-pattern Key pattern for the command (default: R):
G for Gaussian distribution.
R for uniform Random.
S for Sequential.
P for Parallel (Sequential were each client has a subset of the key-range).
Object Options:
-d --data-size=SIZE Object data size (default: 32)
--data-offset=OFFSET Actual size of value will be data-size + data-offset
Will use SETRANGE / GETRANGE (default: 0)
-R --random-data Indicate that data should be randomized
--data-size-range=RANGE Use random-sized items in the specified range (min-max)
--data-size-list=LIST Use sizes from weight list (size1:weight1,..sizeN:weightN)
--data-size-pattern=R|S Use together with data-size-range
when set to R, a random size from the defined data sizes will be used,
when set to S, the defined data sizes will be evenly distributed across
the key range, see --key-maximum (default R)
--expiry-range=RANGE Use random expiry values from the specified range
Imported Data Options:
--data-import=FILE Read object data from file
--data-verify Enable data verification when test is complete
--verify-only Only perform --data-verify, without any other test
--generate-keys Generate keys for imported objects
--no-expiry Ignore expiry information in imported data
Key Options:
--key-prefix=PREFIX Prefix for keys (default: "memtier-")
--key-minimum=NUMBER Key ID minimum value (default: 0)
--key-maximum=NUMBER Key ID maximum value (default: 10000000)
--key-pattern=PATTERN Set:Get pattern (default: R:R)
G for Gaussian distribution.
R for uniform Random.
S for Sequential.
P for Parallel (Sequential were each client has a subset of the key-range).
--key-stddev The standard deviation used in the Gaussian distribution
(default is key range / 6)
--key-median The median point used in the Gaussian distribution
(default is the center of the key range)
WAIT Options:
--wait-ratio=RATIO Set:Wait ratio (default is no WAIT commands - 1:0)
--num-slaves=RANGE WAIT for a random number of slaves in the specified range
--wait-timeout=RANGE WAIT for a random number of milliseconds in the specified range (normal
distribution with the center in the middle of the range)
常用参数:
类型 | 参数 | 说明 |
2.4 压力测试
10个线程,每个线程20个客户端,总共100000次请求,SET与GET比例是3:2
[root]# memtier_benchmark -s 127.0.0.1 -p 6379 -c 20 -t 10 -n 100000 --random-data --randomize --distinct-client-seed -d 1000 --key-maximum=65000000 --key-minimum=1 --key-prefix= --ratio=3:2 --out-file=out.log
Writing results to out.log...
[RUN #1] Preparing benchmark client...
[RUN #1] Launching threads now...
[RUN #1 100%, 174 secs] 0 threads: 20000000 ops, 117589 (avg: 114379) ops/sec, 78.97MB/sec (avg: 73.31MB/sec), 1.70 (avg: 1.75) msec latency
注释:
耗时172秒,共执行了20000000次,平均每秒执行114379次,平均每秒写入73.31MB,平均延迟1.75毫秒。
查看输出日志
[root@guizhou_hp-pop-10-150-57-13 redis]# cat out.log
10 Threads
20 Connections per thread
100000 Requests per client
ALL STATS
============================================================================================================================
Type Ops/sec Hits/sec Misses/sec Avg. Latency p50 Latency p99 Latency p100 Latency KB/sec
----------------------------------------------------------------------------------------------------------------------------
Sets 68619.56 --- --- 1.74980 1.71100 2.47900 3.80700 69747.30
Gets 45746.37 3972.00 41774.37 1.74640 1.71100 2.41500 3.79100 5316.37
Waits 0.00 --- --- --- --- --- --- ---
Totals 114365.93 3972.00 41774.37 1.74844 1.71100 2.44700 3.80700 75063.66
分类:
Redis
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示