Redis实用监控工具一览

程序员面试吧

共 17089字,需浏览 35分钟

 ·

2024-04-12 04:34

v博客前言

Redis已经成为web应用开发不可或缺的一个组成部分,在项目中的应用越来越广泛,这篇文章就来讲讲那些关于Redis监控的那点事。

回到顶部

vredis-benchmark

1.1 简介

第一个就介绍一下,Redis自带的性能检测工具redis-benchmark, 该工具可以模拟 N 个客户端同时发出 Y 个请求。可以使用 redis-benchmark -h 来查看基准参数。

1.2 命令格式:

redis-benchmark [-h ] [-p ] [-c ] [-n <requests]> [-k ]

1.3 参数介绍:

序号 选项 描述 默认值
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 通过管道传输 <numreq> 请求 1
10 -q 强制退出 redis。仅显示 query/sec 值
11 --csv 以 CSV 格式输出
12 -l 生成循环,永久执行测试
13 -t 仅运行以逗号分隔的测试命令列表。
14 -I Idle 模式。仅打开 N 个 idle 连接并等待。

1.4 实例:

1.4.1 同时执行1000个请求来检测性能:

redis-benchmark -n 1000 -q

1.4.2 50个并发请求,10000个请求,检测Redis性能:

redis-benchmark -h localhost -p 6379 -c 50 -n 10000


[root@localhost toutou]# redis-benchmark -h localhost -p 6379 -c 50 -n 10000====== PING_INLINE ======  10000 requests completed in 0.11 seconds  50 parallel clients  3 bytes payload  keep alive: 1
96.25% <= 1 milliseconds98.38% <= 2 milliseconds99.01% <= 3 milliseconds100.00% <= 4 milliseconds88495.58 requests per second
====== PING_BULK ====== 10000 requests completed in 0.10 seconds 50 parallel clients 3 bytes payload keep alive: 1
97.74% <= 1 milliseconds100.00% <= 2 milliseconds95238.10 requests per second
====== SET ====== 10000 requests completed in 0.11 seconds 50 parallel clients 3 bytes payload keep alive: 1
98.44% <= 1 milliseconds100.00% <= 1 milliseconds93457.95 requests per second
====== GET ====== 10000 requests completed in 0.11 seconds 50 parallel clients 3 bytes payload keep alive: 1
98.33% <= 1 milliseconds99.13% <= 2 milliseconds100.00% <= 2 milliseconds93457.95 requests per second
====== INCR ====== 10000 requests completed in 0.10 seconds 50 parallel clients 3 bytes payload keep alive: 1
98.28% <= 1 milliseconds100.00% <= 1 milliseconds95238.10 requests per second
====== LPUSH ====== 10000 requests completed in 0.10 seconds 50 parallel clients 3 bytes payload keep alive: 1
98.70% <= 1 milliseconds100.00% <= 1 milliseconds97087.38 requests per second
====== RPUSH ====== 10000 requests completed in 0.10 seconds 50 parallel clients 3 bytes payload keep alive: 1
98.66% <= 1 milliseconds100.00% <= 1 milliseconds95238.10 requests per second
====== LPOP ====== 10000 requests completed in 0.15 seconds 50 parallel clients 3 bytes payload keep alive: 1
93.78% <= 1 milliseconds96.51% <= 2 milliseconds97.35% <= 3 milliseconds98.41% <= 4 milliseconds99.02% <= 5 milliseconds99.23% <= 6 milliseconds99.46% <= 7 milliseconds99.96% <= 8 milliseconds99.97% <= 9 milliseconds100.00% <= 9 milliseconds67567.57 requests per second
====== RPOP ====== 10000 requests completed in 0.31 seconds 50 parallel clients 3 bytes payload keep alive: 1
65.78% <= 1 milliseconds84.10% <= 2 milliseconds90.96% <= 3 milliseconds94.19% <= 4 milliseconds95.72% <= 5 milliseconds97.05% <= 6 milliseconds98.33% <= 7 milliseconds98.80% <= 8 milliseconds99.40% <= 9 milliseconds99.72% <= 10 milliseconds100.00% <= 14 milliseconds31746.03 requests per second
====== SADD ====== 10000 requests completed in 0.19 seconds 50 parallel clients 3 bytes payload keep alive: 1
93.00% <= 1 milliseconds96.88% <= 2 milliseconds98.33% <= 3 milliseconds98.92% <= 6 milliseconds98.94% <= 7 milliseconds98.95% <= 9 milliseconds99.04% <= 10 milliseconds99.48% <= 12 milliseconds99.61% <= 14 milliseconds99.62% <= 15 milliseconds99.99% <= 16 milliseconds100.00% <= 16 milliseconds52083.33 requests per second
====== HSET ====== 10000 requests completed in 0.11 seconds 50 parallel clients 3 bytes payload keep alive: 1
95.90% <= 1 milliseconds99.95% <= 2 milliseconds100.00% <= 2 milliseconds90909.09 requests per second
====== SPOP ====== 10000 requests completed in 0.11 seconds 50 parallel clients 3 bytes payload keep alive: 1
97.04% <= 1 milliseconds99.75% <= 2 milliseconds99.78% <= 3 milliseconds100.00% <= 3 milliseconds90909.09 requests per second
====== LPUSH (needed to benchmark LRANGE) ====== 10000 requests completed in 0.11 seconds 50 parallel clients 3 bytes payload keep alive: 1
96.48% <= 1 milliseconds99.46% <= 2 milliseconds99.95% <= 3 milliseconds100.00% <= 3 milliseconds87719.30 requests per second
====== LRANGE_100 (first 100 elements) ====== 10000 requests completed in 0.33 seconds 50 parallel clients 3 bytes payload keep alive: 1
32.63% <= 1 milliseconds93.24% <= 2 milliseconds99.83% <= 3 milliseconds100.00% <= 3 milliseconds30303.03 requests per second
====== LRANGE_300 (first 300 elements) ====== 10000 requests completed in 0.85 seconds 50 parallel clients 3 bytes payload keep alive: 1
2.65% <= 1 milliseconds23.01% <= 2 milliseconds53.33% <= 3 milliseconds77.25% <= 4 milliseconds91.47% <= 5 milliseconds98.58% <= 6 milliseconds99.99% <= 7 milliseconds100.00% <= 7 milliseconds11764.71 requests per second
====== LRANGE_500 (first 450 elements) ====== 10000 requests completed in 1.22 seconds 50 parallel clients 3 bytes payload keep alive: 1
1.01% <= 1 milliseconds9.09% <= 2 milliseconds28.25% <= 3 milliseconds50.31% <= 4 milliseconds68.06% <= 5 milliseconds81.18% <= 6 milliseconds90.78% <= 7 milliseconds96.96% <= 8 milliseconds99.43% <= 9 milliseconds100.00% <= 9 milliseconds8196.72 requests per second
====== LRANGE_600 (first 600 elements) ====== 10000 requests completed in 1.57 seconds 50 parallel clients 3 bytes payload keep alive: 1
0.61% <= 1 milliseconds4.90% <= 2 milliseconds14.77% <= 3 milliseconds28.67% <= 4 milliseconds44.56% <= 5 milliseconds59.45% <= 6 milliseconds72.38% <= 7 milliseconds82.29% <= 8 milliseconds90.01% <= 9 milliseconds95.42% <= 10 milliseconds98.34% <= 11 milliseconds99.78% <= 12 milliseconds100.00% <= 12 milliseconds6357.28 requests per second
====== MSET (10 keys) ====== 10000 requests completed in 0.19 seconds 50 parallel clients 3 bytes payload keep alive: 1
68.40% <= 1 milliseconds98.61% <= 2 milliseconds100.00% <= 3 milliseconds53763.44 requests per second

[root@localhost toutou]#


回到顶部

vredis-cli

2.1 简介

查看redis的连接及读写操作

2.2 命令格式

redis-cli -h xx -p yy monitor

2.3 实例:

2.4 redis-cli info:

Redis 监控最直接的方法就是使用系统提供的 info 命令,只需要执行下面一条命令,就能获得 Redis 系统的状态报告。

# Serverredis_version:5.0.2                    # Redis 的版本redis_git_sha1:00000000redis_git_dirty:0redis_build_id:bf5d1747be5380fredis_mode:standaloneos:Linux 2.6.32-220.7.1.el6.x86_64 x86_64arch_bits:64multiplexing_api:epollgcc_version:4.4.7                       #gcc版本process_id:49324                        # 当前 Redis 服务器进程idrun_id:bbd7b17efcf108fdde285d8987e50392f6a38f48tcp_port:6379uptime_in_seconds:1739082               # 运行时间(秒)uptime_in_days:20                       # 运行时间(天)hz:10lru_clock:1734729config_file:/home/s/apps/RedisMulti_video_so/conf/zzz.conf # Clientsconnected_clients:1                     #连接的客户端数量client_longest_output_list:0client_biggest_input_buf:0blocked_clients:0 # Memoryused_memory:821848                       #Redis分配的内存总量             used_memory_human:802.59Kused_memory_rss:85532672                 #Redis分配的内存总量(包括内存碎片)used_memory_peak:178987632used_memory_peak_human:170.70M           #Redis所用内存的高峰值used_memory_lua:33792mem_fragmentation_ratio:104.07           #内存碎片比率mem_allocator:tcmalloc-2.0 # Persistenceloading:0rdb_changes_since_last_save:0            #上次保存数据库之后,执行命令的次数rdb_bgsave_in_progress:0                 #后台进行中的 save 操作的数量rdb_last_save_time:1410848505            #最后一次成功保存的时间点,以 UNIX 时间戳格式显示rdb_last_bgsave_status:okrdb_last_bgsave_time_sec:0rdb_current_bgsave_time_sec:-1aof_enabled:0                            #redis是否开启了aofaof_rewrite_in_progress:0aof_rewrite_scheduled:0aof_last_rewrite_time_sec:-1aof_current_rewrite_time_sec:-1aof_last_bgrewrite_status:okaof_last_write_status:ok # Statstotal_connections_received:5705          #运行以来连接过的客户端的总数量total_commands_processed:204013          # 运行以来执行过的命令的总数量instantaneous_ops_per_sec:0rejected_connections:0sync_full:0sync_partial_ok:0sync_partial_err:0expired_keys:34401                       #运行以来过期的 key 的数量evicted_keys:0                           #运行以来删除过的key的数量keyspace_hits:2129                       #命中key 的次数keyspace_misses:3148                     #没命中key 的次数pubsub_channels:0                        #当前使用中的频道数量pubsub_patterns:0                        #当前使用中的模式数量latest_fork_usec:4391 # Replicationrole:master                              #当前实例的角色master还是slaveconnected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0 # CPUused_cpu_sys:1551.61used_cpu_user:1083.37used_cpu_sys_children:2.52used_cpu_user_children:16.79 # Keyspacedb0:keys=3,expires=0,avg_ttl=0             #各个数据库的 key 的数量,以及带有生存期的 key 的数量

redis-cli info

结果会返回 Server、Clients、Memory、Persistence、Stats、Replication、CPU、Keyspace 8个部分。从info大返回结果中提取相关信息,就可以达到有效监控的目的。

回到顶部

vshowlog

3.1 简介

redis的slowlog是redis用于记录记录慢查询执行时间的日志系统。由于slowlog只保存在内存中,因此slowlog的效率很高,完全不用担心会影响到redis的性能。Slowlog是Redis从2.2.12版本引入的一条命令。

3.2 命令格式

在redis-cli中有关于slowlog的设置:

CONFIG SET slowlog-log-slower-than 6000

CONFIG SET slowlog-max-len 25

3.3 实例:


上面介绍的都是关于Redis自带的命令化性能查询工具。下面介绍介绍一些第三方的Redis可视化性能监控工具。


回到顶部

vRedisLive

4.1 简介

RedisLive是由Python编写的开源的图形化监控工具。核心服务部分只包括一个web服务和基于Redis自带的Info命令以及monitor命令的监控服务。支持多实例监控,监控信息可以使用redis存储和sqlite持久化存储。

4.2 安装

4.2.1 安装依赖环境

RedisLive是由Python2.X编写的,所以最好使用Python2.7来运行RedisLive,在CentOS 7中预安装了Python2.7,但没有安装Python的包管理器pip。

yum install epel-releasesudo yum install python-pippip install --upgrade pippip install tornadopip install redispip install python-dateutil

4.2.2 安装RedisLive

git clone https://github.com/nkrode/RedisLive.git

4.2.3 修改配置文件redis-live.conf

cd RedisLive/src


//按照以下方式修改配置文件{    "RedisServers":            [         #在此处添加需要监控的redis实例        {              "server": "127.0.0.1",                #redis监听地址,此处为本机              "port" : 6379,                        #redis端口号,可以通过lsof -i | grep redis-ser查看 redis-server端口号              "password" : "some-password"          #redis认证密码,如果没有可以删除该行,注意json格式        }            ],
"DataStoreType" : "redis", #监控数据存储方案的配置,可选择redis或sqllite #用来存储监控数据的 Redis 实例 "RedisStatsServer": { "server" : "127.0.0.1", "port" : 6379, "password" : "some-password" }, #监控数据持久化数据存储配置 "SqliteStatsStore" : { "path": "db/redislive.sqlite" #redis数据文件 }}

