失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 死磕数据库系列(三十四):MySQL 性能测试工具 sysbench 详解

死磕数据库系列(三十四):MySQL 性能测试工具 sysbench 详解

时间:2024-03-17 03:42:15

相关推荐

死磕数据库系列(三十四):MySQL 性能测试工具 sysbench 详解

关注公众号,回复“1024”获取2TB学习资源!

前面给大家介绍了:死磕数据库系列(三十三):MySQL 性能分析与相关工具的使用。今天我将详细的为大家介绍 MySQL 数据库的性能测试工具 sysbench 的相关知识 ,希望大家能够从中收获多多!如有帮助,请点在看、转发支持一波!!!

简介和安装

sysbench 是一个很不错的数据库性能测试工具。

官方站点:/akopytov/sysbench/

rpm包下载:https://packagecloud.io/akopytov/sysbench/packages/el/7/sysbench-1.0.15-1.el7.centos.x86_64.rpm

源码包下载:/akopytov/sysbench/archive/1.0.15.tar.gz

如果是编译安装,需要先安装好 mysql 的开发包(尽管编译错误时提示的是缺少 Mysql 库文件)。

yum-yinstallmysql-community-develtarxf1.0.15.tar.gzcdsysbench-1.0.15./autogen.sh./configuremake-jmakeinstall

安装后,只有一个二进制文件sysbench,还提供了很多个lua脚本。

[root@s1~]#rpm-qlsysbench|grep'bin\|lua'/usr/bin/sysbench/usr/share/sysbench/bulk_insert.lua/usr/share/sysbench/oltp_common.lua/usr/share/sysbench/oltp_delete.lua/usr/share/sysbench/oltp_insert.lua/usr/share/sysbench/oltp_point_select.lua/usr/share/sysbench/oltp_read_only.lua/usr/share/sysbench/oltp_read_write.lua/usr/share/sysbench/oltp_update_index.lua/usr/share/sysbench/oltp_update_non_index.lua/usr/share/sysbench/oltp_write_only.lua/usr/share/sysbench/select_random_points.lua/usr/share/sysbench/select_random_ranges.lua/usr/share/sysbench/tests/include/inspect.lua/usr/share/sysbench/tests/include/oltp_legacy/bulk_insert.lua/usr/share/sysbench/tests/include/oltp_legacy/common.lua/usr/share/sysbench/tests/include/oltp_legacy/delete.lua/usr/share/sysbench/tests/include/oltp_legacy/insert.lua/usr/share/sysbench/tests/include/oltp_legacy/oltp.lua/usr/share/sysbench/tests/include/oltp_legacy/oltp_simple.lua/usr/share/sysbench/tests/include/oltp_legacy/parallel_prepare.lua/usr/share/sysbench/tests/include/oltp_legacy/select.lua/usr/share/sysbench/tests/include/oltp_legacy/select_random_points.lua/usr/share/sysbench/tests/include/oltp_legacy/select_random_ranges.lua/usr/share/sysbench/tests/include/oltp_legacy/update_index.lua/usr/share/sysbench/tests/include/oltp_legacy/update_non_index.lua

