前言:最近新到一批机器,需要对机器进行压测,主要压测范围包括CPU测试、磁盘IO测试、线程测试、OLTP测试等,那么sysbench就可以满足我们的压测需求。下面我们简单来看下sysbench的安装使用以及压测结果的解读。

一、sysbench安装

sysbench安装其实非常简单,可以参考下git,其实MySQL官网也可以下到,不过版本比较老了,最新的版本是sysbench1.1.0,这里我们讲下源码编译安装。
上传sysbench到服务器、解压、安装:

  1. [root@localhost tmp]# unzip sysbench-master.zip
  2. [root@localhost tmp]# cd sysbench-master
  3. [root@localhost sysbench-master]# ./autogen.sh
  4. [root@localhost sysbench-master]# ./configure
  5. [root@localhost sysbench-master]# make -j
  6. [root@localhost sysbench-master]# make install

缺少什么包按提示安装一下即可

  1. yum install autoconf automake libtool -y

其实可以查看下README.md,上面也写了如何安装

  1. ## Build and Install
  2. shell
  3. ./autogen.sh
  4. # Add --with-pgsql to build with PostgreSQL support
  5. ./configure
  6. make -j
  7. make install

二、压测过程及压测结果解读

1.cpu测试

  1. [root@localhost storage]# sysbench --test=cpu --cpu-max-prime=2000000 run
  2. WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
  3. sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)
  4. Running the test with following options:
  5. Number of threads: 1
  6. Initializing random number generator from current time
  7. Prime numbers limit: 2000000
  8. Initializing worker threads...
  9. Threads started!
  10. CPU speed:
  11. events per second: 0.63
  12. Throughput:
  13. events/s (eps): 0.6334
  14. time elapsed: 11.0520s
  15. total number of events: 7
  16. Latency (ms):
  17. min: 1571.86
  18. avg: 1578.84
  19. max: 1620.53
  20. 95th percentile: 1618.78
  21. sum: 11051.91
  22. Threads fairness:
  23. events (avg/stddev): 7.0000/0.00
  24. execution time (avg/stddev): 11.0519/0.00

