Redis性能测试Redis-benchmark

本文通过实际案例展示了Redis性能调优的过程,分享了使用redis-benchmark工具进行性能测试的方法,并详细解释了各参数的作用及如何根据测试结果进行优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

=

最近排查redis的问题和优化,真费劲儿。

功夫都在细节处。

=

测试暂时用的脚本如下,还是能发现不少问题的。

-c并发数,

-n请求数,

-q仅仅输出简要结果避免太长,

-d以字节的形式指定 SET/GET 值的数据大小,

-r(Use random keys 且 指定key的长度,比如30)

./redis-benchmark -n 1000000 -q

./redis-benchmark -n 1000000 -q -r 30

./redis-benchmark -n 1000000 -q -r 50

./redis-benchmark -n 1000000 -q -r 80

./redis-benchmark -n 1000000 -q -r 30

./redis-benchmark -n 1000000 -q -r 30 -c 50

./redis-benchmark -n 1000000 -q -r 30 -c 50 -d 1024

./redis-benchmark -n 1000000 -q -r 30 -c 50 -d 2048

./redis-benchmark -n 1000000 -q -r 30 -d 398092

=

#首先看看标准文档,其实总比各种文要全。排查问题,很多还得弄的仔细些。

./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 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).

 -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.

 

#例子

测试命令事例:

1、redis-benchmark -h 192.168.1.201 -p 6379 -c 100 -n 100000 
100个并发连接,100000个请求,检测host为localhost 端口为6379的redis服务器性能 

2、redis-benchmark -h 192.168.1.201 -p 6379 -q -d 100  

测试存取大小为100字节的数据包的性能

3、redis-benchmark -t set,lpush -n 100000 -q

只测试某些操作的性能

4、redis-benchmark -n 100000 -q script load "redis.call('set','foo','bar')"

只测试某些数值存取的性能

 

中文参考:redis-benchmark http://www.runoob.com/redis/redis-benchmarks.html

redis-benchmark使用方法总结  https://blog.youkuaiyun.com/xubo245/article/details/48320689 中文翻译:

 -h <hostname>      Server hostname (default 127.0.0.1)                    //服务主机名  

 -p <port>          Server port (default 6379)                             //服务器端口号  

 -s <socket>        Server socket (overrides host and port)                //socket,覆盖host和port  

 -a <password>      Password for Redis Auth                                //密码  

 -c <clients>       Number of parallel connections (default 50)            //并行请求的客户端数量,默认50  

 -n <requests>      Total number of requests (default 100000)              //请求的总量,默认100000  

 -d <size>          Data size of SET/GET value in bytes (default 2)        //set或get的数据尺寸,单位是字节,默认是2  

 -dbnum <db>        SELECT the specified db number (default 0)             //选择指定的数据号  

 -k <boolean>       1=keep alive 0=reconnect (default 1)                   //布尔量,1代表保持活跃,0代表重连  

 -r <keyspacelen>   Use random keys for SET/GET/INCR, random values for SADD //set/get/incr使用随机的keys,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).    //管道请求,默认是1,没管道(并行?)  

 -q                 Quiet. Just show query/sec values                       //安静,仅仅展示每秒的查询值  

 --csv              Output in CSV format                                    //输出按CSV格式  

 -l                 Loop. Run the tests forever  

 -t <tests>         Only run the comma separated list of tests. The test    //仅仅运行tests  

                    names are the same as the ones produced as output.  

 -I                 Idle mode. Just open N idle connections and wait.       //闲置模型,进打开N个闲置链接且等待  

=

=

 

<think>好的,我现在需要解决用户的问题:确认redis-benchmark命令中参数顺序是否影响命令含义,特别是参数顺序-n10000 -q是否会影响命令执行。首先,我得回忆一下redis-benchmark的基本用法和相关参数。根据之前提供的引用内容,用户提到的引用[2]和引用[3]都提到了使用-t参数来选择测试用例,而引用[4]则展示了不同的参数组合,比如-c和-n。用户现在的问题是参数顺序是否重要,比如-n和-q的顺序不同是否会导致不同的结果。我需要确认redis-benchmark的解析方式。通常,命令行参数的解析依赖于工具的实现方式,是否使用位置参数还是选项参数。在大多数命令行工具中,选项参数的顺序通常不重要,因为它们是通过标志(如-n、-q)来识别的,而不是位置。例如,无论是-n10000 -q还是-q -n10000,只要参数和对应的值正确配对,工具应该能正确解析。 接下来,我需要验证这一点。查阅redis-benchmark的官方文档或帮助信息(可以通过redis-benchmark-h查看),看看参数是否对顺序敏感。根据引用[1]中的内容,用户提到redis-benchmark类似于Apache ab,而这类工具的参数通常不依赖于顺序。例如,-n指定请求数,-q表示安静模式,输出简化的结果,这两个参数是独立的选项,顺序应该不影响。另外,用户提供的引用[2]和[3]中的例子,比如-tset,lpush -n100000-q,其中参数的顺序是-t、-n、-q,而输出结果正常。这说明在这些例子中,参数的顺序并没有影响命令的执行结果。还需要考虑可能的特殊情况,比如某些参数是否互相依赖或有冲突。例如,如果某个参数需要另一个参数先被设置,但这种情况在redis-benchmark中是否存在呢?根据常见的参数设计,-n和-q都是独立的,没有依赖关系,因此它们的顺序应该无关紧要。为了进一步确认,可以设想两种命令形式:redis-benchmark -n10000-q和redis-benchmark-q-n10000,执行这两个命令,观察输出是否相同。根据引用[2]中的例子,-q的作用是让输出更简洁,只显示每秒请求数,而-n则指定总请求数。无论顺序如何,这两个参数的作用应该保持不变,因此输出结果应该一致。此外,用户可能担心参数顺序会影响解析错误,例如工具是否可能将某个值误认为是另一个参数。例如,如果写成-n -q10000,这里-q后面没有值,而-n后面跟的是非数值,就会出错。但用户的问题是关于正确的参数顺序,即参数和对应的值正确配对的情况下的顺序是否重要。在这种情况下,只要每个选项后面紧跟自己的值,顺序应该不重要。 总结:redis-benchmark的参数顺序不会影响命令的含义,只要每个参数及其对应的值正确配对。因此,-n10000-q和-q-n10000的效果是相同的。接下来,需要将分析过程转化为结构化的回答,并引用相关的参考资料。同时,生成相关问题,以帮助用户进一步理解。</think>### Redis-benchmark 参数顺序问题解析 **参数顺序不会影响命令含义**。`redis-benchmark` 的参数解析遵循标准命令行工具逻辑,参数顺序无关紧要,只要参数与值成对匹配即可[^1][^2]。 #### 验证示例 假设以下两条命令: ```bash redis-benchmark -n 10000 -q -t get redis-benchmark -q -t get -n 10000 ``` **结果一致性**: 两条命令完全等价,均表示: - 总请求数 `-n 10000` - 安静模式 `-q`(仅输出QPS) - 测试类型 `-t get`(仅测试GET命令) #### 关键原理 1. **参数独立性**: `redis-benchmark` 的参数设计为独立选项(如 `-n`、`-c`、`-t`),无依赖顺序性。 2. **值绑定规则**: 每个参数的值紧跟参数名(如 `-n 10000`),工具通过前缀(如 `-n`)识别参数类型,而非位置顺序[^4]。 #### 特殊情况说明 若参数与值未正确配对(如 `-n -q 10000`),则会导致解析错误,因为 `-n` 的值被误识别为 `-q`。 --- ### 性能测试建议 - **混合读写测试**:使用 `-t` 指定多个命令(如 `-t get,set`)[^2] - **延迟分析**:添加 `--latency` 参数获取响应时间分布[^1] - **真实场景模拟**:通过 `-r` 参数指定随机键范围(如 `-r 100000`)模拟多键操作[^3] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值