mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新,给出了性能测试数据而且提供了多种引擎的性能比较。mysqlslap为mysql性能优化前后提供了直观的验证依据,系统运维和DBA人员应该掌握一些常见的压力测试工具,才能准确的掌握线上数据库支撑的用户流量上限及其抗压性等问题。

在对MySQL进行压力测试之前,需要更改其默认的最大连接数,如下:

  1. [root@db01 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. max_connections=1024
  4. [root@db01 ~]# systemctl restart mysqld
  5. [root@db01 ~]# mysql -uroot -p123
  6. mysql> show variables like \'max_connections\';
  7. +-----------------+-------+
  8. | Variable_name | Value |
  9. +-----------------+-------+
  10. | max_connections | 1024 |
  11. +-----------------+-------+
  12. #验证修改已经生效
  13. [root@db01 ~]# mysqlslap --defaults-file=/etc/my.cnf --concurrency=100,200 --iterations=1 \
  14. --number-int-cols=20 --number-char-cols=30 --auto-generate-sql \
  15. --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed \
  16. --engine=myisam,innodb --number-of-queries=2000 -uroot -p123 --verbose
  17. #模拟测试两次读写并发,第一次100,第二次200,自动生成SQL脚本,测试表包含20个init字段,30个char字段,每次执行2000查询请求。测试引擎分别是myisam,innodb。
  18. Benchmark
  19. Running for engine myisam
  20. Average number of seconds to run all queries: 0.325 seconds
  21. Minimum number of seconds to run all queries: 0.325 seconds
  22. Maximum number of seconds to run all queries: 0.325 seconds
  23. Number of clients running queries: 100
  24. Average number of queries per client: 20
  25. #Myisam第一次100客户端同时发起增查用0.325/s
  26. Benchmark
  27. Running for engine myisam
  28. Average number of seconds to run all queries: 0.331 seconds
  29. Minimum number of seconds to run all queries: 0.331 seconds
  30. Maximum number of seconds to run all queries: 0.331 seconds
  31. Number of clients running queries: 200
  32. Average number of queries per client: 10
  33. #Myisam第二次200客户端同时发起增查用0.331/s
  34. Benchmark
  35. Running for engine innodb
  36. Average number of seconds to run all queries: 0.661 seconds
  37. Minimum number of seconds to run all queries: 0.661 seconds
  38. Maximum number of seconds to run all queries: 0.661 seconds
  39. Number of clients running queries: 100
  40. Average number of queries per client: 20
  41. #Innodb第一次100客户端同时发起增查用0.661/s
  42. Benchmark
  43. Running for engine innodb
  44. Average number of seconds to run all queries: 0.610 seconds
  45. Minimum number of seconds to run all queries: 0.610 seconds
  46. Maximum number of seconds to run all queries: 0.610 seconds
  47. Number of clients running queries: 200
  48. Average number of queries per client: 10
  49. #Innodb第二次200客户端同时发起增查用0.610/s

可以根据实际需求,一点点的加大并发数量进行压力测试!

关于mysqlslap工具各个选项的详细解释可以通过mysqlslap --help进行查询!

  1. [root@db01 ~]# yum -y install epel-release
  2. #安装epel源
  3. [root@db01 ~]# yum -y install sysbench
  4. #安装sysbench工具
  5. [root@db01 ~]# sysbench --version
  6. sysbench 1.0.17
  7. #查询sysbench工具的版本信息,确认sysbench工具已经安装

sysbench可以进行以下测试:

  • CPU运算性能测试;
  • 磁盘IO性能测试;
  • 调度程序性能测试;
  • 内存分配及传输速度测试;
  • POSIX线程性能测试;
  • 数据库性能测试(OLTP 基准测试,需要通过 /usr/share/sysbench/ 目录中的 Lua 脚本执行,例如 oltp_read_only.lua 脚本执行只读测试);
  • sysbench还可以通过运行命令时指定自己的Lua脚本进行自定义测试;
  1. [root@mysql ~]# sysbench --help
  2. Usage:
  3. sysbench [options]... [testname] [command]
  4. Commands implemented by most tests: prepare run cleanup help # 可用的命令,四个
  5. General options: # 通用选项
  6. --threads=N 要使用的线程数,默认 1 [1]
  7. --events=N 最大允许的事件个数 [0]
  8. --time=N 最大的总执行时间,以秒为单位 [10]
  9. --forced-shutdown=STRING --time 时间限制到达后,强制关闭之前等待的秒数,默认“off”禁用(number of seconds to wait after the --time limit before forcing shutdown, or \'off\' to disable [off]
  10. --thread-stack-size=SIZE 每个线程的堆栈大小 [64K]
  11. --rate=N 平均传输速率。0 则无限制 [0]
  12. --report-interval=N 以秒为单位定期报告具有指定间隔的中间统计信息 0 禁用中间报告 [0]
  13. --report-checkpoints=[LIST,...] 转储完整的统计信息并在指定的时间点重置所有计数器。参数是一个逗号分隔的值列表,表示从测试开始经过这个时间量时必须执行报告检查点(以秒为单位)。报告检查点默认关闭。 []
  14. --debug[=on|off] 打印更多 debug 信息 [off]
  15. --validate[=on|off] 尽可能执行验证检查 [off]
  16. --help[=on|off] 显示帮助信息并退出 [off]
  17. --version[=on|off] 显示版本信息并退出 [off]
  18. --config-file=FILENAME 包含命令行选项的文件
  19. --tx-rate=N 废弃,改用 --rate [0]
  20. --max-requests=N 废弃,改用 --events [0]
  21. --max-time=N 废弃,改用 --time [0]
  22. --num-threads=N 废弃,改用 --threads [1]
  23. Pseudo-Random Numbers Generator options: # 伪随机数发生器选项
  24. --rand-type=STRING random numbers distribution {uniform,gaussian,special,pareto} [special]
  25. --rand-spec-iter=N number of iterations used for numbers generation [12]
  26. --rand-spec-pct=N percentage of values to be treated as \'special\' (for special distribution) [1]
  27. --rand-spec-res=N percentage of \'special\' values to use (for special distribution) [75]
  28. --rand-seed=N seed for random number generator. When 0, the current time is used as a RNG seed. [0]
  29. --rand-pareto-h=N parameter h for pareto distribution [0.2]
  30. Log options: # 日志选项
  31. --verbosity=N verbosity level {5 - debug, 0 - only critical messages} [3]
  32. --percentile=N percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95]
  33. --histogram[=on|off] print latency histogram in report [off]
  34. General database options: # 通用的数据库选项
  35. --db-driver=STRING 指定要使用的数据库驱动程序 (\'help\' to get list of available drivers)
  36. --db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]
  37. --db-debug[=on|off] print database-specific debug information [off]
  38. Compiled-in database drivers: # 內建的数据库驱动程序,默认支持 MySQL 和 PostgreSQL
  39. mysql - MySQL driver
  40. pgsql - PostgreSQL driver
  41. mysql options: # MySQL 数据库专用选项
  42. --mysql-host=[LIST,...] MySQL server host [localhost]
  43. --mysql-port=[LIST,...] MySQL server port [3306]
  44. --mysql-socket=[LIST,...] MySQL socket
  45. --mysql-user=STRING MySQL user [sbtest]
  46. --mysql-password=STRING MySQL password []
  47. --mysql-db=STRING MySQL database name [sbtest]
  48. --mysql-ssl[=on|off] use SSL connections, if available in the client library [off]
  49. --mysql-ssl-cipher=STRING use specific cipher for SSL connections []
  50. --mysql-compression[=on|off] use compression, if available in the client library [off]
  51. --mysql-debug[=on|off] trace all client library calls [off]
  52. --mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205]
  53. --mysql-dry-run[=on|off] Dry run, pretend that all MySQL client API calls are successful without executing them [off]
  54. pgsql options: # PostgreSQL 数据库专用选项
  55. --pgsql-host=STRING PostgreSQL server host [localhost]
  56. --pgsql-port=N PostgreSQL server port [5432]
  57. --pgsql-user=STRING PostgreSQL user [sbtest]
  58. --pgsql-password=STRING PostgreSQL password []
  59. --pgsql-db=STRING PostgreSQL database name [sbtest]
  60. Compiled-in tests: # 內建测试类型
  61. fileio - File I/O test
  62. cpu - CPU performance test
  63. memory - Memory functions speed test
  64. threads - Threads subsystem performance test
  65. mutex - Mutex performance test
  66. See \'sysbench <testname> help\' for a list of options for each test.
  1. [root@db01 ~]# sysbench /usr/share/sysbench/oltp_common.lua help
  2. #查看sysbench自带的lua脚本使用方法
  3. [root@db01 ~]# mysqladmin -uroot -p123 create sbtest;
  4. #必须创建sbtest库,sbtest是sysbench默认使用的库名
  5. [root@db01 ~]# sysbench --mysql-host=127.0.0.1 \
  6. --mysql-port=3306 \
  7. --mysql-user=root \
  8. --mysql-password=123 \
  9. /usr/share/sysbench/oltp_common.lua \
  10. --tables=10 \
  11. --table_size=100000 \
  12. prepare
  13. #准备测试所用的表,这些测试表放在测试库sbtest中。这里使用的lua脚本为/usr/share/sysbench/oltp_common.lua
  14. #--tables=10表示创建10个测试表
  15. #--table_size=100000表示每个表中插入10W行数据
  16. #prepare表示这是准备数的过程
  1. [root@db01 ~]# mysql -uroot -p123 -e \'show tables from sbtest;\'
  2. +------------------+
  3. | Tables_in_sbtest |
  4. +------------------+
  5. | sbtest1 |
  6. | sbtest10 |
  7. | sbtest2 |
  8. | sbtest3 |
  9. | sbtest4 |
  10. | sbtest5 |
  11. | sbtest6 |
  12. | sbtest7 |
  13. | sbtest8 |
  14. | sbtest9 |
  15. +------------------+
  16. [root@db01 ~]# mysql -uroot -p123 -e \'select count(*) from sbtest.sbtest1;\'
  17. +----------+
  18. | count(*) |
  19. +----------+
  20. | 100000 |
  21. +----------+

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

所以,我这里用oltp_read_write.lua脚本来做读、写测试。还有很多其它类型的测试,比如只读测试、只写测试、删除测试、大批量插入测试等等。可找到对应的lua脚本进行调用即可。

  1. [root@db01 ~]# sysbench --threads=4 \
  2. --time=20 \
  3. --report-interval=5 \
  4. --mysql-host=127.0.0.1 \
  5. --mysql-port=3306 \
  6. --mysql-user=root \
  7. --mysql-password=123 \
  8. /usr/share/sysbench/oltp_read_write.lua \
  9. --tables=10 \
  10. --table_size=100000 \
  11. run
  12. sysbench 1.0.17 (using system LuaJIT 2.0.4)
  13. Running the test with following options:
  14. Number of threads: 4
  15. Report intermediate results every 5 second(s)
  16. Initializing random number generator from current time
  17. Initializing worker threads...
  18. Threads started!
  19. #以下是每5秒返回一次的结果,统计的指标包括:
  20. # 线程数、tps(每秒事务数)、qps(每秒查询数)、
  21. # 每秒的读/写/其它次数、延迟、每秒错误数、每秒重连次数
  22. [ 5s ] thds: 4 tps: 318.10 qps: 6371.34 (r/w/o: 4461.96/1272.39/636.99) lat (ms,95%): 20.37 err/s: 0.00 reconn/s: 0.00
  23. [ 10s ] thds: 4 tps: 340.15 qps: 6803.43 (r/w/o: 4760.92/1362.21/680.30) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
  24. [ 15s ] thds: 4 tps: 380.98 qps: 7622.45 (r/w/o: 5336.16/1524.33/761.97) lat (ms,95%): 13.22 err/s: 0.00 reconn/s: 0.00
  25. [ 20s ] thds: 4 tps: 387.29 qps: 7741.76 (r/w/o: 5420.03/1547.15/774.58) lat (ms,95%): 12.75 err/s: 0.00 reconn/s: 0.00
  26. SQL statistics:
  27. queries performed:
  28. read: 99918 #执行的读操作数量
  29. write: 28548 #执行的写操作数量
  30. other: 14274 #执行的其他操作数量
  31. total: 142740
  32. transactions: 7137 (356.13 per sec.) #执行事务的平均概率
  33. queries: 142740 (7122.70 per sec.) #平均每秒可以进行多少次查询
  34. ignored errors: 0 (0.00 per sec.)
  35. reconnects: 0 (0.00 per sec.)
  36. General statistics:
  37. total time: 20.0391s #总消耗时间
  38. total number of events: 7137 #总请求数量(读、写、其他)
  39. Latency (ms):
  40. min: 2.58
  41. avg: 11.22
  42. max: 450.05
  43. 95th percentile: 15.00 #采样计算的平均延迟
  44. sum: 80067.39
  45. Threads fairness:
  46. events (avg/stddev): 1784.2500/7.43
  47. execution time (avg/stddev): 20.0168/0.01

