MariaDB sysbench基准测试
越发认识到了解数据库的性能是十分必要的,评估系统的能力,观察预期效果和实际效果的差别,对数据库的参数优化都是有作用的。
关于sysbench
sysbench是一个基于LuaJIT的可编写脚本的多线程基准测试工具,常用于数据库基准测试,包括olyp_*.lua
、fileio
、cpu
、memory
、threads
、mutex
几种方式。其中oltp_*.lua
就是用于数据库的基准测试,在/usr/share/sysbench
目录下。
安装
使用环境: VMware虚拟机、Ubuntu 18.04,MariaDB 10.4.7
1 | curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.deb.sh | sudo bash |
安装完毕
1 | sysbench --version |
基准测试
准备数据
先进入数据库创建一个用于基准测试的库:
1 | MariaDB [(none)]> create database dbtest1a; |
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 prepare |
部分命令选项说明:--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 | SQL statistics: |
清理数据
测试结束后,清理数据,以免影响到后续的测试:
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 | MariaDB [(none)]> drop database dbtest1a; |
补充
可以进入目录/usr/share/sysbench/tests/include/oltp_legacy
查看oltp.lua
的内容,贴上部分内容:
1 | if pathtest then |
结合common.lua
脚本,就可以根据自己的业务来编写lua脚本用于sysbench基准测试。
推荐
在学习sysbench的使用时,搜索到美团的文章,很值得学习: sysbench在美团点评中的应用