Redis性能测试
Redis自带一个性能测试工具:redis-benchmark。
启动redis后,输入redis-benchmark命令和参数即可。
序号 | 选项 | 描述 | 默认值 |
---|---|---|---|
1 | -h | 指定服务器主机名 | 127.0.0.1 |
2 | -p | 指定服务器端口 | 6379 |
3 | -s | 指定服务器 socket | |
4 | -c | 指定并发连接数 | 50 |
5 | -n | 指定请求数 | 10000 |
6 | -d | 以字节的形式指定 SET/GET 值的数据大小 | 2 |
7 | -k | 1=keep alive 0=reconnect | 1 |
8 | -r | SET/GET/INCR 使用随机 key, SADD 使用随机值 | |
9 | -P | 通过管道传输 请求 | 1 |
10 | -q | 强制退出 redis。仅显示 query/sec 值 | |
11 | –csv | 以 CSV 格式输出 | |
12 | -l(小写的L) | 生成循环,永久执行测试 | |
13 | -t | 仅运行以逗号分隔的测试命令列表 | |
14 | -I(大写的i) | Idle 模式。仅打开 N 个 idle 连接并等待 |
拿出一块内容分析一下,其他的类似。
# 这是一个INLINE的PING命令的测试结果
====== PING_INLINE ======
# 发送了100000个请求,在1.26s完成
100000 requests completed in 1.26 seconds
# 并发客户端是50个
50 parallel clients
# 每次发送3个byte的数据
3 bytes payload
# 只有一台服务器处理这个请求
keep alive: 1
# 测试的一些配置
host configuration "save": 3600 1 300 100 60 10000
host configuration "appendonly": no
multi-thread: no
# 按百分比分布的延迟
Latency by percentile distribution:
0.000% <= 0.143 milliseconds (cumulative count 3)
50.000% <= 0.303 milliseconds (cumulative count 52929)
75.000% <= 0.375 milliseconds (cumulative count 76779)
87.500% <= 0.423 milliseconds (cumulative count 87745)
93.750% <= 0.487 milliseconds (cumulative count 93962)
96.875% <= 0.559 milliseconds (cumulative count 96916)
98.438% <= 0.647 milliseconds (cumulative count 98487)
99.219% <= 0.759 milliseconds (cumulative count 99222)
99.609% <= 0.911 milliseconds (cumulative count 99627)
99.805% <= 1.015 milliseconds (cumulative count 99805)
99.902% <= 1.119 milliseconds (cumulative count 99907)
99.951% <= 1.215 milliseconds (cumulative count 99954)
99.976% <= 1.295 milliseconds (cumulative count 99976)
99.988% <= 1.375 milliseconds (cumulative count 99988)
99.994% <= 1.487 milliseconds (cumulative count 99994)
99.997% <= 1.647 milliseconds (cumulative count 99997)
99.998% <= 1.759 milliseconds (cumulative count 99999)
99.999% <= 1.871 milliseconds (cumulative count 100000)
100.000% <= 1.871 milliseconds (cumulative count 100000)
# 延迟的累计分布
Cumulative distribution of latencies:
0.000% <= 0.103 milliseconds (cumulative count 0)
0.858% <= 0.207 milliseconds (cumulative count 858)
52.929% <= 0.303 milliseconds (cumulative count 52929)
84.866% <= 0.407 milliseconds (cumulative count 84866)
94.852% <= 0.503 milliseconds (cumulative count 94852)
97.909% <= 0.607 milliseconds (cumulative count 97909)
98.967% <= 0.703 milliseconds (cumulative count 98967)
99.378% <= 0.807 milliseconds (cumulative count 99378)
99.602% <= 0.903 milliseconds (cumulative count 99602)
99.796% <= 1.007 milliseconds (cumulative count 99796)
99.895% <= 1.103 milliseconds (cumulative count 99895)
99.947% <= 1.207 milliseconds (cumulative count 99947)
99.977% <= 1.303 milliseconds (cumulative count 99977)
99.990% <= 1.407 milliseconds (cumulative count 99990)
99.994% <= 1.503 milliseconds (cumulative count 99994)
99.996% <= 1.607 milliseconds (cumulative count 99996)
99.998% <= 1.703 milliseconds (cumulative count 99998)
99.999% <= 1.807 milliseconds (cumulative count 99999)
100.000% <= 1.903 milliseconds (cumulative count 100000)
# 摘要信息,每秒钟处理79428.12个请求
Summary:
throughput summary: 79428.12 requests per second
# 延迟的摘要信息
latency summary (msec):
avg min p50 p95 p99 max
0.330 0.136 0.303 0.511 0.711 1.871
看到这个数据,我们就可以直观的理解,Redis的性能到底有多快了。
Redis基本知识说明
Redis默认有16个数据库,默认使用的是第0个。
[root@localhost bin]# redis-cli
# 使用select命令切换到数据库1
127.0.0.1:6379> select 1
OK
# 使用DBSIZE查看当前数据库里的数据量
127.0.0.1:6379[1]> DBSIZE
(integer) 0
# set数据
127.0.0.1:6379[1]> set key value
OK
# get数据
127.0.0.1:6379[1]> get key
"value"
# 使用DBSIZE查看当前数据库里的数据量
127.0.0.1:6379[1]> DBSIZE
# 查看所有的key
127.0.0.1:6379[1]> keys *
1) "key"
# 清空当前数据库
127.0.0.1:6379[1]> FLUSHDB
OK
# 清空所有数据库
127.0.0.1:6379> FLUSHALL
OK
(integer) 1
在Redis 6.0之前,Redis是单线程的,CPU不是Redis的性能瓶颈,Redis的性能瓶颈是机器的内存和网络带宽。
在Redis 6.0之后,加入了多线程,在某些操作的时候可以提高它的数据处理速度。
Redis底层是C语言编写的,Redis的数据都是存在内存里的,对于内存系统,没有上下文切换效率就是最高的。
Key基本命令
# set一个key-value
127.0.0.1:6379> set key value
OK
# exists命令查看key是否存在
127.0.0.1:6379> exists key
(integer) 1
127.0.0.1:6379> exists key1
(integer) 0
# 把key-value移动到数据库1
127.0.0.1:6379> move key 1
(integer) 1
# 再次填充一个key-value
127.0.0.1:6379> set key value
OK
# 设置key的过期时间是10秒
127.0.0.1:6379> expire key 10
(integer) 1
# 查看key的剩余时间
127.0.0.1:6379> ttl key
(integer) 6
# 查看key对应value的类型
127.0.0.1:6379> type key
string
# 删除key
127.0.0.1:6379> del key
(integer) 1
其余更多命令可以在中文官网查看。