sysbench内置的几个测试指标如下:

  1. [root@db01 ~]# sysbench --help
  2. .......... # 省略部分内容
  3. Compiled-in tests:
  4. fileio - File I/O test
  5. cpu - CPU performance test
  6. memory - Memory functions speed test
  7. threads - Threads subsystem performance test
  8. mutex - Mutex performance test

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

  1. [root@db01 ~]# sysbench fileio help
  2. sysbench 1.0.17 (using system LuaJIT 2.0.4)
  3. fileio options:
  4. --file-num=N number of files to create [128]
  5. --file-block-size=N block size to use in all IO operations [16384]
  6. --file-total-size=SIZE total size of files to create [2G]
  7. --file-test-mode=STRING test mode {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw}
  8. --file-io-mode=STRING file operations mode {sync,async,mmap} [sync]
  9. --file-async-backlog=N number of asynchronous operatons to queue per thread [128]
  10. --file-extra-flags=[LIST,...] list of additional flags to use to open files {sync,dsync,direct} []
  11. --file-fsync-freq=N do fsync() after this number of requests (0 - don\'t use fsync()) [100]
  12. --file-fsync-all[=on|off] do fsync() after each write operation [off]
  13. --file-fsync-end[=on|off] do fsync() at the end of test [on]
  14. --file-fsync-mode=STRING which method to use for synchronization {fsync, fdatasync} [fsync]
  15. --file-merged-requests=N merge at most this number of IO requests if possible (0 - don\'t merge) [0]
  16. --file-rw-ratio=N reads/writes ratio for combined test [1.5]
1)测试IO性能
  1. [root@db01 ~]# sysbench fileio --file-num=5 --file-total-size=2G prepare
  2. [root@db01 ~]# ll
  3. 总用量 2097200
  4. -rw------- 1 root root 429506560 4 15 21:21 test_file.0
  5. -rw------- 1 root root 429506560 4 15 21:21 test_file.1
  6. -rw------- 1 root root 429506560 4 15 21:21 test_file.2
  7. -rw------- 1 root root 429506560 4 15 21:21 test_file.3
  8. -rw------- 1 root root 429506560 4 15 21:21 test_file.4
  9. [root@db01 ~]# sysbench --events=5000 \
  10. --threads=16 \
  11. fileio \
  12. --file-num=5 \
  13. --file-total-size=2G \
  14. --file-test-mode=rndrw \
  15. --file-fsync-freq=0 \
  16. --file-block-size=16384 \
  17. run
  18. sysbench 1.0.17 (using system LuaJIT 2.0.4)
  19. Running the test with following options:
  20. Number of threads: 16
  21. Initializing random number generator from current time
  22. Extra file open flags: (none)
  23. 5 files, 409.6MiB each
  24. 2GiB total file size
  25. Block size 16KiB
  26. Number of IO requests: 5000
  27. Read/Write ratio for combined random IO test: 1.50
  28. Calling fsync() at the end of test, Enabled.
  29. Using synchronous I/O mode
  30. Doing random r/w test
  31. Initializing worker threads...
  32. Threads started!
  33. File operations:
  34. reads/s: 4590.43
  35. writes/s: 3067.95
  36. fsyncs/s: 122.53
  37. Throughput: #吞吐量
  38. read, MiB/s: 71.73 #表示读带宽
  39. written, MiB/s: 47.94 #表示写带宽
  40. General statistics:
  41. total time: 0.6517s
  42. total number of events: 5000
  43. Latency (ms):
  44. min: 0.00
  45. avg: 1.61
  46. max: 43.35
  47. 95th percentile: 5.47
  48. sum: 8035.39
  49. Threads fairness:
  50. events (avg/stddev): 312.5000/16.19
  51. execution time (avg/stddev): 0.5022/0.01
2)测试CPU性能
  1. [root@db01 ~]# sysbench cpu --threads=40 --events=10000 --cpu-max-prime=20000 run
  2. sysbench 1.0.17 (using system LuaJIT 2.0.4)
  3. Running the test with following options:
  4. Number of threads: 40
  5. Initializing random number generator from current time
  6. Prime numbers limit: 20000
  7. Initializing worker threads...
  8. Threads started!
  9. CPU speed:
  10. events per second: 507.66
  11. General statistics:
  12. total time: 10.0370s
  13. total number of events: 5096
  14. Latency (ms):
  15. min: 1.94
  16. avg: 77.06
  17. max: 775.00
  18. 95th percentile: 397.39
  19. sum: 392676.67
  20. Threads fairness:
  21. events (avg/stddev): 127.4000/2.05
  22. execution time (avg/stddev): 9.8169/0.12

版权声明:本文为lvzhenjiang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/lvzhenjiang/p/14197149.html