cpu测试主要是进行素数的加法运算,上面我们的例子中,指定了最大的质数发生器数量为 2000000,可以看出服务器此次测试 执行时间 大约为11.05秒
2.磁盘IO测试

  1. [root@localhost storage]# sysbench --test=fileio --num-threads=16 --file-total-size=30G --file-test-mode=rndrw prepare
  2. WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
  3. WARNING: --num-threads is deprecated, use --threads instead
  4. sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)
  5. 128 files, 245760Kb each, 30720Mb total
  6. Creating files for the test...
  7. Extra file open flags: (none)
  8. Creating file test_file.0
  9. Creating file test_file.1
  10. Creating file test_file.2
  11. Creating file test_file.3
  12. Creating file test_file.4
  13. Creating file test_file.5
  14. Creating file test_file.6
  15. Creating file test_file.7
  16. Creating file test_file.8
  17. Creating file test_file.9
  18. Creating file test_file.10
  19. Creating file test_file.11
  20. Creating file test_file.12
  21. Creating file test_file.13
  22. Creating file test_file.14
  23. Creating file test_file.15
  24. Creating file test_file.16
  25. Creating file test_file.17
  26. Creating file test_file.18
  27. Creating file test_file.19
  28. Creating file test_file.20
  29. Creating file test_file.21
  30. Creating file test_file.22
  31. Creating file test_file.23
  32. Creating file test_file.24
  33. Creating file test_file.25
  34. Creating file test_file.26
  35. Creating file test_file.27
  36. Creating file test_file.28
  37. Creating file test_file.29
  38. Creating file test_file.30
  39. Creating file test_file.31
  40. Creating file test_file.32
  41. Creating file test_file.33
  42. Creating file test_file.34
  43. Creating file test_file.35
  44. Creating file test_file.36
  45. Creating file test_file.37
  46. Creating file test_file.38
  47. Creating file test_file.39
  48. Creating file test_file.40
  49. Creating file test_file.41
  50. Creating file test_file.42
  51. Creating file test_file.43
  52. Creating file test_file.44
  53. Creating file test_file.45
  54. Creating file test_file.46
  55. Creating file test_file.47
  56. Creating file test_file.48
  57. Creating file test_file.49
  58. Creating file test_file.50
  59. Creating file test_file.51
  60. Creating file test_file.52
  61. Creating file test_file.53
  62. Creating file test_file.54
  63. Creating file test_file.55
  64. Creating file test_file.56
  65. Creating file test_file.57
  66. Creating file test_file.58
  67. Creating file test_file.59
  68. Creating file test_file.60
  69. Creating file test_file.61
  70. Creating file test_file.62
  71. Creating file test_file.63
  72. Creating file test_file.64
  73. Creating file test_file.65
  74. Creating file test_file.66
  75. Creating file test_file.67
  76. Creating file test_file.68
  77. Creating file test_file.69
  78. Creating file test_file.70
  79. Creating file test_file.71
  80. Creating file test_file.72
  81. Creating file test_file.73
  82. Creating file test_file.74
  83. Creating file test_file.75
  84. Creating file test_file.76
  85. Creating file test_file.77
  86. Creating file test_file.78
  87. Creating file test_file.79
  88. Creating file test_file.80
  89. Creating file test_file.81
  90. Creating file test_file.82
  91. Creating file test_file.83
  92. Creating file test_file.84
  93. Creating file test_file.85
  94. Creating file test_file.86
  95. Creating file test_file.87
  96. Creating file test_file.88
  97. Creating file test_file.89
  98. Creating file test_file.90
  99. Creating file test_file.91
  100. Creating file test_file.92
  101. Creating file test_file.93
  102. Creating file test_file.94
  103. Creating file test_file.95
  104. Creating file test_file.96
  105. Creating file test_file.97
  106. Creating file test_file.98
  107. Creating file test_file.99
  108. Creating file test_file.100
  109. Creating file test_file.101
  110. Creating file test_file.102
  111. Creating file test_file.103
  112. Creating file test_file.104
  113. Creating file test_file.105
  114. Creating file test_file.106
  115. Creating file test_file.107
  116. Creating file test_file.108
  117. Creating file test_file.109
  118. Creating file test_file.110
  119. Creating file test_file.111
  120. Creating file test_file.112
  121. Creating file test_file.113
  122. Creating file test_file.114
  123. Creating file test_file.115
  124. Creating file test_file.116
  125. Creating file test_file.117
  126. Creating file test_file.118
  127. Creating file test_file.119
  128. Creating file test_file.120
  129. Creating file test_file.121
  130. Creating file test_file.122
  131. Creating file test_file.123
  132. Creating file test_file.124
  133. Creating file test_file.125
  134. Creating file test_file.126
  135. Creating file test_file.127
  136. 32212254720 bytes written in 55.25 seconds (556.00 MiB/sec).
  137. [root@localhost storage]# sysbench --test=fileio --num-threads=16 --file-total-size=30G --file-test-mode=rndrw run
  138. WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
  139. WARNING: --num-threads is deprecated, use --threads instead
  140. sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)
  141. Running the test with following options:
  142. Number of threads: 16
  143. Initializing random number generator from current time
  144. Extra file open flags: (none)
  145. 128 files, 240MiB each
  146. 30GiB total file size
  147. Block size 16KiB
  148. Number of IO requests: 0
  149. Read/Write ratio for combined random IO test: 1.50
  150. Periodic FSYNC enabled, calling fsync() each 100 requests.
  151. Calling fsync() at the end of test, Enabled.
  152. Using synchronous I/O mode
  153. Doing random r/w test
  154. Initializing worker threads...
  155. Threads started!
  156. Throughput:
  157. read: IOPS=68491.05 1070.17 MiB/s (1122.16 MB/s)
  158. write: IOPS=45660.96 713.45 MiB/s (748.11 MB/s)
  159. fsync: IOPS=146103.57
  160. Latency (ms):
  161. min: 0.00
  162. avg: 0.06
  163. max: 3.61
  164. 95th percentile: 0.31
  165. sum: 156128.82

可以看到,磁盘的读IOPS可以达到68491.05,写IOPS可以达到45660.96,fsync可以达到146103.57
3.线程测试

  1. [root@localhost storage]# sysbench --test=threads --num-threads=64 --thread-yields=100 --thread-locks=2 run
  2. WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
  3. WARNING: --num-threads is deprecated, use --threads instead
  4. sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)
  5. Running the test with following options:
  6. Number of threads: 64
  7. Initializing random number generator from current time
  8. Initializing worker threads...
  9. Threads started!
  10. Throughput:
  11. events/s (eps): 14851.1802
  12. time elapsed: 10.0036s
  13. total number of events: 148565
  14. Latency (ms):
  15. min: 0.02
  16. avg: 4.31
  17. max: 24.49
  18. 95th percentile: 10.46
  19. sum: 639889.93
  20. Threads fairness:
  21. events (avg/stddev): 2321.3281/42.94
  22. execution time (avg/stddev): 9.9983/0.00

