我们需要知道的是sysbench并不是一个压力测试工具,是一个基准测试工具。linux自带的版本比较低,我们需要自己安装sysbench。
[root@test2 ~]# sysbench --version sysbench 0.4.12
安装sysbench,sysbench的源码托管在GitHub上,下载源码:
unzip sysbench-master.zip #解压源码yum -y install make automake libtool pkgconfig libaio-devel #下载依赖包 cd sysbench-mastersh autogen.sh编译: ./configure --with-mysql-includes=/usr/local/mysql/include --with-mysql-libs=/usr/local/mysql/lib #根据安装的MySQL的位置,设置目录位置makemake install这样安装之后使用sysbench命令时会报错。 [root@test3 sysbench-master]# sysbench --version sysbench: error while loading shared libraries: libmysqlclient.so.20: cannot open shared object file: No such file or directory 解决办法: 在/etc/profile文件中加入一行: export LD_LIBRARY_PATH=/usr/local/mysql/lib source /etc/profile 命令可以正常使用 [root@test3 sysbench-master]# sysbench --version sysbench 1.1.0
查看sysbench的一些帮助信息:
[root@test3 ~]# sysbench ----threads=N number of threads to use [--events=N limit total number of events [--=N limit total execution seconds [--warmup-=N #在进行基准测试多少秒之后启用统计信息--forced-shutdown=STRING --thread-stack-size=--thread-init-timeout=N seconds worker threads to initialize [--rate=N average transactions rate. unlimited rate [--report-interval=N #打印出中间的信念,N表示每隔N秒打印一次,0表示禁用--report-checkpoints=[LIST,...] #转储完全统计信息并在指定时间点复位所有计数器,参数是逗号分隔值的列表,表示从必须执行报告检查点的测试开始所经过的时间(以秒为单位)。 默认情况下,报告检查点处于关闭状态[off]。--debug[=on|off] print debugging --validate[=on|--help[=on|--version[=on|--config-=--luajit-cmd=STRING perform LuaJIT control command. This option is equivalent to . See LuaJIT documentation information #上面是一些通用的配置信息,在具体测试某个测试时,会再详细说明参数设置
首先来进行IO测试
[root@test3 ~]# sysbench fileio help #查看IO测试的文档 sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3) fileio options: --file-num=N number of files to create [128] #文件的数量 --file-block-size=N block size to use in all IO operations [16384] #文件块的大小,如果要是针对INNODB的测试,可以设置为innodb_page_size的大小 --file-total-size=SIZE total size of files to create [2G] #文件的总大小 --file-test-mode=STRING test mode {seqwr【顺序写】, seqrewr【顺序读写】, seqrd【顺序读】, rndrd【随机读】, rndwr【随机写】, rndrw【随机读写】} #文件测试模式 --file-io-mode=STRING file operations mode {sync【同步】,async【异步】,mmap【map映射】} [默认为:sync] #文件的io模式 --file-async-backlog=N number of asynchronous operatons to queue per thread [128] #打开文件时的选项,这是与API相关的参数。 --file-extra-flags=[LIST,...] #打开文件时的选项,这是与API相关的参数。可选有sync,dsync,direct。--file-fsync-freq=N #执行fsync函数的频率,fsync主要是同步磁盘文件,因为可能有系统和磁盘缓冲的关系。默认为100,如果为0表示不使用fsync。 --file-fsync-all[=on|off] #每执行完一次写操作,就执行一次fsync,默认未off。--file-fsync-end[=on|off] #在测试结束时,执行fsync,默认为on。--file-fsync-mode=STRING #文件同步函数的选择,同样是和API相关的参数,由于多个操作对fdatasync支持的不同,因此不建议使用fdatasync。默认为fsync。--file-merged-requests=N #尽可能合并此数量的io请求(0-不合并),默认为[0]。 --file-rw-ratio=N #测试时的读写比例,默认是2:1。
在使用sysbench进行测试的时候,通常分为三个步骤prepare,run,cleanup阶段。
第一步准备数据(prepare阶段):
[root@test3 systext]# sysbench fileio ---num= ---total-size=. (using bundled LuaJIT .- test_file. test_file. test_file. test_file. test_file. test_file. test_file. test_file. test_file. test_file. bytes written seconds ( MiB/sec). #这里给出一个每秒写入的数据量104.59MB/s, 这里的写入是顺序写入的,表示磁盘的吞吐量为104.59MB/s。 【】 [root@test3 systext]# ll -h #文件大小为5个G total 50G -rw------- 1 root root 5.0G Nov 27 09:30 test_file.0 -rw------- 1 root root 5.0G Nov 27 09:31 test_file.1 -rw------- 1 root root 5.0G Nov 27 09:32 test_file.2 -rw------- 1 root root 5.0G Nov 27 09:32 test_file.3 -rw------- 1 root root 5.0G Nov 27 09:33 test_file.4 -rw------- 1 root root 5.0G Nov 27 09:34 test_file.5 -rw------- 1 root root 5.0G Nov 27 09:35 test_file.6 -rw------- 1 root root 5.0G Nov 27 09:36 test_file.7 -rw------- 1 root root 5.0G Nov 27 09:36 test_file.8 -rw------- 1 root root 5.0G Nov 27 09:37 test_file.9
数据准备好之后,进行测试:
#这里进行测试 [root@test3 systext]# sysbench fileio ---num= ---total-size=50G ---block-size= ---test-mode=rndrw ---io-mode= ---extra-flags=direct --= --threads= --report-interval=. (using bundled LuaJIT .-/Write ratio combined random IO test: //! MiB/s writes: MiB/s fsyncs: /s latency (ms,%): MiB/s writes: MiB/s fsyncs: /s latency (ms,%): MiB/s writes: MiB/s fsyncs: /s latency (ms,%): MiB/s writes: MiB/s fsyncs: /s latency (ms,%): MiB/s writes: MiB/s fsyncs: /s latency (ms,%): MiB/s writes: MiB/s fsyncs: /s latency (ms,%): MiB/s writes: MiB/s fsyncs: /s latency (ms,%): MiB/s writes: MiB/s fsyncs: /s latency (ms,%): MiB/s writes: MiB/s fsyncs: /s latency (ms,%): MiB/s writes: MiB/s fsyncs: /s latency (ms,%): = MiB/s ( MB/: IOPS= MiB/s ( MB/= :
[root@test3 systext]# sysbench fileio --file-num=10 --file-total-size=50G --file-block-size=16384 --file-test-mode=seqrd --file-io-mode=sync --file-extra-flags=direct --time=100 --threads=16 --report-interval=10 run sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3) Running the test with following options: Number of threads: 16Report intermediate results every 10 second(s) Initializing random number generator from current timeExtra file open flags: directio10 files, 5GiB each 50GiB total file size Block size 16KiB Periodic FSYNC enabled, calling fsync() each 100 requests. Calling fsync() at the end of test, Enabled. Using synchronous I/O mode Doing sequential read test Initializing worker threads... Threads started![ 10s ] reads: 98.88 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.020[ 20s ] reads: 98.64 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.681[ 30s ] reads: 93.24 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 2.913[ 40s ] reads: 89.12 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 4.028[ 50s ] reads: 93.17 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 4.487[ 60s ] reads: 91.98 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 4.652[ 70s ] reads: 97.08 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.425[ 80s ] reads: 93.71 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.020[ 90s ] reads: 94.63 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.304[ 100s ] reads: 89.57 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.364Throughput: read: IOPS=6016.01 94.00 MiB/s (98.57 MB/s) write: IOPS=0.00 0.00 MiB/s (0.00 MB/s) fsync: IOPS=0.00Latency (ms): min: 0.40 avg: 2.66 max: 687.00 95th percentile: 3.62 sum: 1599247.42#测试结果可以看到顺序的读和随机读的差距还是超大的
顺序读的测试
可以更改--file-test-mode的模式,改变测试的模式。
测试阶段完成之后,需要进行最后的cleanup阶段,
[root@test3 systext]# sysbench fileio --file-num=10 --file-total-size=50 cleanup sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3) Removing test files... [root@test3 systext]# ls[root@test3 systext]# df -h Filesystem Size Used Avail Use% Mounted on/dev/vda3 29G 8.4G 20G 31% /tmpfs 3.9G 44K 3.9G 1% /dev/shm/dev/vda1 190M 30M 151M 17% /boot/dev/vdb 100G 25G 76G 25% /data cgroup_root 3.9G 0 3.9G 0% /cgroup #看到磁盘空间已经释放
测试MySQL的OLTP
sysbench新版的用法和之前的旧版本有所不同,先来看测试数据库时的一些参数:
General database options: --db-driver=STRING specifies database driver to use ('help' to get list of available drivers) [mysql] #指定数据库驱动,默认是mysql --db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto] # --db-debug[=on|off] print database-specific debug information [off] #dubug模式 Compiled-in database drivers: mysql - MySQL driver mysql options: --mysql-host=[LIST,...] MySQL server host [localhost] --mysql-port=[LIST,...] MySQL server port [3306] --mysql-socket=[LIST,...] MySQL socket --mysql-user=STRING MySQL user [sbtest] --mysql-password=STRING MySQL password [] --mysql-db=STRING MySQL database name [sbtest] #数据库名字,默认是sbtest --mysql-ssl[=on|off] use SSL connections, if available in the client library [off] #以下是ssl的连接测试 --mysql-ssl-key=STRING path name of the client private key file --mysql-ssl-ca=STRING path name of the CA file --mysql-ssl-cert=STRING path name of the client public key certificate file --mysql-ssl-cipher=STRING use specific cipher for SSL connections [] --mysql-compression[=on|off] use compression, if available in the client library [off] #压缩测试 --mysql-debug[=on|off] trace all client library calls [off] --mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205] #忽略的错误 --mysql-dry-run[=on|off] Dry run, pretend that all MySQL client API calls are successful without executing them [off]
MySQL测试的lua脚本:
/data/sysbench-master/src/--
prepare阶段:
创建默认的测试库:
mysql> row affected (/data/sysbench-master/src/lua/oltp_read_write.lua --tables= --table_size= --mysql-user=root --mysql-password= --mysql-host=. --mysql-port= --mysql-db=. (using bundled LuaJIT .- records into records into records into ... #在MySQL shel1中查看数据 mysql> select count(*) from sbtest1; +----------+ | count(*) | +----------+ | 10000000 | +----------+ 1 row in set (1.89 sec) mysql> show tables; +------------------+ | Tables_in_sbtest | +------------------+ | sbtest1 | | sbtest2 | | sbtest3 | +------------------+ 3 rows in set (0.00 sec)
run阶段
选择一个合适的lua脚本进行测试:
[root@test3 lua]# sysbench /data/sysbench-master/src/lua/oltp_point_select.lua --tables= --table_size= --mysql-user=root --mysql-password= --mysql-host=. --mysql-port= --mysql-db=sbtest --threads= --= --report-interval=. (using bundled LuaJIT .-! tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: tps: qps: (r//o: //) lat (ms,%): err/s: reconn/s: : ( ( ( (/s (eps): elapsed: : /stddev): / (avg/stddev): /
我们一般关注的指标主要有:
- response time avg:平均响应时间(后面的95%的大小可以通过–percentile=98的方式去更改)。
- transactions:精确的说是这一项后面的TPS,但如果使用了–skip-trx=on,这项事务数为0,需要用total number of events去除以总时间,得到tps(其实还可以分为读tps和写tps)。
- queries:用它除以总时间,得到吞吐量QPS。
因为上面的TPS与QPS是一样的,因此只绘了TPS的图,如下:
刚开始的时候有一个明显的上升,这时候是因为在bp中没有缓存数据,需要从磁盘中读数据,也就是预热阶段!
清理数据
[root@test3 lua]# sysbench /data/sysbench-master/src/lua/oltp_read_write.lua --tables=3 --table_size=10000000 --mysql-user=root --mysql-password=123456 --mysql-host=10.0.102.214 --mysql-port=3306 --mysql-db=sbtest cleanup sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3) Dropping table 'sbtest1'... Dropping table 'sbtest2'... Dropping table 'sbtest3'... [root@test3 lua]#
sysbench除了以上的测试之外,还可以测试:
Compiled-in tests: fileio - File I/O test cpu - CPU performance test memory - Memory functions speed test threads - Threads subsystem performance test mutex - Mutex performance test See 'sysbenchhelp' for a list of options for each test