MariaDB sysbench基准测试

越发认识到了解数据库的性能是十分必要的,评估系统的能力,观察预期效果和实际效果的差别,对数据库的参数优化都是有作用的。

关于sysbench

sysbench是一个基于LuaJIT的可编写脚本的多线程基准测试工具,常用于数据库基准测试,包括olyp_*.luafileiocpumemorythreadsmutex几种方式。其中oltp_*.lua就是用于数据库的基准测试,在/usr/share/sysbench目录下。

安装

使用环境: VMware虚拟机、Ubuntu 18.04,MariaDB 10.4.7

1
2
curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.deb.sh | sudo bash
sudo apt -y install sysbench

安装完毕

1
2
$ sysbench --version
sysbench 1.0.17

基准测试

准备数据

先进入数据库创建一个用于基准测试的库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
MariaDB [(none)]> create database dbtest1a;
Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| dbtest1a |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)

MariaDB [(none)]>
1
2
3
4
5
6
7
$ sysbench /usr/share/sysbench/tests/include/oltp_legacy/oltp.lua --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password=6971f9c9c488c5ae --mysql-db=dbtest1a  --warmup-time=600 --rate=0 --percentile=99 --oltp-test-mode=complex --oltp-read-only=off --oltp-tables-count=10 --oltp-table-size=200000 --threads=10 --time=120 --rand-init=on --report-interval=10 prepare
sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

Creating table 'sbtest1'...
Inserting 200000 records into 'sbtest1'
Creating secondary indexes on 'sbtest1'...
......(余下省略)

部分命令选项说明:
--warmup-time: 预热时间,预防冷数据对测试结果影响
--rate: 该数字指定平均每个线程应执行的每秒事务数。0(默认值)表示无限速率,即事件尽可能快地执行
percentile: 输出99线的响应时间
--oltp-test-mode=complex: 执行模式,可选值有simple、nontrx、complex,默认是complex。其中simple模式下只测试简单的查询;nontrx不仅测试查询,还包括测试插入更新等,但是不使用事务;complex模式下测试最全面,会测试增删改查,而且会使用事务
--oltp-read-only=off: 表示不止产生只读SQL,也就是使用oltp.lua时会采用读写混合模式。默认off,如果设置为on,则不会产生update,delete,insert的SQL
--oltp-tables-count=10: 产生表的数量
--oltp-table-size=200000: 每个表产生的记录行数
--threads=10: 并发线程数
--time=120: 压力测试的持续时间
--rand-init=on: 是否随机初始化数据
--report-interval=10 表示每10s输出一次测试进度报告

执行测试

1
$ sysbench /usr/share/sysbench/tests/include/oltp_legacy/oltp.lua --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password=6971f9c9c488c5ae --mysql-db=dbtest1a  --warmup-time=600 --rate=0 --percentile=99 --oltp-test-mode=complex --oltp-read-only=off --oltp-tables-count=10 --oltp-table-size=200000 --threads=10 --time=120 --rand-init=on --report-interval=10 run

那么等待执行完即可看到测试的结果,当前测试花费120.0092S,TPS: 636.05/S,QPS: 12720.90/S,99%的请求花费44.17毫秒:

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
SQL statistics:
queries performed:
read: 1068648
write: 305328
other: 152664
total: 1526640
transactions: 76332 (636.05 per sec.)
queries: 1526640 (12720.90 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)

General statistics:
total time: 120.0092s
total number of events: 76332

Latency (ms):
min: 1.53
avg: 15.72
max: 868.91
99th percentile: 44.17
sum: 1199851.78

Threads fairness:
events (avg/stddev): 7633.2000/55.95
execution time (avg/stddev): 119.9852/0.00

清理数据

测试结束后,清理数据,以免影响到后续的测试:

1
$ sysbench /usr/share/sysbench/tests/include/oltp_legacy/oltp.lua --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password=6971f9c9c488c5ae --mysql-db=dbtest1a  --warmup-time=600 --rate=0 --percentile=99 --oltp-test-mode=complex --oltp-read-only=off --oltp-tables-count=10 --oltp-table-size=200000 --threads=10 --time=120 --rand-init=on --report-interval=10 cleanup
1
2
MariaDB [(none)]> drop database dbtest1a;
Query OK, 0 rows affected (0.002 sec)

补充

可以进入目录/usr/share/sysbench/tests/include/oltp_legacy查看oltp.lua的内容,贴上部分内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if pathtest then
dofile(pathtest .. "common.lua")
else
require("common")
end

function thread_init()
set_vars()

if (((db_driver == "mysql") or (db_driver == "attachsql")) and mysql_table_engine == "myisam") then
local i
local tables = {}
for i=1, oltp_tables_count do
tables[i] = string.format("sbtest%i WRITE", i)
end
begin_query = "LOCK TABLES " .. table.concat(tables, " ,")
commit_query = "UNLOCK TABLES"
else
begin_query = "BEGIN"
commit_query = "COMMIT"
end

end

结合common.lua脚本,就可以根据自己的业务来编写lua脚本用于sysbench基准测试。

推荐

在学习sysbench的使用时,搜索到美团的文章,很值得学习: sysbench在美团点评中的应用

参考

http://seanlook.com/2016/03/28/mysql-sysbench/

https://www.cnblogs.com/kismetv/p/7615738.html

0%