redis-benchmark 基准测试
大家好,我是爱吃鱼的程序员,一个渴望在互联网行业做到C位的程序员。可柔可刚,点赞则柔,白嫖则刚!看完记得给我来个三连哦!欢迎私信!
1. 概述
当我们希望提高性能的使用,往往想到的是异步、缓存这个两种手段。
- 前者,例如说内存队列(例如说 JDK Queue、Disruptor 等)、分布式队列(例如说 RabbitMQ、RocketMQ、Kafka),更多适合写操作。
- 后者,例如说内存缓存(例如说 JDK Map、EhCache 等)、分布式缓存(例如说 Redis、Memcached 等),更适合读操作。
不过,本文我们不会去聊上述所有的手段或是框架、中间件,而是聚焦本文的主角 Redis 。
日常中,我们经常能在公司、论坛、技术群里看到如下一段对话:
甲:我们的 MySQL 读取很慢啊,有什么办法解决啊?
乙:上缓存啊,Redis 额。
那么,为什么上 Redis 就一般能解决读的问题呢?为了避免将问题复杂化,我们直接看 Redis 和 MySQL 的性能对比。还是老样子,我们来对比阿里云的 MySQL 性能规格 和 Redis 性能规格 :
- 1C 1GB 配置
- Redis 1C 1GB 主从版,提供 80000 QPS
- MySQL 1C 1GB 通用型,提供 465 QPS
- 相差 172 倍左右的性能
- 16C 128G 配置
- Redis 16C 128G 集群版(单副本),提供 1280000 QPS
- MySQL 16C 128G 独享版,提供 48102 QPS
- 相差 26 倍左右的性能
当然,两者测试的方式,有一定差异,这里仅仅作为一个量级上的对比。
在开始基准测试之前,我们再来看看 Redis 大体的性能规格,从各大云厂商提供的 Redis 云服务。
2. 性能指标
通过我们看各大厂商提供的指标,我们不难发现,主要是 QPS 。
3. 测试工具
Redis 的性能测试工具,目前主流使用的是 redis-benchmark 。为什么这么说呢?
- 在我们 Google 搜索 “Redis 性能测试”时,清一色的文章选择的工具,清一色的都是 redis-benchmark 。
- 我翻看了云厂商(腾讯云、UCloud 等),提供的测试方法,都是基于 redis-benchmark 。
当然,也是有其它工具:
考虑到主流,本文使用 redis-benchmark 作为性能测试工具。
4. redis-benchmark
FROM 《Redis 有多快?》
Redis 自带了一个叫 redis-benchmark 的工具来模拟 N 个客户端同时发出 M 个请求。(类似于 Apache ab 程序)。
4.1 测试环境
艿艿:和我一样抠门(穷)的胖友,可以买竞价类型服务器,使用完后,做成镜像。等下次需要使用的时候,恢复一下。HOHO 。
- 系统 :CentOS 7.6 64位
- CPU :4 核
- 内存 :8 GB
- 磁盘 :40 GB ESSD 云盘
- Redis :5.0.5
不想编译安装的朋友,可以看看 《How to Install Latest Redis on CentOS 7》 文章。
4.2 安装工具
因为 redis-benchmark 是 Redis 自带的,所以不需要专门去安装,舒服~。
4.3 使用指南
redis-benchmark 的使用非常简单,只要了解它每个参数的作用,就可以非常方便的执行一次性能测试。我们来一起看看有哪些参数。执行 redis-benchmark -h 命令,返回参数列表:
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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)