性能测试
redis-benchmark是Redis自带的基准性能测试工具, 它提供了很多选项帮助开发和运维人员测试Redis的相关性能。
以下测试实例指定实例中主机为 127.0.0.1,端口号为 6379,执行的命令为 set,lpush,请求数为 10000,通过 -q 参数让结果只显示每秒执行的请求数:
[root@test11 redis-5.0.5]# ./src/redis-benchmark -h 127.0.0.1 -p 6379 -t set,lpush -n 100000 -q
SET: 75528.70 requests per second
LPUSH: 103305.79 requests per second
- 当然也可以不指定测试命令,这样就是测试所有的redis命令的性能,如下:
[root@test11 redis-5.0.5]# ./src/redis-benchmark -n 100000
====== SET ======
100000 requests completed in 1.01 seconds
50 parallel clients
3 bytes payload
keep alive: 1
99.35% <= 1 milliseconds
99.75% <= 2 milliseconds
99.84% <= 3 milliseconds
99.90% <= 4 milliseconds
100.00% <= 4 milliseconds
99304.87 requests per second
====== GET ======
.
.
.
====== INCR ======
.
.
.
redis 性能测试工具可选参数如下所示:
系统监控
info可以查看redis的状态,如下:
具体参数含义请参考:https://redis.io/commands/info
127.0.0.1:6379> info
# Server
redis_version:5.0.5
redis_git_sha1:00000000
redis_git_dirty:0
.
# Clients
.
# Memory
.
# Persistence
.
# Stats
.
# Replication
.
# CPU
.
# Cluster
cluster_enabled:0
# Keyspace
db0:keys=8,expires=0,avg_ttl=0
可以具体的查看某一项的状态信息,语法:info [option] 下面将着重介绍一些比较重要的性能指标:
MEMORY
127.0.0.1:6379> info memory
# Memory
used_memory:2885952 #由Redis分配器分配的内存总量,以字节(byte)为单位,不包含内存碎片浪费掉的内存
used_memory_human:2.75M #与used_memory一样,只能单位转为M为单位,易于阅读
used_memory_rss_human:6.90M #从操作系统上显示已经分配的内存总量,包含内存碎片
used_memory_lua_human:37.00K #Lua脚本引擎所使用的内存大小
mem_fragmentation_ratio:2.54 #内存碎片率
mem_allocator:jemalloc-5.1.0 #在编译时指定的Redis使用的内存分配器
- 内存使用率是Redis服务最关键的一部分。如果一个Redis实例的内存使用率超过可用最大内存 (used_memory > 可用最大内存),那么操作系统开始进行内存与swap空间交换,Redis和依赖Redis上数据的应用会受到严重的性能影响。
- 若是在使用Redis期间没有开启rdb快照或aof持久化策略,那么缓存数据在Redis崩溃时就有丢失的危险。因为当Redis内存使用率超过可用内存的95%时,部分数据开始在内存与swap空间来回交换,这时就可能有丢失数据的危险。
- 可以按以下方案来避免这种问题
1、通过减少Redis的内存占用率
2、尽可能的使用Hash数据结构
3、设置key的过期时间
4、回收key
- 可以按以下方案来避免这种问题
- mem_fragmentation_ratio=used_memory_rss_human/used_memory_human;内存碎片率稍大于1是合理的,这个值表示内存碎片率比较低,也说明redis没有发生内存交换。但如果内存碎片率超过1.5,那就说明Redis消耗了实际需要物理内存的150%,其中50%是内存碎片率。若是内存碎片率低于1的话,说明Redis内存分配超出了物理内存,操作系统正在进行内存交换。内存交换会引起非常明显的响应延迟。
- 倘若内存碎片率超过了1.5,那可能是操作系统或Redis实例中内存管理变差的表现。下面有3种方法解决内存管理变差的问题,并提高Redis性能:
1、重启Redis服务器
2、限制内存交换
3、修改内存分配器
- 倘若内存碎片率超过了1.5,那可能是操作系统或Redis实例中内存管理变差的表现。下面有3种方法解决内存管理变差的问题,并提高Redis性能:
STATS
# Stats
total_connections_received:1309 #Redis自启动以来处理的客户端连接数总数
total_commands_processed:2240765 #Redis自启动以来命令处理数
rejected_connections:0 #Redis自启动以来拒绝的客户端连接数
latest_fork_usec:252 #获取最近一个fork操作的耗
时, 单位为微秒
- 可以写个脚本,定期记录total_commands_processed的值。当客户端明显发现响应时间过慢时,可以通过记录的total_commands_processed历史数据值来判断命理处理总数是上升趋势还是下降趋势,以便排查问题。
- 命令队列里的命令数量过多,后面命令一直在等待中,解决办法:
1、使用多参数命令,通过单命令多参数的形式取代多命令单参数的形式
2、管道命令:减少多命令的方法是使用管道(pipeline),把几个命令合并一起执行,从而减少因网络开销引起的延迟问题
3、避免操作大集合的慢命令:如果命令处理频率过低导致延迟时间增加,这可能是因为使用了高时间复杂度的命令操作导致 - 慢命令阻塞Redis,解决办法:
1、使用slowlog查出引发延迟的慢命令
- 命令队列里的命令数量过多,后面命令一直在等待中,解决办法:
CLIENTS
127.0.0.1:6379> info clients
# Clients
connected_clients:2 #当前Redis节点的客户端连接数,默认最大值为10000,一旦超过新的客户端连接将被拒绝。
client_recent_max_input_buffer:2 #最大输入缓冲区,可以设置超过10M报警
client_recent_max_output_buffer:0 #最大输出缓冲区
blocked_clients:0 #正在执行阻塞命令的客户端个数
Redis监控
基础环境:服务器:192.168.37.11
系统版本:centos7.6
go版本:1.12
由grafana+prometheus+redis_exporter组成监控套件,部暑如下:
grafana
执行如下命令安装:
yum install https://dl.grafana.com/oss/release/grafana-5.4.2-1.x86_64.rpm
启动:
systemctl start grafana-server
访问地址:http://192.168.37.11:3000/login 默认管理员帐号密码:admin admin
node_exporter
github地址:https://github.com/prometheus/node_exporter/
作用:用于机器系统数据收集,配合Prometheus使用,每个redis服务器上安装,此实例是单服务器,所以只安装一个。
安装node_exporter
[root@test11 ~]# go get github.com/prometheus/node_exporter [root@test11 ~]# cd go/src/github.com/prometheus/node_exporter [root@test11 redis_exporter]# make
启动:
[root@test11 node_exporter]# ./node_exporter &
安装成功界面如下:
redis_exporter
github地址:https://github.com/oliver006/redis_exporter
作用:抓取redis的指标,每个redis服务器上安装,此实例是单服务器,所以只安装一个。
安装redis_exporter:
[root@test11 ~]# go get github.com/oliver006/redis_exporter [root@test11 ~]# cd go/src/github.com/oliver006/redis_exporter/ [root@test11 redis_exporter]# go build
启动:
[root@test11 redis_exporter]# ./redis_exporter &
安装成功界面如下:
prometheus
github地址:https://github.com/prometheus/prometheus
定义:标准的数据搬运系统
下载:
wget https://github.com/prometheus/prometheus/archive/master.zip
解压到并移动到指定目录下:
unzip master.zip mv prometheus-master/ /usr/local/ cd /usr/local/prometheus-master
构建prometheus-master
make build
编辑prometheus.yml文件,监控原来创建的redis节点,如下:
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['localhost:9090']
labels:
instance: prometheus
- job_name: 'node_exporter'
static_configs:
- targets: ['192.168.37.11:9100']
- job_name: 'redis_exporter_targets'
static_configs:
- targets:
- redis://127.0.0.1:6379
- redis://192.168.37.11:7001
- redis://192.168.37.11:7002
- redis://192.168.37.11:7003
- redis://192.168.37.11:7004
- redis://192.168.37.11:7005
- redis://192.168.37.11:7006
metrics_path: /scrape
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.37.11:9121
- job_name: 'redis_exporter'
static_configs:
- targets:
- 192.168.37.11:9121
启动:
[root@test11 prometheus-master]# ./prometheus --config.file=prometheus.yml &
安装成功界面如下:
集成到Grafana
下载grafana的redis的prometheus-redis_rev1.json模板,下载地址:
https://grafana.com/api/dashboards/763/revisions/3/download在grafana中新建prometheus数据源,如下:
导入刚才下载的json模板,如下:
集成到grafana的模板界面如下: