sysbench 是一个多线程的基准测试工具,一般用来评估不同系统参数下的数据库负载情况。

sysbench是一款简单易用的压力测试工具,可以测试cpu,memory,fileio,mysql数据库; 

sysbench安装方法

默认的epel源中的sysbench是0.4版本的,现在已经到1.x版本,本例采用1.x版本

github-->https://github.com/akopytov/sysbench

centos安装方法:7.6版本默认是1.x版本的

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

通过多线程压测CPU

启用10个线程进行测试:sysbench --threads=10 --time=300 threads run

查看CPU使用情况:

[root@nginx ~]# mpstat -P ALL 5
Average:     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
Average:     all   24.01    0.00   69.00    0.00    0.00    0.00    0.00    0.00    0.00    7.00
Average:       0   23.96    0.00   68.98    0.00    0.00    0.00    0.00    0.00    0.00    7.07
Average:       1   24.29    0.00   68.97    0.00    0.00    0.00    0.00    0.00    0.00    6.74
Average:       2   23.79    0.00   69.27    0.00    0.00    0.00    0.00    0.00    0.00    6.94
Average:       3   23.98    0.00   68.76    0.00    0.00    0.00    0.00    0.00    0.00    7.26


压测Mysql数据库

安装完成后重要的文件

    对数据库压力测试的文件:

    /usr/share/sysbench/bulk_insert.lua
    /usr/share/sysbench/oltp_common.lua
    /usr/share/sysbench/oltp_delete.lua    #数据库删除测试文件
    /usr/share/sysbench/oltp_insert.lua     #数据库插入测试文件
    /usr/share/sysbench/oltp_point_select.lua
    /usr/share/sysbench/oltp_read_only.lua #数据库读测试文件
    /usr/share/sysbench/oltp_read_write.lua#数据库读写测试文件
    /usr/share/sysbench/oltp_update_index.lua
    /usr/share/sysbench/oltp_update_non_index.lua
    /usr/share/sysbench/oltp_write_only.lua #数据库写测试文件
    /usr/share/sysbench/select_random_points.lua
    /usr/share/sysbench/select_random_ranges.lua


压测Mysql数据库
开始数据库测试:

    数据库测试分为3步:prepare(准备测试数据),run(开始测试),cleanup(清除测试数据)

    用到的相关参数:

        --db-driver=mysql
        --mysql-host=localhost
        --mysql-port=3306
        --mysql-socket=/tmp/mysql.sock
        --mysql-user=root
        --mysql-password=123456
        --mysql-db=sysbench
        --tables=10
        --table-size=500000
        --report-interval=10  
        --time=120
        --threads=50

    参数解析:

        --db-driver:用到的数据库类型

        --mysql-host:数据库的IP

        --mysql-port:数据库的端口

        --mysql-socket:socket的路径

        --mysql-user:数据库用户名

        --mysql-password:用户密码

        --mysql-db:数据库名字,默认为sysbench,需要提前创建创好

        --tables:生成表的个数

        --table-size:每个表的行数

        --report-interval:每隔多久在屏幕打印一次信息

        --time:压测时间

        --threads:启动多少个线程,即模拟多少个用户

准备测试数据:   

sysbench /usr/share/sysbench/oltp_read_only.lua --db-driver=mysql --mysql-host=localhost --mysql-port=3306 --mysql-socket=/tmp/mysql.sock --mysql-user=root --mysql-password=123456 --mysql-db=sysbench --tables=10 --table-size=500000 --report-interval=10  --time=120 --threads=50 prepare

压测数据库:

#读测试
sysbench /usr/share/sysbench/oltp_read_only.lua --db-driver=mysql --mysql-host=localhost --mysql-port=3306 --mysql-socket=/tmp/mysql.sock --mysql-user=root --mysql-password=123456 --mysql-db=sysbench --tables=10 --table-size=500000 --report-interval=10  --time=120 --threads=50 run
#读写测试
sysbench /usr/share/sysbench/oltp_read_write.lua --db-driver=mysql --mysql-host=localhost --mysql-port=3306 --mysql-socket=/tmp/mysql.sock --mysql-user=root --mysql-password=123456 --mysql-db=sysbench --tables=10 --table-size=500000 --report-interval=10  --time=120 --threads=50 run
#其它诊次类推