本文介绍的是新版本 sysbench oltp lua脚本的用法(/usr/share/sysbench/*.lua),所以不涉及传统的lua(tests/include/oltp_legacy/*.lua),如果想要了解这些传统Lua脚本的用法,网上随便找。更多关于MySQL学习的文章,请参阅:死磕数据库系列之 MySQL ,本系列持续更新中。

sysbench 使用方法

以下列出了sysbench对测试mysql时常用的选项。

[root@xuexi~]#sysbench--helpUsage:sysbench[options]...[test_lua][lua_options][command]Commandsimplementedbymosttests:prepareruncleanuphelp

通用选项:以下中括号中的值表示默认值

--threads=N#指定线程数[1]--events=N#限制最大请求数,0表示不限制[0]--time=N#限制最长执行时间,0表示不限制[10]#--events和--time二者选一即可--forced-shutdown=STRING#达到最长执行时间后还需等待多久关闭sysbenchoff表示禁用该功能[off]--thread-stack-size=SIZE#每个线程使用的栈空间大小[64K]--rate=N#平均事务处理速率,0表示不限制[0]--report-interval=N#每隔几秒报告一次结果,0表示禁用间隔报告[0]--config-file=FILENAME#从文件中读取命令行选项--tx-rate=N#已废弃,是--rate的别名[0]--max-requests=N#已废弃,是--events的别名[0]--max-time=N#已废弃,是--time的别名[0]--num-threads=N#已废弃,是--threads的别名[1]--db-ps-mode=STRING#是否使用prepare模式的语句{auto,disable}[auto]

mysql相关选项:

--mysql-host=[LIST,...]MySQLserverhost[localhost]--mysql-port=[LIST,...]MySQLserverport[3306]--mysql-socket=[LIST,...]MySQLsocket--mysql-user=STRINGMySQLuser[sbtest]--mysql-password=STRINGMySQLpassword[]--mysql-db=STRINGMySQLdatabasename[sbtest]--mysql-ignore-errors=[LIST,...]要忽略的错误代码,值可为"all"[1213,1020,1205]Compiled-intests:fileio-FileI/Otestcpu-CPUperformancetestmemory-Memoryfunctionsspeedtestthreads-Threadssubsystemperformancetestmutex-Mutexperformancetest

其中,command部分有4类:prepare run cleanup和help:

prepare:#准备数据的命令。例如,在sysbench压力测试之前,需要先准备好测试库、测试表以及测试表中的数据。具体用法见后文。run:#表示进行压力测试。cleanup:#清除测试时产生的数据。help:#输出给定lua脚本的帮助信息。

test_lua是想要使用的lua脚本,如果是rpm包安装的sysbench,则这些脚本都是/usr/share/sysbench目录下。对于一般的数据库测试,只需使用和oltp有关的lua脚本就足够。更多关于MySQL学习的文章,请参阅:死磕数据库系列之 MySQL,本系列持续更新中。

options和lua_options是不同的,options是sysbench的选项,lua_options是lua脚本的选项,lua_options应该要放在test_lua的后面(非必需,但建议)。

例如,要查看oltp_common.lua的用法,可以:

sysbench/usr/share/sysbench/oltp_common.luahelp

准备测试数据

首先创建sysbench所需数据库sbtest(这是sysbench默认使用的库名,必须创建测试库)。

mysqladmin-h127.0.0.1-uroot-pP@ssword1!-P3306createsbtest;

然后,准备测试所用的表,这些测试表放在测试库sbtest中。这里使用的lua脚本为/usr/share/sysbench/oltp_common.lua

sysbench--mysql-host=127.0.0.1\--mysql-port=3306\--mysql-user=root\--mysql-password=P@ssword1!\/usr/share/sysbench/oltp_common.lua\--tables=10\--table_size=100000\prepare

其中--tables=10表示创建10个测试表,--table_size=100000表示每个表中插入10W行数据,prepare表示这是准备数的过程。

mysql>showtablesfromsbtest;+------------------+|Tables_in_sbtest|+------------------+|sbtest1||sbtest10||sbtest2||sbtest3||sbtest4||sbtest5||sbtest6||sbtest7||sbtest8||sbtest9|+------------------+

mysql>selectcount(*)fromsbtest.sbtest1;+----------+|count(*)|+----------+|100000|+----------+

如果想要清除这10个表,可使用cleanup命令。

sysbench--mysql-host=127.0.0.1\--mysql-port=3306\--mysql-user=root\--mysql-password=P@ssword1!\/usr/share/sysbench/oltp_common.lua\--tables=10\cleanup

数据库测试和结果分析

稍微修改下之前准备数据的语句,就可以拿来测试了。

需要注意的是,之前使用的lua脚本为oltp_common.lua,它是一个通用脚本,是被其它lua脚本调用的,它不能直接拿来测试。

所以,我这里用oltp_read_write.lua脚本来做读、写测试。还有很多其它类型的测试,比如只读测试、只写测试、删除测试、大批量插入测试等等。可找到对应的lua脚本进行调用即可。更多关于MySQL学习的文章,请参阅:死磕数据库系列之 MySQL,本系列持续更新中。

sysbench--threads=4\--time=20\--report-interval=5\--mysql-host=127.0.0.1\--mysql-port=3306\--mysql-user=root\--mysql-password=P@ssword1!\/usr/share/sysbench/oltp_read_write.lua\--tables=10\--table_size=100000\run

以下是测试返回的结果:

Initializingworkerthreads...Threadsstarted!####以下是每5秒返回一次的结果,统计的指标包括:####线程数、tps(每秒事务数)、qps(每秒查询数)、####每秒的读/写/其它次数、延迟、每秒错误数、每秒重连次数[5s]thds:4tps:130.16qps:2606.30(r/w/o:1824.51/520.66/261.13)lat(ms,95%):104.84err/s:0.00reconn/s:0.00[10s]thds:4tps:126.74qps:2539.17(r/w/o:1778.17/507.52/253.47)lat(ms,95%):108.68err/s:0.00reconn/s:0.00[15s]thds:4tps:136.54qps:2736.34(r/w/o:1915.25/548.01/273.07)lat(ms,95%):102.97err/s:0.00reconn/s:0.00[20s]thds:4tps:107.44qps:2148.65(r/w/o:1505.60/428.17/214.89)lat(ms,95%):132.49err/s:0.00reconn/s:0.00SQLstatistics:queriesperformed:read:35098#执行的读操作数量write:10028#执行的写操作数量other:5014#执行的其它操作数量total:50140transactions:2507(124.29persec.)#执行事务的平均速率queries:50140(2485.82persec.)#平均每秒能执行多少次查询ignorederrors:0(0.00persec.)reconnects:0(0.00persec.)Generalstatistics:totaltime:20.1694s#总消耗时间totalnumberofevents:2507#总请求数量(读、写、其它)Latency(ms):min:2.32avg:32.13max:575.7895thpercentile:118.92#采样计算的平均延迟sum:80554.96Threadsfairness:events(avg/stddev):626.7500/2.49executiontime(avg/stddev):20.1387/0.04

cpu/io/内存等测试

sysbench内置了几个测试指标。

Compiled-intests:fileio-FileI/Otestcpu-CPUperformancetestmemory-Memoryfunctionsspeedtestthreads-Threadssubsystemperformancetestmutex-Mutexperformancetest

可直接help输出测试方法。例如,fileio测试。

[root@xuexi~]#sysbenchfileiohelpsysbench1.0.15(usingbundledLuaJIT2.1.0-beta2)fileiooptions:--file-num=Nnumberoffilestocreate[128]--file-block-size=NblocksizetouseinallIOoperations[16384]--file-total-size=SIZEtotalsizeoffilestocreate[2G]--file-test-mode=STRINGtestmode{seqwr,seqrewr,seqrd,rndrd,rndwr,rndrw}--file-io-mode=STRINGfileoperationsmode{sync,async,mmap}[sync]--file-async-backlog=Nnumberofasynchronousoperatonstoqueueperthread[128]--file-extra-flags=[LIST,...]listofadditionalflagstousetoopenfiles{sync,dsync,direct}[]--file-fsync-freq=Ndofsync()afterthisnumberofrequests(0-don'tusefsync())[100]--file-fsync-all[=on|off]dofsync()aftereachwriteoperation[off]--file-fsync-end[=on|off]dofsync()attheendoftest[on]--file-fsync-mode=STRINGwhichmethodtouseforsynchronization{fsync,fdatasync}[fsync]--file-merged-requests=NmergeatmostthisnumberofIOrequestsifpossible(0-don'tmerge)[0]--file-rw-ratio=Nreads/writesratioforcombinedtest[1.5]

例如,创建5个文件,总共1G,每个文件大概200M。

sysbenchfileio--file-num=5--file-total-size=1Gprepare[root@xuexi~]#ls-lhtest*-rw-------1rootroot205MJul812:15test_file.0-rw-------1rootroot205MJul812:15test_file.1-rw-------1rootroot205MJul812:15test_file.2-rw-------1rootroot205MJul812:15test_file.3-rw-------1rootroot205MJul812:15test_file.4

然后,运行测试。

sysbench--events=5000\--threads=16\fileio\--file-num=5\--file-total-size=1G\--file-test-mode=rndrw\--file-fsync-freq=0\--file-block-size=16384\run

结果:

Fileoperations:reads/s:98.67writes/s:66.85fsyncs/s:6.26Throughput:#吞吐量read,MiB/s:1.54#表示读的带宽written,MiB/s:1.04#表示读的带宽Generalstatistics:totaltime:12.7426stotalnumberofevents:2117Latency(ms):min:0.00avg:86.66max:2919.4195thpercentile:646.19sum:183460.80Threadsfairness:events(avg/stddev):132.3125/24.19executiontime(avg/stddev):11.4663/1.09

再比例cpu性能测试:

[root@xuexi~]#sysbenchcpu--threads=40--events=10000--cpu-max-prime=20000runsysbench1.0.15(usingbundledLuaJIT2.1.0-beta2)Runningthetestwithfollowingoptions:Numberofthreads:40InitializingrandomnumbergeneratorfromcurrenttimePrimenumberslimit:20000Initializingworkerthreads...Threadsstarted!CPUspeed:eventspersecond:2127.81Generalstatistics:totaltime:4.6986stotalnumberofevents:10000Latency(ms):min:1.72avg:18.16max:302.1795thpercentile:110.66sum:181628.49Threadsfairness:events(avg/stddev):250.0000/30.81executiontime(avg/stddev):4.5407/0.10

出处:/f-ck-need-u/

p/9279703.html

邀你加入技术交流群, 我们一起卷!

推荐阅读点击标题可跳转

文心一言的魔性作图,我愣住了。。。

TCP连接数最大不能超过65535?那服务器是如何应对百万千万并发的?

root 与 sudo 的用法与区别,居然这么多人搞不清楚

ChatGPT 团队阵容揭秘!太强了。。。

Docker 翻脸,不再开源!

戴尔 PC 将完全退出中国!完整时间表曝光

看了这篇文章!我才知道mv命令真正使用方法

PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下在看,加个星标,这样每次新文章推送才会第一时间出现在你的订阅列表里。点在看支持我们吧!

如果觉得《死磕数据库系列(三十四):MySQL 性能测试工具 sysbench 详解》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。