(发送64次/个测试线程请求,每次/个线程请求产生/生成100个数量,每个线程的锁数量为2) ,测试执行时间为10秒
4.内存测试

  1. [root@localhost storage]# sysbench --test=memory --memory-block-size=8k --memory-total-size=40G run
  2. WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
  3. sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)
  4. Running the test with following options:
  5. Number of threads: 1
  6. Initializing random number generator from current time
  7. Running memory speed test with the following options:
  8. block size: 8KiB
  9. total size: 40960MiB
  10. operation: write
  11. scope: global
  12. Initializing worker threads...
  13. Threads started!
  14. Total operations: 5242880 (1443403.42 per second)
  15. 40960.00 MiB transferred (11276.59 MiB/sec)
  16. Throughput:
  17. events/s (eps): 1443403.4239
  18. time elapsed: 3.6323s
  19. total number of events: 5242880
  20. Latency (ms):
  21. min: 0.00
  22. avg: 0.00
  23. max: 0.02
  24. 95th percentile: 0.00
  25. sum: 2926.10
  26. Threads fairness:
  27. events (avg/stddev): 5242880.0000/0.00
  28. execution time (avg/stddev): 2.9261/0.00

上述参数指定了本次测试整个过程是在内存中传输 40G 的数据量,每个 block 大小为 8K。测试结果显示:
执行时间为3.6323秒,每秒传输速度为11276.59Mb每秒
5.OLTP测试
(1)100线程
数据准备:

  1. [root@localhost storage]# sysbench /usr/local/share/sysbench/oltp_common.lua \
  2. > --mysql-user=tpcc --mysql-password=tpcc --mysql-socket=/tmp/mysql3306.sock \
  3. > --tables=10 --table_size=20000000 --threads=100 --max-requests=0 prepare
  4. sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)
  5. Initializing worker threads...
  6. Creating table 'sbtest1'...
  7. Creating table 'sbtest4'...
  8. Creating table 'sbtest2'...
  9. Creating table 'sbtest9'...
  10. Creating table 'sbtest6'...
  11. Creating table 'sbtest8'...
  12. Creating table 'sbtest7'...
  13. Creating table 'sbtest5'...
  14. Creating table 'sbtest10'...
  15. Creating table 'sbtest3'...
  16. Inserting 20000000 records into 'sbtest1'
  17. Inserting 20000000 records into 'sbtest7'
  18. Inserting 20000000 records into 'sbtest4'
  19. Inserting 20000000 records into 'sbtest9'
  20. Inserting 20000000 records into 'sbtest6'
  21. Inserting 20000000 records into 'sbtest8'
  22. Inserting 20000000 records into 'sbtest2'
  23. Inserting 20000000 records into 'sbtest5'
  24. Inserting 20000000 records into 'sbtest3'
  25. Inserting 20000000 records into 'sbtest10'
  26. Creating a secondary index on 'sbtest4'...
  27. Creating a secondary index on 'sbtest1'...
  28. Creating a secondary index on 'sbtest10'...
  29. Creating a secondary index on 'sbtest3'...
  30. Creating a secondary index on 'sbtest6'...
  31. Creating a secondary index on 'sbtest8'...
  32. Creating a secondary index on 'sbtest7'...
  33. Creating a secondary index on 'sbtest9'...
  34. Creating a secondary index on 'sbtest5'...
  35. Creating a secondary index on 'sbtest2'...

开始测试:

  1. [root@localhost storage]# sysbench /usr/local/share/sysbench/oltp_read_write.lua --mysql-host=localhost --mysql-port=3306 --mysql-db=sbtest --mysql-user=tpcc --mysql-password=tpcc --table_size=20000000 --tables=10 --threads=100 --time=3600 --report-interval=10 --mysql-socket=/tmp/mysql3306.sock run > /tmp/40_100.log

测试过程中,可以tail -f /tmp/40_100.log查看输出
测试结果:这里只展示统计部分

  1. SQL statistics:
  2. queries performed:
  3. read: 143195290
  4. write: 40912940
  5. other: 20456470
  6. total: 204564700
  7. transactions: 10228235 (2841.15 per sec.)
  8. queries: 204564700 (56823.07 per sec.)
  9. ignored errors: 0 (0.00 per sec.)
  10. reconnects: 0 (0.00 per sec.)
  11. Throughput:
  12. events/s (eps): 2841.1536
  13. time elapsed: 3600.0289s
  14. total number of events: 10228235
  15. Latency (ms):
  16. min: 2.49
  17. avg: 35.19
  18. max: 2017.54
  19. 95th percentile: 39.65
  20. sum: 359973625.38
  21. Threads fairness:
  22. events (avg/stddev): 102282.3500/5194.20
  23. execution time (avg/stddev): 3599.7363/0.01

(2)200线程
数据准备:

  1. [root@localhost storage]# sysbench /usr/local/share/sysbench/oltp_common.lua \
  2. > --mysql-user=tpcc --mysql-password=tpcc --mysql-socket=/tmp/mysql3306.sock \
  3. > --tables=10 --table_size=20000000 --threads=200 --max-requests=0 prepare

开始测试:

  1. [root@localhost storage]# sysbench /usr/local/share/sysbench/oltp_read_write.lua \
  2. --mysql-host=localhost --mysql-port=3306 --mysql-db=sbtest --mysql-user=tpcc \
  3. --mysql-password=tpcc --table_size=20000000 --tables=10 --threads=200 --time=3600 \
  4. --report-interval=10 --mysql-socket=/tmp/mysql3306.sock run > /tmp/40_200.log

测试结果:

  1. SQL statistics:
  2. queries performed:
  3. read: 142876258
  4. write: 40821788
  5. other: 20410894
  6. total: 204108940
  7. transactions: 10205447 (2834.80 per sec.)
  8. queries: 204108940 (56696.02 per sec.)
  9. ignored errors: 0 (0.00 per sec.)
  10. reconnects: 0 (0.00 per sec.)
  11. Throughput:
  12. events/s (eps): 2834.8009
  13. time elapsed: 3600.0577s
  14. total number of events: 10205447
  15. Latency (ms):
  16. min: 2.39
  17. avg: 70.55
  18. max: 2011.65
  19. 95th percentile: 80.03
  20. sum: 719978372.22
  21. Threads fairness:
  22. events (avg/stddev): 51027.2350/3840.19
  23. execution time (avg/stddev): 3599.8919/0.02

(3)300线程
数据准备:

  1. [root@localhost storage]# sysbench /usr/local/share/sysbench/oltp_common.lua \
  2. > --mysql-user=tpcc --mysql-password=tpcc --mysql-socket=/tmp/mysql3306.sock \
  3. > --tables=10 --table_size=20000000 --threads=300 --max-requests=0 prepare

开始测试:

  1. sysbench /usr/local/share/sysbench/oltp_read_write.lua \
  2. --mysql-host=localhost --mysql-port=3306 --mysql-db=sbtest \
  3. --mysql-user=tpcc --mysql-password=tpcc --table_size=20000000 --tables=10 --threads=300 \
  4. --time=3600 --report-interval=10 --mysql-socket=/tmp/mysql3306.sock run > /tmp/40_300.log

测试结果:

  1. SQL statistics:
  2. queries performed:
  3. read: 144281438
  4. write: 41223268
  5. other: 20611634
  6. total: 206116340
  7. transactions: 10305817 (2862.66 per sec.)
  8. queries: 206116340 (57253.12 per sec.)
  9. ignored errors: 0 (0.00 per sec.)
  10. reconnects: 0 (0.00 per sec.)
  11. Throughput:
  12. events/s (eps): 2862.6561
  13. time elapsed: 3600.0891s
  14. total number of events: 10305817
  15. Latency (ms):
  16. min: 2.64
  17. avg: 104.79
  18. max: 4576.60
  19. 95th percentile: 123.28
  20. sum: 1079986824.04
  21. Threads fairness:
  22. events (avg/stddev): 34352.7233/2459.96
  23. execution time (avg/stddev): 3599.9561/0.03

生成压测图
安装gnuplot

  1. plot 'D:\40_100.log' using 9 with linepoint title '100 threads','D:\40_200.log' using 9 with linepoint title '200 threads','D:\40_300.log' using 9 with linepoint title '300 threads';

【MySQL】sysbench压测服务器及结果解读 - 图1

【MySQL】sysbench压测服务器及结果解读 - 图2

三、压测注意事项

1.在OLTP测试过程中,数据库初始化后,冷备data目录,每次测试完毕后删除当前数据目录,拷贝data目录重新启动,为的是每次开始测试前环境保持一致。
2.测试完成后,需要再系统层做一些清理的工作
shell>sync #刷新脏数据到磁盘
shell>echo 3 >/proc/sys/vm/drop_cache #清除cache
shell>swapoff -a && swapon -a 释放swap
3.压测过程中需要修改参数max_prepared_stmt_count默认为16382,修改为65536
4.如果是SSD硬盘的话,innodb_io_capacity和innodb_io_capacity_max两个参数也要相应调大,建议调整到50000以上。
5.对于测试数据的准备,我们通过show engine innodb status观察Free buffers,尽量模拟生产环境,让测试数据填满整个buffer pool。我这里innodb_buffer_pool_size设置的40G,测试表数量10张,每张表数据2000万,差不多填满buffer pool。

下篇会给大家带来InnoDB引擎和TokuDB引擎的压力测试。