#读测试结果,模拟320个用户
[ 11s ] thds: 320 tps: 242.69 qps: 4209.17 (r/w/o: 3694.35/0.00/514.83) lat (ms,95%): 1678.14 err/s: 0.00 reconn/s: 0.00
[ 21s ] thds: 320 tps: 187.49 qps: 2996.82 (r/w/o: 2621.44/0.00/375.38) lat (ms,95%): 2120.76 err/s: 0.00 reconn/s: 0.00
[ 31s ] thds: 320 tps: 229.77 qps: 3649.12 (r/w/o: 3190.18/0.00/458.94) lat (ms,95%): 2159.29 err/s: 0.00 reconn/s: 0.00
[ 41s ] thds: 320 tps: 259.54 qps: 4175.73 (r/w/o: 3656.75/0.00/518.98) lat (ms,95%): 1803.47 err/s: 0.00 reconn/s: 0.00
[ 51s ] thds: 320 tps: 276.39 qps: 4406.96 (r/w/o: 3853.68/0.00/553.28) lat (ms,95%): 1589.90 err/s: 0.00 reconn/s: 0.00
[ 61s ] thds: 320 tps: 281.21 qps: 4496.82 (r/w/o: 3934.29/0.00/562.53) lat (ms,95%): 1561.52 err/s: 0.00 reconn/s: 0.00
[ 71s ] thds: 320 tps: 276.10 qps: 4435.70 (r/w/o: 3883.80/0.00/551.90) lat (ms,95%): 1648.20 err/s: 0.00 reconn/s: 0.00
[ 81s ] thds: 320 tps: 275.19 qps: 4392.43 (r/w/o: 3841.85/0.00/550.58) lat (ms,95%): 1618.78 err/s: 0.00 reconn/s: 0.00
[ 91s ] thds: 320 tps: 280.39 qps: 4491.68 (r/w/o: 3931.19/0.00/560.48) lat (ms,95%): 1589.90 err/s: 0.00 reconn/s: 0.00
[ 101s ] thds: 320 tps: 270.71 qps: 4334.41 (r/w/o: 3792.58/0.00/541.83) lat (ms,95%): 1678.14 err/s: 0.00 reconn/s: 0.00
[ 111s ] thds: 320 tps: 256.68 qps: 4100.25 (r/w/o: 3587.18/0.00/513.07) lat (ms,95%): 1771.29 err/s: 0.00 reconn/s: 0.00
[ 121s ] thds: 320 tps: 261.12 qps: 4087.48 (r/w/o: 3578.23/0.00/509.25) lat (ms,95%): 1708.63 err/s: 0.00 reconn/s: 0.00
SQL statistics:
    queries performed:
        read:                            438242
        write:                           0
        other:                           62606
        total:                           500848
    transactions:                        31303  (258.37 per sec.)
    queries:                             500848 (4133.87 per sec.)
    ignored errors:                      0      (0.00 per sec.)
    reconnects:                          0      (0.00 per sec.)

General statistics:
    total time:                          121.1541s
    total number of events:              31303

Latency (ms):
         min:                                124.29
         avg:                               1232.39
         max:                               3681.62
         95th percentile:                   1836.24
         sum:                            38577567.35

Threads fairness:
    events (avg/stddev):           97.8219/1.70
    execution time (avg/stddev):   120.5549/0.33

删除测试数据:

sysbench /usr/share/sysbench/oltp_read_write.lua --db-driver=mysql --mysql-host=localhost --mysql-port=3306 --mysql-socket=/tmp/mysql.sock --mysql-user=root --mysql-password=123456 --mysql-db=sysbench --tables=10 --table-size=500000 --report-interval=10  --time=120 --threads=50 cleanup