redis-live.conf的配置可以参考redis-live.conf.example

4.3 启动

启动监控服务,每60秒监控一次

./redis-monitor.py --duration=60

再次开启一个终端,进入/root/RedisLive/src目录,启动web服务

./redis-live.py

4.4 效果图

回到顶部

vredis-faina

5.1 简介

5.1.1 背景

redis-faina是由Instagram开发并开源的一个 Redis 查询分析小工具。Instagram团队曾经使用 PGFouine 来作为其PostgreSQL的查询分析工具,他们觉得Redis也需要一个类似的工具来进行query分析工作,于是开发了 redis-faina。

5.1.1 概念

redis-faina 是通过Redis的 MONITOR命令来实现的,通过对在Redis上执行的query进行监控,统计出一段时间的query特性。

5.2 安装

git clone https://github.com/facebookarchive/redis-faina.git

5.3 命令介绍


[root@localhost toutou]# cd redis-faina/[root@localhost redis-faina]# lsheroku-redistogo-faina.sh  LICENSE  README.md  redis-faina.py[root@localhost redis-faina]# ./redis-faina.py -husage: redis-faina.py [-h] [--prefix-delimiter PREFIX_DELIMITER]                      [--redis-version REDIS_VERSION]                      [input]
positional arguments: input File to parse; will read from stdin otherwise
optional arguments: -h, --help show this help message and exit --prefix-delimiter PREFIX_DELIMITER String to split on for delimiting prefix and rest of key --redis-version REDIS_VERSION Version of the redis server being monitored[root@localhost redis-faina]#

其中--prefix-delimiter主要用于统计前缀的key的数据。

可以通过redis MONITOR命令以及管道进行分析,例如:

redis-cli -p 6379 MONITOR | head -n | ./redis-faina.py [options]

或者

redis-cli -p 6379 MONITOR > outfile.txt

./redis-faina.py ./outfile.txt


Overall Stats========================================Lines Processed     117773Commands/Sec        11483.44
Top Prefixes========================================friendlist 69945followedbycounter 25419followingcounter 10139recentcomments 3276queued 7
Top Keys========================================friendlist:zzz:1:2 534followingcount:zzz 227friendlist:zxz:1:2 167friendlist:xzz:1:2 165friendlist:yzz:1:2 160friendlist:gzz:1:2 160friendlist:zdz:1:2 160friendlist:zpz:1:2 156
Top Commands========================================SISMEMBER 59545HGET 27681HINCRBY 9413SMEMBERS 9254MULTI 3520EXEC 3520LPUSH 1620EXPIRE 1598
Command Time (microsecs)========================================Median 78.2575% 105.090% 187.2599% 411.0
Heaviest Commands (microsecs)========================================SISMEMBER 5331651.0HGET 2618868.0HINCRBY 961192.5SMEMBERS 856817.5MULTI 311339.5SADD 54900.75SREM 40771.25EXEC 28678.5
Slowest Calls========================================3490.75 "SMEMBERS" "friendlist:zzz:1:2"2362.0 "SMEMBERS" "friendlist:xzz:1:3"2061.0 "SMEMBERS" "friendlist:zpz:1:2"1961.0 "SMEMBERS" "friendlist:yzz:1:2"1947.5 "SMEMBERS" "friendlist:zpz:1:2"1459.0 "SISMEMBER" "friendlist:hzz:1:2" "zzz"1416.25 "SMEMBERS" "friendlist:zhz:1:2"1389.75 "SISMEMBER" "friendlist:zzx:1:2" "zzz"

回到顶部

v博客总结

关于Redis的监控工具还有很多,这里就不一一列举了,下面给出其它几款优秀的Redis监控工具链接,感兴趣的可以看看。

其他监控工具:

  • https://github.com/junegunn/redis-stat

  • https://github.com/steelThread/redmon

  • https://github.com/oliver006/redis_exporter

链接:https://www.cnblogs.com/toutou/p/redis_monitor.html

                                                             (版权归原作者所有,侵删)


浏览 15
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报