常见指标

  • TPS:单位时间内所处理的事务数
  • QPS:单位时间内所处理的查询数
  • Latency:响应时延,通常参考95%请求所在的响应时延。
  • 并发量:同时处理的查询请求数量

测试软件

通常使用 sysbench 测试MySQL性能。sysbench是一款开源的多线程性能测试工具,由C语言编写,可以执行CPU、内存、线程、IO、数据库等方面的性能测试。

测试环境

  • 规格:4C8G,100G数据盘
  • 系统:CentOS Linux release 7.9.2009 (Core)
  • MySQL版本:Percona Server 5.7.35-38
  • sysbench版本:1.0.20

测试准备

编译安装sysbench

本文以“sysbench-1.0.20”版本、数据库以Percona Server为例,下载源码,并编译安装。

  1. 下载地址:

  2. Percona Server需要安装如下rpm包:

    • ePercona-Server-devel-57-5.7.35-38.1.el7.x86_64.rpm(提供mysql_config命令及sysbench所需要的软件库)
    • Percona-Server-client-57-5.7.35-38.1.el7.x86_64.rpm
    • Percona-Server-server-57-5.7.35-38.1.el7.x86_64.rpm
    • Percona-Server-shared-57-5.7.35-38.1.el7.x86_64.rpm
    • Percona-Server-shared-compat-57-5.7.35-38.1.el7.x86_64.rpm
  3. 安装sysbench的依赖包。

1
yum install gcc gcc-c++ automake make libtool -y
  1. 查询Percona Server的库文件保存路径,记录回显信息中的“pkgincludedir”和“pkglibdir”的值,供后续步骤使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@mysql-5 ~]# mysql_config --help
Usage: /usr/bin/mysql_config-64 [OPTIONS]
Compiler: GNU 4.8.5
Options:
--cflags [-I/usr/include/mysql -m64 ]
--cxxflags [-I/usr/include/mysql -m64 ]
--include [-I/usr/include/mysql]
--libs [-L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lrt -lcoredumper -lssl -lcrypto -ldl]
--libs_r [-L/usr/lib64/mysql -lmysqlclient -lpthread -lz -lm -lrt -lcoredumper -lssl -lcrypto -ldl]
--plugindir [/usr/lib64/mysql/plugin]
--socket [/var/lib/mysql/mysql.sock]
--port [0]
--version [5.7.35-38]
--libmysqld-libs [-L/usr/lib64/mysql -l ]
--variable=VAR VAR is one of:
pkgincludedir [/usr/include/mysql]
pkglibdir [/usr/lib64/mysql]
plugindir [/usr/lib64/mysql/plugin]
  1. 下载并安装sysbench。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 下载sysbench源码
wget https://github.com/akopytov/sysbench/archive/refs/tags/1.0.20.tar.gz

# 解压
tar zxvf sysbench-1.0.20.tar.gz && cd sysbench-1.0.20

# 配置及生成编译文件
# - “/usr/include/mysql”为步骤3中查到的“pkgincludedir”的值。
# - “/usr/lib64/mysql” 为步骤3中查到的“pkglibdir”的值。
./autogen.sh
./configure --with-mysql-includes=/usr/include/mysql --with-mysql-libs=/usr/lib64/mysql --with-mysql

# 编译并安装
make && make install

# 验证sysbench
sysbench --version

MySQL数据库准备

1
2
3
4
5
6
# 允许用户远程登录(mysql 5.7)
grant all privileges on *.* to 'root'@'%' identified by 'Inspur@123';
flush privileges;

# 创建测试数据库
create database sbtest;

清理缓存及swap

1
2
3
4
5
6
#!/bin/sh
source ~/.bash_profile

sync
echo 3 >/proc/sys/vm/drop_caches
swapoff -a && swapon -a

清理并加载测试数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 清理测试数据
sysbench /root/sysbench-1.0.20/src/lua/oltp_read_write.lua \
--db-driver=mysql --mysql-host=127.0.0.1 \
--mysql-port=3306 --mysql-user=root \
--mysql-password='Inspur@123' \
--table_size=500000 --tables=4 \
--threads=1 --report-interval=2 cleanup

# 加载测试数据
sysbench /root/sysbench-1.0.20/src/lua/oltp_read_write.lua \
--db-driver=mysql --mysql-host=127.0.0.1 \
--mysql-port=3306 --mysql-user=root \
--mysql-password='Inspur@123' \
--table_size=500000 --tables=4 \
--threads=1 --report-interval=2 prepare

测试开始

1
2
3
4
5
6
7
8
9
# 运行测试
# 1. 数据量必须与加载的数据量一致,否则测出来的数据没有参考价值。
# 2. 并发数由 --threads 参数控制。
sysbench /root/sysbench-1.0.20/src/lua/oltp_read_write.lua \
--db-driver=mysql --mysql-host=127.0.0.1 \
--mysql-port=3306 --mysql-user=root \
--mysql-password='Inspur@123' \
--table_size=500000 --tables=4 \
--threads=1 --report-interval=2 run

结果分析

以如下24线程测试结果解析该数据库性能数据:

  • TPS:1427.66
  • QPS:28553.18
  • Latency(95th percentile):41.85
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 24
Report intermediate results every 2 second(s)
Initializing random number generator from current time


Initializing worker threads...

Threads started!

[ 2s ] thds: 24 tps: 1467.54 qps: 29452.70 (r/w/o: 20636.07/5871.16/2945.47) lat (ms,95%): 44.98 err/s: 0.00 reconn/s: 0.00
[ 4s ] thds: 24 tps: 1094.50 qps: 21940.91 (r/w/o: 15363.94/4386.48/2190.49) lat (ms,95%): 44.98 err/s: 0.00 reconn/s: 0.00
[ 6s ] thds: 24 tps: 1529.42 qps: 30551.81 (r/w/o: 21378.82/6115.16/3057.83) lat (ms,95%): 38.25 err/s: 0.00 reconn/s: 0.00
[ 8s ] thds: 24 tps: 1524.10 qps: 30495.54 (r/w/o: 21350.93/6095.41/3049.20) lat (ms,95%): 40.37 err/s: 0.00 reconn/s: 0.00
[ 10s ] thds: 19 tps: 1523.76 qps: 30353.84 (r/w/o: 21226.75/6091.06/3036.03) lat (ms,95%): 42.61 err/s: 0.00 reconn/s: 0.00
SQL statistics:
queries performed:
read: 200410
write: 57260
other: 28630
total: 286300
transactions: 14315 (1427.66 per sec.)
queries: 286300 (28553.18 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)

General statistics:
total time: 10.0257s
total number of events: 14315

Latency (ms):
min: 2.12
avg: 16.76
max: 608.98
95th percentile: 41.85
sum: 239986.30

Threads fairness:
events (avg/stddev): 596.4583/16.73
execution time (avg/stddev): 9.9994/0.01

参考文档