一、sysbench 介绍

sysbench 是一个模块化、支持多平台安装运行,多线程的基准测试工具,sysbench,不仅用来测试数据库的性能,也可以测试运行数据库的服务器的性能。
sysbench,不仅用来测试数据库的性能,也可以测试运行数据库的服务器的性能。
oltp 测试主要是结合了 lua 脚本,不需要修改源码,通过自定义 lua 脚本就可以实现不同业务类型的测试。

主要有以下几种方式的测试:
cpu 基准测试
磁盘 I/O 基准测试
内存连续读写性能测试
测试互斥锁性能
测试顺序读写性能
数据库性能 OLTP 基准测试

什么是 OLTP?
On-Line Transaction Processing 联机事务处理过程(OLTP)
也称为面向交易的处理过程,其基本特征是前台接收的用户数据可以立即传送到计算中心进行处理,并在很短的时间内给出处理结果,是对用户操作快速响应的方式之一。

二、sysbench 安装和部分参数介绍

1.安装 sysbench

在 centos7 上

# curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash
sudo yum -y install sysbench

  

2.sysbench 参数介绍

$ sysbench --help
Usage:
  sysbench [options]... [testname] [command]

Commands implemented by most tests: prepare run cleanup help
sysbench 压测需要 3 个步骤:
prepare(准备数据) -> run(运行测试) -> cleanup(清理数据)
General options:
  --threads=N        #创建测试线程的数量,默认值为 1
  --events=N           #限制事件的总数量,0 表示不限制,默认值为 0
  --time=N                #限制总共执行多长时间,单位是秒,默认是 10
  --forced-shutdown=STRING      #超过--time 后,等待多长时间强制关闭,单位是秒,默认 off
  --thread-stack-size=SIZE        #每个线程的堆大小,默认是 64k
  --rate=N                        #平均事务率,0 表示不限制
  --report-interval=N            #定期报告统计数据的时间间隔,单位秒,默认为 0,表示不显示中间报告。
  --report-checkpoints=[LIST,...]  #转储完整的统计信息并在指定的时间点重置所有计数器。默认为关闭
  --config-file=FILENAME         #可以把命令参数写到一个文件中,指定这个文件
  --tx-rate=N                         #该参数弃用,改为 --rate [0]
  --max-requests=N               #该参数弃用,改为--events [0]
  --max-time=N                     #该参数弃用,改为 --time [0]
  --num-threads=N                #该参数弃用,改为 --threads [1]

mysql options:
  --mysql-host=[LIST,...]        #  MySQL 服务器地址 ,默认 localhost
  --mysql-port=[LIST,...]        # MySQL 服务器端口 ,默认 3306
  --mysql-socket=[LIST,...]     # MySQL socket 文件
  --mysql-user=STRING              MySQL user 默认 sbtest
  --mysql-password=STRING          MySQL password 默认为空
  --mysql-db=STRING                MySQL database name 默认 sbtest

  --mysql-compression[=on|off]     #是否使用压缩,默认为 off

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

  

三、sysbench 使用说明

sysbench 压测 MySQL 有三个步骤:prepare --> run --> cleanup
我们都是使用 sysbench 自带的 lua 脚本来压测 MySQL

1.压测 MySQL

使用 oltp_read_write.lua 脚本压测 MySQL
准备阶段:

mysql -uroot -pllill2018 -e "create database sbtest;" -S /tmp/mysql3308.sock
sysbench /usr/share/sysbench/oltp_read_write.lua --mysql-host=172.16.17.123 --mysql-port=3308  --mysql-user=root --mysql-password='llill2018' --mysql-socket=/tmp/mysql3308.sock --mysql-db=sbtest --db-driver=mysql --tables=20 --table-size=5000000  --threads=32  prepare

  

测试阶段:

sysbench /usr/share/sysbench/oltp_read_write.lua --mysql-host=172.16.17.123 --mysql-port=3308  --mysql-user=root --mysql-password='llill2018' --mysql-socket=/tmp/mysql3308.sock --mysql-db=sbtest --db-driver=mysql --tables=10 --table-size=10000  --threads=32  --report-interval=10 --time=120 run

  

输出结果如下:

sysbench 1.0.14 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 32
Report intermediate results every 10 second(s)
Initializing random number generator from current time


Initializing worker threads...

Threads started!

[ 10s ] thds: 32 tps: 266.21 qps: 5355.17 (r/w/o: 3753.19/1066.56/535.43) lat (ms,95%): 186.54 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 32 tps: 279.40 qps: 5590.16 (r/w/o: 3914.18/1116.99/559.00) lat (ms,95%): 127.81 err/s: 0.00 reconn/s: 0.00
[ 30s ] thds: 32 tps: 273.60 qps: 5470.54 (r/w/o: 3829.26/1094.09/547.19) lat (ms,95%): 144.97 err/s: 0.00 reconn/s: 0.00
[ 40s ] thds: 32 tps: 258.20 qps: 5169.85 (r/w/o: 3620.43/1033.01/516.40) lat (ms,95%): 248.83 err/s: 0.00 reconn/s: 0.00
[ 50s ] thds: 32 tps: 281.80 qps: 5627.48 (r/w/o: 3936.89/1127.20/563.40) lat (ms,95%): 127.81 err/s: 0.00 reconn/s: 0.00
[ 60s ] thds: 32 tps: 279.50 qps: 5595.15 (r/w/o: 3917.53/1118.41/559.20) lat (ms,95%): 127.81 err/s: 0.00 reconn/s: 0.00
[ 70s ] thds: 32 tps: 278.30 qps: 5565.72 (r/w/o: 3895.85/1113.38/556.49) lat (ms,95%): 132.49 err/s: 0.00 reconn/s: 0.00
[ 80s ] thds: 32 tps: 262.40 qps: