看一些书上介绍说,sysbench(https://launchpad.net/sysbench/)是MySQL DBA做基准测试至少应该掌握的工具。MySQL安装自带的mysqlslap(http://blog.csdn.net/sweeper_freedoman/article/details/73556910)和曾经自带Benchmark Suit(http://blog.csdn.net/sweeper_freedoman/article/details/73557834),功能都只是局限于数据库层级的测试。sysbench则是基于数据库运行的整个操作系统级别的模块化的跨平台多线程基准测试工具,涵盖:
- file I/O performance
- scheduler performance
- memory allocation and transfer speed
- POSIX threads implementation performance
- database server performance (OLTP benchmark)
其中磁盘I/O测试和数据库OLTP测试是和MySQL以及InnoDB存储引擎最相关的测试指标。
sysbench安装可以去github(https://github.com/akopytov/sysbench)上下载源码包安装。Ubuntu Linux有apt源支持,可以敲命令直接安装。
root@ubuntu:~# apt-cache search sysbench sysbench - Cross-platform and multi-threaded benchmark tool root@ubuntu:~# apt-get install sysbench
安装完成后可以“ sysbench --help ”看看使用方法。以下是一些常用选项。
- --test指定执行何种测试(列出在了“Compiled-in tests”部分),如本次比较关心的“fileio”和“oltp”。
- --num-threads线程使用数,默认1。
- --max-requests最大请求数限制,默认1w。
- --max-time最大运行时间限制(秒),默认无限制。
- --init-rng初始化随机数生成器开关,默认关闭。
先看看I/O测试组件吧,“ sysbench --test=fileio help ”。
- --file-num生成文件数,默认128。
- --file-block-size磁盘I/O操作时文件块大小,默认16384(InnoDB16K数据页)。
- --file-total-size生成文件总大小,--file-total-size / --file-num即为生成的每个文件的大小了,默认2G。
- --file-test-mode磁盘I/O测试模式:{seqwr(顺序写), seqrewr(顺序读写), seqrd(顺序读), rndrd(随机读), rndwr(随机写), rndrw(随机读写)}
- --file-io-mode磁盘I/O操作模式:{sync(同步),async(异步),fastmmap(快映射),slowmmap(缓映射)},默认sync。
执行fileio测试需要三个步骤。
①prepare:准备阶段,生成测试文件。
②run:测试阶段,执行测试JOB
③cleanup:清场阶段。移除测试生成文件。
开始一个简单的fileio测试实例吧。
①prepare。生成总大小为1GB的8个文件(每个文件大小为128MB)。
②run。执行一个限时180s、双线程、最大1024请求的随机读写测试。root@ubuntu:~# mkdir benchmarker root@ubuntu:~# cd benchmarker/ root@ubuntu:~/benchmarker# sysbench --test=fileio --file-num=8 --file-total-size=1G prepare sysbench 0.4.12: multi-threaded system evaluation benchmark 8 files, 131072Kb each, 1024Mb total Creating files for the test... root@ubuntu:~/benchmarker# ls -lh 总用量 1.1G -rw------- 1 root root 128M 6月 22 07:47 test_file.0 -rw------- 1 root root 128M 6月 22 07:47 test_file.1 -rw------- 1 root root 128M 6月 22 07:47 test_file.2 -rw------- 1 root root 128M 6月 22 07:47 test_file.3 -rw------- 1 root root 128M 6月 22 07:47 test_file.4 -rw------- 1 root root 128M 6月 22 07:47 test_file.5 -rw------- 1 root root 128M 6月 22 07:47 test_file.6 -rw------- 1 root root 128M 6月 22 07:47 test_file.7
结果可知,随机读写能力为7.20Mb/s,IOPS为461.13,每个请求的平均用时为4.08ms,95%分布23.14ms。root@ubuntu:~/benchmarker# sysbench --test=fileio --file-num=8 --file-total-size=1G --file-test-mode=rndrw \ > --max-time=180 --max-requests=1024 --num-threads=2 --init-rng=on run sysbench 0.4.12: multi-threaded system evaluation benchmark Running the test with following options: Number of threads: 2 Initializing random number generator from timer. Extra file open flags: 0 8 files, 128Mb each 1Gb total file size Block size 16Kb Number of random requests for random IO: 1024 Read/Write ratio for combined random IO test: 1.50 Periodic FSYNC enabled, calling fsync() each 100 requests. Calling fsync() at the end of test, Enabled. Using synchronous I/O mode Doing random r/w test Threads started! Done. Operations performed: 615 Read, 410 Write, 88 Other = 1113 Total Read 9.6094Mb Written 6.4062Mb Total transferred 16.016Mb (7.2051Mb/sec) 461.13 Requests/sec executed Test execution summary: total time: 2.2228s total number of events: 1025 total time taken by event execution: 4.1798 per-request statistics: min: 0.01ms avg: 4.08ms max: 358.32ms approx. 95 percentile: 23.14ms Threads fairness: events (avg/stddev): 512.5000/74.50 execution time (avg/stddev): 2.0899/0.09
③cleanup。root@ubuntu:~/benchmarker# sysbench --test=fileio --file-num=8 --file-total-size=1G cleanup sysbench 0.4.12: multi-threaded system evaluation benchmark Removing test files... root@ubuntu:~/benchmarker# ls -a . ..
接着看看oltp测试。“ sysbench --test=oltp help ”一下,发现比fileio的选项多了好多,分为三个部分“oltp options”、“General database options”和“mysql options”。同样记录一些常用的选项。
- --db-driver数据库驱动(MySQL已经内编译可直接指定)。
- --mysql-db测试用schema,默认sbtest。
- --mysql-table-engine测试用table存储引擎,默认InnoDB。
- --oltp-table-size测试生成表大小(记录),默认1w。
- --oltp-read-only只读查询测试开关,默认关闭(即可以进行修改操作)。
- --oltp-dist-type随机数分布{uniform(均匀分布),gaussian(正态分布),special(特殊分布)},默认special,
oltp测试和fileio测试一样,也要经过prepare、run和cleanup三个阶段。区别在于oltp的对象是数据库而fileio的对象是文件。就行一个oltp基准测试的栗子吧。
root@ubuntu:~/benchmarker# sysbench --test=oltp --db-driver=mysql \ > --mysql-user=root --mysql-password=520 --mysql-db=benchmarker --oltp-table-size=10000 prepare sysbench 0.4.12: multi-threaded system evaluation benchmark Creating table 'sbtest'... Creating 10000 records in table 'sbtest'...
root@ubuntu:~/benchmarker# sysbench --test=oltp --db-driver=mysql --mysql-user=root --mysql-password=520 \ > --mysql-db=benchmarker --oltp-table-size=10000 --max-time=180 --max-requests=0 \ > --num-threads=2 --init-rng=on --oltp-read-only=on --oltp-dist-type=gaussian run sysbench 0.4.12: multi-threaded system evaluation benchmark Running the test with following options: Number of threads: 2 Initializing random number generator from timer. Doing OLTP test. Running mixed OLTP test Doing read-only test Using Normal distribution (12 iterations) Using "BEGIN" for starting transactions Using auto_inc on the id column Threads started! Time limit exceeded, exiting... (last message repeated 1 times) Done. OLTP test statistics: queries performed: read: 3750096 write: 0 other: 535728 total: 4285824 transactions: 267864 (1488.12 per sec.) deadlocks: 0 (0.00 per sec.) read/write requests: 3750096 (20833.74 per sec.) other operations: 535728 (2976.25 per sec.) Test execution summary: total time: 180.0011s total number of events: 267864 total time taken by event execution: 358.6674 per-request statistics: min: 1.14ms avg: 1.34ms max: 78.39ms approx. 95 percentile: 1.89ms Threads fairness: events (avg/stddev): 133932.0000/988.00 execution time (avg/stddev): 179.3337/0.00
root@ubuntu:~/benchmarker# sysbench --test=oltp --db-driver=mysql \ > --mysql-user=root --mysql-password=520 --mysql-db=benchmarker --oltp-table-size=10000 cleanup sysbench 0.4.12: multi-threaded system evaluation benchmark Dropping table 'sbtest'... Done.
可以看出,本次测试的TPS为1488.12。
参考:
《MySQL技术内幕 InnoDB存储引擎》(https://book.douban.com/subject/24708143/)
《高性能MySQL》(https://book.douban.com/subject/23008813/)