介绍
如果先服务上线前,我们想了解mysql的性能,可以使用一款mysql压测工具sysbench,是一款非常方便的工具,它可以帮你在数据库中构建大量的大数据,自动的创建表,接着模拟很多的线程去并发访问你的数据库,可以执行各种各样用于读写数据库的sql语句,以及提交复杂的事物
我们先去安装sysbench
安装sysbench
在linux服务器,使用root账户安装
curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh
yum -y install sysbench
执行上述两条命令,就可以安装完成了
数据准备阶段
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.1.106 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=eshop --tables=20 --table_size=1000000 oltp_read_write --db-ps-mode=disable prepare
数据安装完成以后,我们使用上述命令进行数据准备,下面介绍下各个命令的参数
1.–db-driver=mysql 使用mysql数据库驱动
2.–time=300 连续访问300s
3.-threads=10 10个线程模拟并发访问
4.-report-interval=1 每隔1s输出压测情况
5.–mysql-host=192.168.1.106 mysqlip地址
6.–mysql-port=3306 mysql端口号
7.–mysql-user=root mysql用户名
8.–mysql-password=root mysql密码
9.-mysql-db=eshop mysql数据库
10.–tables=20 表的数量
11.–table_size=1000000 每张表的数据量
12.oltp_read_write 读写模式,这里有多种模式,后面会继续介绍
13 -db-ps-mode=disable 禁用ps模式
14prepare 数据准备阶段,这里一般会有3种模式 run 运行, cleanup 清楚准备好的数据
压测阶段
这里会有多种的压测模式
读写压测 oltp_read_write run
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.1.106 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=eshop --tables=20 --table_size=1000000 oltp_read_write --db-ps-mode=disable run
只读压测 oltp_read_only
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.1.106 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=eshop --tables=20 --table_size=1000000 oltp_read_only --db-ps-mode=disable run
删除压测 oltp_delete
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.1.106 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=eshop --tables=20 --table_size=1000000 oltp_delete --db-ps-mode=disable run
更新字段索引 oltp_update_idnex
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.1.106 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=eshop --tables=20 --table_size=1000000 oltp_update_idnex --db-ps-mode=disable run
更新非字段索引功能 oltp_update_non_idnex
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.1.106 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=eshop --tables=20 --table_size=1000000 oltp_update_non_idnex --db-ps-mode=disable run
插入功能 oltp_insert
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.1.106 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=eshop --tables=20 --table_size=1000000 oltp_insert --db-ps-mode=disable run
写入性能 oltp_write_only
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.1.106 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=eshop --tables=20 --table_size=1000000 oltp_write_only --db-ps-mode=disable run
清除数据
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.1.106 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=eshop --tables=20 --table_size=1000000 oltp_read_write --db-ps-mode=disable cleanup
压测报告分析
[ 300s ] thds: 10 tps: 127.75 qps: 2044.99 (r/w/o: 1790.49/0.00/254.50) lat (ms,95%): 108.68 err/s: 0.00 reconn/s: 0.00
SQL statistics:
queries performed:
read: 461090
write: 0
other: 65870
total: 526960
transactions: 32935 (109.76 per sec.)
queries: 526960 (1756.23 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 300.0485s
total number of events: 32935
Latency (ms):
min: 38.32
avg: 91.08
max: 492.23
95th percentile: 130.13
sum: 2999714.63
Threads fairness:
events (avg/stddev): 3293.5000/26.39
我们分别解释输出报告的含义
[ 300s ] thds: 10 tps: 127.75 qps: 2044.99 (r/w/o: 1790.49/0.00/254.50) lat (ms,95%): 108.68 err/s: 0.00 reconn/s: 0.00
这个就是我们之前设定的 每秒1次的输出报告,由于我是在本地虚拟机压测的所以性能特别低
- thds: 10 10个线程
- tps: 127.75 每s执行了127.75 个事务
- qps: 2044.99 每s 执行2044.99个请求
- (r/w/o: 1790.49/0.00/254.50) 在2044.99 个请求中有1790.49个请求时读请求 0.00个写请求(我这里压测的是只读),有254.5 是其它请求
- lat (ms,95%): 108.68 有95%的请求延迟都在108ms以下
- err/s: 0.00 reconn/s: 0.00 meis有0个请求失败,有0个请求进行网络失联
总的分析报告:
SQL statistics:
queries performed:
read: 461090 //在300s请求内发生了461090次读请求
write: 0 //在300s请求内发生了0次写请求
other: 65870 //在300s请求内发生了65870次其它请求
total: 526960 //在300s请求内发生了526960 次请求
transactions: 32935 (109.76 per sec.) //300s 内一共执行3万多事务,每秒109个事务 TPS
queries: 526960 (1756.23 per sec.)//300s 内一共执行50万请求,每秒1756个请求 TPS
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 300.0485s // 进行了300s压测
total number of events: 32935 //事务有3万多个
Latency (ms):
min: 38.32 //请求中延迟最小的是38.32ms
avg: 91.08 //请求中平均延迟91.08ms
max: 492.23 // 请求中最大延迟 492ms
95th percentile: 130.13 //95%请求延迟在130ms
sum: 2999714.63
Threads fairness:
events (avg/stddev): 3293.5000/26.39
性能监控
但在压测的过程中我们还需要密切关注服务器cpu 内存,磁盘,网络的性能,下面我们介绍下关注它们的命令
cpu
在Linux服务器下,关注CPU的性能主要使用top
内存
free-m top
磁盘IO情况
观察磁盘吞吐量
dstat -d 查看每s读写的数量
dstat -r 查看随机读写的数量
网卡情况
dstat - -n