sysbench是一款压力测试工具,可以测试系统的硬件性能,也可以用来对数据库进行基准测试,本文用的版本为sysbench-1.0.20,下载地址为:

sysbench支持三种测试方案:
1、使用自带的测试模块,如对cpu,I/O,memory等的测试。
2、使用自带的lua脚本进行测试,如果使用快速安装的方式,默认的脚本路径为:/usr/share/sysbench
3、使用自定义的lua脚本。

sysbench支持以下几种测试模式:
1、CPU运算性能
2、磁盘IO性能
3、调度程序性能
4、内存分配及传输速度
5、POSIX线程性能
6、数据库性能(OLTP基准测试)
目前sysbench主要支持 mysql,drizzle,pgsql,oracle 等几种数据库

1.安装sysbench

#安装依赖包
[root@dqynj027116 dzqd-root]# yum install automake libtool -y
[root@dqynj027116 dzqd-root]# tar zxf sysbench-1.0.20.tar.gz && cd sysbench-1.0.20
#执行autogen.sh来生成configure文件
[root@dqynj027116 sysbench-1.0.20]# ./autogen.sh
#压测编译安装的mysql需要指定include和lib文件的对应位置
#如果想要让 sysbench 支持 pgsql/oracle 的话,就需要在编译的时候加上参数
--with-pgsql 或者 --with-oracle
这2个参数默认是关闭的,只有 MySQL 是默认支持的
[root@dqynj027116 sysbench-1.0.20]# ./configure --prefix=/data_mysql/sysbench/ --with-mysql --with-mysql-includes=/data_mysql/mysql/include --with-mysql-libs=/data_mysql/mysql/lib && make && make install
#设置依赖包的软连接
[root@dqynj027116 sysbench-1.0.20]# ln -s /data_mysql/mysql/lib/libmysqlclient.so.20 /lib64/libmysqlclient.so.20
#设置环境变量
[root@dqynj027116 dzqd-root]# echo "export PATH=$PATH:/data_mysql/sysbench/bin"  >> /etc/profile && source /etc/profile
#测试成果
[root@dqynj027116 dzqd-root]# sysbench cpu --cpu-max-prime=10000 run
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 1
Initializing random number generator from current time


Prime numbers limit: 10000

Initializing worker threads...

Threads started!

CPU speed:
    events per second:   689.90

General statistics:
    total time:                          10.0006s
    total number of events:              6901

Latency (ms):
         min:                                    1.39
         avg:                                    1.45
         max:                                    1.52
         95th percentile:                        1.47
         sum:                                 9997.79

Threads fairness:
    events (avg/stddev):           6901.0000/0.00
    execution time (avg/stddev):   9.9978/0.00

2.命令解析:

如果对相关模块或者脚本的参数不明确的,可以使用sysbench testname help来查看,testname含模块名称和脚本名称。
sysbench经典测试命令:
prepare: 创建测试数据或者相关的测试文件,为测试做准备工作。如数据库的测试数据准备等。

run: 开始执行测试。

cleanup: 移除测试数据

help: 帮助命令

[root@dqynj027116 ~]# /data_mysql/sysbench/bin/sysbench --help
Usage:
  sysbench [options]... [testname] [command]

Commands implemented by most tests: prepare run cleanup help

General options:            # 通用选项
  --threads=N               要使用的线程数,默认 1 个 [1]
  --events=N                最大允许的事件个数,默认为[0]
  --time=N                  最大的总执行时间,以秒为单位默认为[10]
  --thread-stack-size=SIZE     每个线程的堆栈大小,默认为[64K]
  --rate=N                   指定数量多少事件(事务)平均每秒钟应该执行的所有线程。0(默认)意味着无限的速率,即事件尽快执行
  --report-interval=N          以秒为单位定期报告具有指定间隔的中间统计信息, 0 禁用中间报告,默认为0
  --debug[=on|off]             打印更多 debug 信息 [off]
  --validate[=on|off]            尽可能执行验证检查 [off]
  --help[=on|off]               显示帮助信息并退出 [off]
  --version[=on|off]             显示版本信息并退出 [off]
  --config-file=FILENAME        包含命令行选项的文件
  --tables=N                   表数量(数据库测试)
  --table-size=N                   表大小


mysql options:              # MySQL 数据库专用选项
  --mysql-host=[LIST,...]          MySQL server host [localhost]
  --mysql-port=[LIST,...]          MySQL server port [3306]
  --mysql-socket=[LIST,...]        MySQL socket
  --mysql-user=STRING              
  --mysql-password=STRING         
  --mysql-db=STRING             #指定数据库需要提前创建库  
  --mysql-ssl[=on|off]             
  --mysql-ssl-cipher=STRING       
  --mysql-compression[=on|off]     
  --mysql-debug[=on|off]          
  --mysql-ignore-errors=[LIST,...] 
  --mysql-dry-run[=on|off]         

pgsql options:              # PostgreSQL 数据库专用选项
  --pgsql-host=STRING     
  --pgsql-port=N          
  --pgsql-user=STRING     
  --pgsql-password=STRING 
  --pgsql-db=STRING

3.测试mysql

1.创建测试数据库
[root@dqynj027116 slow-mysql-query]# /data_mysql/sysbench/bin/sysbench /data_mysql/sysbench/share/sysbench/oltp_common.lua --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=pcmcc --table-size=500000 --tables=10 --threads=8 prepare

2.读性能测试
[root@dqynj027116 slow-mysql-query]# /data_mysql/sysbench/bin/sysbench /data_mysql/sysbench/share/sysbench/oltp_read_only.lua --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=pcmcc --table-size=500000 --tables=10 --threads=1 run
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 8
Initializing random number generator from current time


Initializing worker threads...

Threads started!

SQL statistics:
    queries performed:
        read:                            938224    -- 读总数
        write:                           268064    -- 写总数
        other:                           134032    -- 其他操作总数(SELECT、INSERT、UPDATE、DELETE之外的操作,例如COMMIT等)
        total:                           1340320    -- 全部总数
    transactions:                        67016  (1116.83 per sec.)    -- 总事务数(每秒事务数)
    deadlocks:                           0      (0.00 per sec.)    -- 发生死锁总数
    read/write requests:                 1206288 (20103.01 per sec.)    -- 读写总数(每秒读写次数)
    other operations:                    134032 (2233.67 per sec.)    -- 其他操作总数(每秒其他操作次数)

General statistics:    -- 一些统计结果
    total time:                          60.0053s    -- 总耗时
    total number of events:              67016    -- 共发生多少事务数
    total time taken by event execution: 479.8171s    -- 所有事务耗时相加(不考虑并行因素)
    response time:    -- 响应时长统计
         min:                                  4.27ms    -- 最小耗时
         avg:                                  7.16ms    -- 平均耗时
         max:                                 13.80ms    -- 最长耗时
         approx.  99 percentile:               9.88ms    -- 超过99%平均耗时

Threads fairness:
    events (avg/stddev):           8377.0000/44.33
    execution time (avg/stddev):   59.9771/0.00

3.写性能测试:
[root@dqynj027116 slow-mysql-query]# /data_mysql/sysbench/bin/sysbench /data_mysql/sysbench/share/sysbench/oltp_write_only.lua --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=pcmcc --table-size=500000 --tables=10 --threads=1 run

4.删除测试数据:
[root@dqynj027116 slow-mysql-query]#  /data_mysql/sysbench/bin/sysbench /data_mysql/sysbench/share/sysbench/oltp_common.lua --mysql-host=localhost --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=pcmcc --table-size=500000 --tables=10 --threads=8 cleanup