当前位置: 首页 > 科技观察

简单易用的官方MySQL压测工具,推荐收藏!

时间:2023-03-23 11:51:08 科技观察

1.MySQL自带的压力测试工具MysqlslapMysqlslap是mysql自带的一款基准测试工具。该工具查询数据语法简单,灵活易用。该工具可以模拟多个客户端并发向服务端发送查询更新,给出性能测试数据,并提供各种引擎的性能对比。mysqlslap为mysql性能优化前后提供了直观的验证依据。系统运维和DBA人员要掌握一些常用的压测工具,才能准确把握在线数据库支持的用户流量上限及其抗压能力。1.更改默认的最大连接数在对MySQL进行压力测试之前,需要更改默认的最大连接数,如下:[root@mysql~]#vim/etc/my.cnf...............[mysqld]max_connections=1024[root@mysql~]#systemctlrestartmysqld查看最大连接数mysql>showvariableslike'max_connections';+-----------------+--------+|Variable_name|Value|+----------------+------+|max_connections|1024|+----------------+--------+1rowinset(0.00sec)用于压力测试:[root@mysql~]#mysqlslap--defaults-file=/etc/my.cnf--concurrency=100,200--iterations=1--number-int-cols=20--number-char-cols=30\--auto-generate-sql--auto-generate-sql-add-autoincrement--auto-generate-sql-load-type=mixed--engine=myisam,innodb--number-of-queries=2000-uroot-p123--verbose以上命令的测试说明:Simulate两次并发读写,第一次100,第二次200,SQL脚本自动生成,测试表包含ns20个init字段,30个char字段,每次执行2000个查询请求。测试引擎是myisam和innodb。(上面很多选项都是默认值,可以省略,如果想知道每个选项的解释,可以使用mysqlslap--help查询)。测试结果说明:Myisam第一次100个客户端同时发起附加查询0.557/s,第二次200个客户端同时发起附加查询0.522/sInnodb第一次100个客户端同时发起附加查询0.256/s,第二次200客户端同时以0.303/s的速度启动额外的检查。根据实际需要,可以一点点增加并发数进行压力测试。二、使用第三方sysbench工具进行压力测试1、安装sysbench工具[root@mysql~]#yum-yinstallepel-release#安装第三方epel源[root@mysql~]#yum-yinstallsysbench#安装sysbench工具[root@mysql~]#sysbench--version#确认工具已经安装。测试需要通过/usr/share/sysbench/目录下的Lua脚本执行,例如oltp_read_only.lua脚本执行只读测试)。sysbench还可以通过在运行命令时指定您自己的Lua脚本来自定义测试。2、查看sysbench工具的帮助选项[root@mysql~]#sysbench--helpUsage:sysbench[options]...[testname][command]Commandsimplementedbymosttests:prepareruncleanuphelp#Availablecommands,四个Generaloptions:#Generaloptions--threads=N要使用的线程数,默认1[1]--events=N允许的最大事件数[0]--time=N最大总执行时间,以秒为单位[10]--forced-shutdown=STRINGNumber在达到--time限制后强制关闭之前等待的秒数,默认'off'禁用(numberofsecondstowaitafterthe--timelimitbeforecingshutdown,或'off'todisable)[off]--thread-stack-size=SIZE每个堆栈大小thread[64K]--rate=N平均传输速率。0表示无限制[0]--report-interval=N以秒为单位定期报告具有指定间隔的中间统计信息0禁用中间报告[0]--report-checkpoints=[LIST,...]转储完整统计信息并重置所有计数器指定的时间点。该参数是一个逗号分隔的值列表,表示在从测试开始经过这段时间后必须执行报告检查点(以秒为单位)。报告检查点默认关闭。[]--debug[=on|off]打印更多调试信息[off]--validate[=on|off]尽可能进行验证检查[off]--help[=on|off]显示帮助信息和exit[off]--version[=on|off]显示版本信息并退出[off]--config-file=FILENAME包含命令行选项的文件--tx-rate=N已过时,改用--rate[0]--max-requests=N过时,使用--events[0]--max-time=N过时,使用--time[0]--num-threads=N过时,使用--threads[1]伪-RandomNumbersGeneratoroptions:#伪随机数生成器选项--rand-type=STRINGrandomnumbersdistribution{uniform,gaussian,special,pareto}[special]--rand-spec-iter=Nnumberofiterationsusedfornumbersgeneration[12]--rand-spec-pct=Npercentageofvaluestobetreatedas'special'(forspecialdistribution)[1]--rand-spec-res=Npercentageof'special'valuestouse(forspecialdistribution)[75]--rand-seed=Nseedforrandomnumberbergenerator.When0,thecurrenttimeisusedasaRNGseed.[0]--rand-pareto-h=N参数hforparetodistribution[0.2]Logoptions:#logoptions--verbosity=Nverbositylevel{5-debug,0-onlycriticalmessages}[3]--percentile=Npercentiletocalculateinlatencystatistics(1-100).Usethespecialvalueof0禁用percentilecalculations[95]--histogram[=on|off]printlatencyhistograminreport[off]Generaldatabaseoptions:#Generaldatabaseoptions--db-driver=STRING指定要使用的数据库驱动程序('help'togetlistofavailabledrivers)--db-ps-mode=STRINGpreparedstatementsusagemode{auto,disable}[auto]--db-debug[=on|off]printdatabase-specificdebuginformation[off]Compiled-indatabaseddrivers:#内置数据库驱动,支持MySQL和PostgreSQLmysql-MySQLdriverpgsql-PostgreSQLdrivermysqloptions:#MySQLDatabase-specific选项--mysql-host=[LIST,...]MySQLserverhost[localhost]--mysql-port=[LIST,...]MySQLserverport[3306]--mysql-socket=[LIST,...]MySQLsocket--mysql-user=STRINGMySQL用户[sbtest]--mysql-password=STRINGMySQL密码[]--mysql-db=STRINGMySQL数据库名称[sbtest]--mysql-ssl[=on|off]使用SSL连接,如果在客户端库中可用[off]--mysql-ssl-cipher=STRING使用特定密码进行SSL连接[]--mysql-compression[=on|off]使用压缩,如果在客户端库中可用[off]--mysql-debug[=on|off]traceallclientlibrarycalls[off]--mysql-ignore-errors=[LIST,...]错误列表oignore,or"all"[1213,1020,1205]--mysql-dry-run[=on|off]Dryrun,假装所有MySQLclientAPI调用都成功而不执行它们[off]pgsqloptions:#PostgreSQLdatabase-specificoptions--pgsql-host=STRINGPostgreSQLserverhost[localhost]--pgsql-port=NPostgreSQLserverport[5432]--pgsql-user=STRINGPostgreSQLuser[sbtest]--pgsql-password=STRINGPostgreSQLpassword[]--pgsql-db=STRINGPostgreSQLdatabasename[sbtest]编译测试:#内置测试类型fileio-FileI/Otestcpu-CPUperformancetestmemory-Memoryfunctionsspeedtestthreads-Threadssubsystemperformancetestmutex-Muteperformancetest参见'sysbenchhelp'foralistofoptionsforeachtest.3、sysbench测试MySQL数据库性能1)准备测试数据#查看自带的lua脚本使用方法sysbench[root@mysql~]#sysbench/usr/share/sysbench/oltp_common.luahelp#必须创建sbtest库,sbtest是sysbench默认使用的库名[root@mysql~]#mysqladmin-uroot-p123createsbtest;#然后,准备使用的表为了测试,把这些测试表放在测试库sbtest中这里用到的lua脚本是/usr/share/sysbench/oltp_common.lua。[root@mysql~]#sysbench--mysql-host=127.0.0.1\--mysql-port=3306\--mysql-user=root\--mysql-password=123\/usr/share/sysbench/oltp_common.lua\--tables=10\--table_size=100000\prepare#其中--tables=10表示创建10张测试表,#--table_size=100000表示每张表插入10W行数据,#prepare意思是这是准备数字的过程。2)确认测试数据存在[root@mysql~]#mysql-uroot-p123sbtest;#登录sbtest库mysql>showtables;#查看对应表+------------------+|Tables_in_sbtest|+----------------+|sbtest1||sbtest10||sbtest2||sbtest3||sbtest4||sbtest5||sbtest6||sbtest7||sbtest8||sbtest9|+----------------+10rowsinset(0.00sec)mysql>selectcount(*)fromsbtest1;#随机选择一个表并确认有100000个Bar数据+--------+|count(*)|+----------+|100000|+----------+1rowinset(0.01sec)3)数据库测试及结果分析将之前准备数据的语句稍作修改,即可用于测试。需要注意的是,之前使用的lua脚本是oltp_common.lua,这是一个普通脚本,会被其他lua脚本调用,所以不能直接用于测试。所以,这里我使用oltp_read_write.lua脚本来做读写测试。还有很多其他类型的测试,比如只读测试、只写测试、删除测试、批量插入测试等等。可以找到对应的lua脚本并调用。#执行测试命令如下:[root@mysql~]#sysbench--threads=4\--time=20\--report-interval=5\--mysql-host=127.0.0.1\--mysql-port=3306\--mysql-user=root\--mysql-password=123\/usr/share/sysbench/oltp_read_write.lua\--tables=10\--table_size=100000\run以上返回的结果命令如下:root@mysql~]#sysbench--threads=4--time=20--report-interval=5--mysql-host=127.0.0.1--mysql-port=3306--mysql-user=root--mysql-password=123/usr/share/sysbench/oltp_read_write.lua--tables=10--table_size=100000runsysbench1.0.17(usingsystemLuaJIT2.0.4)Runningthetestwithfollowingoptions:Numberofthreads:4Reportintermediateresultsevery5second(s)InitializingThreadizingrandomnumbergeneratorfromcurrenttimeInit!#以下是每5次每秒返回一次结果,统计指标包括:#threads、tps(每秒事务数)、qps(每秒查询数)、#read/write/othertimespersecond、delay、errorsper第二,每次重新连接的次数秒[5s]thds:4tps:1040.21qps:20815.65(r/w/o:14573.17/4161.25/2081.22)lat(ms,95%):7.17err/s:0.00reconn/s:0.00[10s]thds:4tps:1083.34qps:21667.15(r/w/o:15165.93/4334.55/2166.68)lat(ms,95%):6.55err/s:0.00reconn/s:0.00[15s]thds:4tps:1121.57qps:22429.09(r/w/o:15700.64/4485.30/2243.15)lat(ms,95%):6.55err/s:0.00reconn/s:0.00[20s]thds:4tps:1141.69qps:22831.98(r/w/o:15982.65/4566.16/2283.18)lat(ms,95%):6.09err/s:0.00reconn/s:0.00SQLstatistics:queriesperformed:read:307146#执行的读操作写:87756#执行的写操作数other:43878#执行的其他操作数total:438780transactions:21939(1096.57persec.)#事务查询的平均执行率:438780(21931.37persec.)#可以执行多少查询每秒平均ignoreerrors:0(0.00persec.)reconnects:0(0.00persec.)Generalstatistics:totaltime:20.0055s#Totalconsumptiontimetotalnumberofevents:21939#Totalnumbersofrequests(read,write,others)Latency(ms):min:1.39avg:3.64max:192.0595thpercentile:6.67#采样计算的平均延迟sum:79964.26Threadsfairness:events(avg/stddev):5484.7500/15.12executiontime(avg/stddev):19.9911/0.004,cpu/io/memory等几个内置的ysbench测试测试指标如下:[root@mysql~]#sysbench--help............#省略部分内容Compiled-intests:fileio-FileI/Otestcpu-CPUperformancetestmemory-Memoryfunctionsspeedtestthreads-Threadssubsystemperformancetestmutex-Mutexperformancetest可以直接帮助输出测试方法,例如fileio测试:[root@mysql~]#sysbenchfileiohelpsysbench1.0.17(usingsystemLuaJIT2.0.4)fileiooptions:--file-num=Nnumberofffilestocreate[128]--file-block-size=NblocksizetouseinallIOoperations[16384]--file-total-size=SIZEtotalsizeofffilestocreate[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]1)测试io性能例如,创建5个文件,共2G,每个文件概要400M[root@mysql~]#sysbenchfileio--file-num=5--file-total-size=2Gprepare[root@mysql~]#ll-lhtest*-rw------1rootroot410MMay2616:05test_file.0-rw--------1rootroot410MMay2616:05test_file.1-rw-------1rootroot410MMay2616:05test_file.2-rw-------1rootroot410MMay2616:05test_file.3-rw-------1rootroot410MMay2616:05test_file.4然后运行测试:[root@mysql~]#sysbench--events=5000\--threads=16\fileio\--file-num=5\--file-total-size=2G\--file-test-mode=rndrw\--file-fsync-freq=0\--file-block-size=16384\run返回的结果如下:Runningthetestwithfollowingoptions:Numberofthreads:16InitializingrandomnumbergeneratorfromcurrenttimeExtrafileopenflags:(none)5files,409.6MiBeach2GiBtotalfilesizeBlocks:50requestBlocksizeBlock50RequestOf16Ki/WriteratioforcombinedrandomIOtest:1.50Callingfsync()attheendoftest,Enabled.UsingsynchronousI/OmodeDoingrandomr/wtestInitializingworkerthreads...Threadsstarted!Fileoperations:reads/s:9899.03writes/s:6621.38fsyncs/s:264.33Throughput:#吞6吞./s读1,MiB6/s#表示阅读带宽written,MiB/s:103.46#表示写入带宽Generalstatistics:totaltime:0.3014stotalnumberofevents:5000Latency(ms):min:0.00avg:0.81max:53.5695thpercentile:4.10sum:4030.48Threadsfairness:events(avg/stddev):312.5000/tion7(avgexecution/stddev):0.2519/0.022)测试cpu性能[root@mysql~]#sysbenchcpu--threads=40--events=10000--cpu-max-prime=20000跑压测的时候注意!!!!