myql基准测试工具Sysbench

一、Sysbench介绍

SysBench是一个模块化的、跨平台、多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况。它主要包括以下几种方式的测试:

1、cpu性能

2、磁盘io性能

3、调度程序性能

4、内存分配及传输速度

5、POSIX线程性能

6、数据库性能(OLTP基准测试)

Sysbench的oltp主要用于评估测试各种不同系统参数下的数据库负载情况。目前sysbench的数据库测试支持MySQL、PostgreSQL、Oracle。相比0.4版本,后续的版本oltp测试主要结合了lua脚本,不需要修改源码,通过自定义lua脚本就可以实现不同业务类型的测试。

二、Sysbench安装使用

如果没有什么特别需求,直接使用EPEL源安装Sysbench即可。

# yum -y install epel-release

# yum -y install sysbench

# sysbench --version

sysbench 1.0.9

# sysbench --help

Usage:

  sysbench [options]... [testname] [command]

General options

--threads=N:创建测试线程的数量, 默认为[1]。

--events=N:事件最大数量,默认为[0] ,不限制。

--time=N:最大执行时间,单位是s,默认是[0] ,不限制。

--forced-shutdown=STRING:超过max-time强制中断, 默认是[off]。

--thread-stack-size=SIZE:每个线程的堆栈大小, 默认是[64k]。

--rate=N:average transactions rate. 0 for unlimited rate [0]。

--report-interval=N:报告中间统计信息间隔,0代表禁止,默认为[0]。

--report-checkpoints=[LIST,...]:转储完全统计信息并在指定时间点复位所有计数器,参数是逗号分隔值的列表,表示从必须执行报告检查点的测试开始所经过的时间(以秒为单位)。 默认情况下,报告检查点处于关闭状态[off]。

--debug[=on|off]:是否显示更多的调试信息,默认是[off]。

--validate[=on|off]:在可能情况下执行验证检查,默认是[off]。

--help[=on|off]:输出help信息,并退出。

--version[=on|off]:输出版本信息, 并退出。

--config-file=FILENAME:配置文件。

--tx-rate=N:deprecated alias for –rate[0].

--max-requests=N:deprecated alias for –events[0].

--max-time=N:deprecated alias for –time[0].

--num-threads=N:deprecated alias for –threads[1].

Pseudo-Random Numbers Generator options

--rand-type=STRING:分布的随机数{uniform(均匀分布),Gaussian(高斯分布),special(空间分布)},默认是special。

--rand-spec-iter=N:产生数的迭代次数,默认是12。

--rand-spec-pct=N:值的百分比被视为’special’ (for special distribution),默认是1。

--rand-spec-res=N:’special’的百分比值,默认是75。

--rand-seed=N:随机数发生器的种子,值为0时,将当前时间用作rng种子,默认值[0]。

--rand-pareto-h=N:参数h用于pareto分布[0.2]。

Log options

--verbosity=N:日志详细级别{5-调试, 0-仅重要消息},默认为[3]。

--percentile=N:在延迟统计数据中计算的百分点 (1-100),使用特殊值0来禁用百分比计算[95]。

--histogram[=on|off]:在报告中打印滞后时间直方图,默认为[off]。

Compiled-in database drivers

mysql:MySQL驱动。

pgsql:PostgreSQL驱动。

mysql options

--mysql-host=[LIST,...]:MySQL服务器地址[localhost]。

--mysql-port=[LIST,...]:MySQL服务器端口[3306]。

--mysql-socket=[LIST,...]:MySQL服务器的socket地址。

--mysql-user=STRING:MySQL服务器的用户名。

--mysql-password=STRING:MySQL用户名密码。

--mysql-db=STRING:MySQL数据库名称[sbtest]。

--mysql-compression[=on|off]:是否使用压缩[off]。

pgsql options

--pgsql-host=[LIST,...]:PostgreSQL服务器地址[localhost]。

--pgsql-port=[LIST,...]:PostgreSQL服务器端口[5432]。

--pgsql-user=STRING:PostgreSQL服务器的用户名。

--pgsql-password=STRING:PostgreSQL用户名密码。

--pgsql-db=STRING:PostgreSQL数据库名称[sbtest]。

Compiled-in tests

fileio:文件I/O测试。

cpu:CPU性能测试。

memory:内存速率测试。

threads:线程子系统性能测试。

mutex:互斥锁性能测试。

三、Sysbench压测MySQL

Sysbench压测MySQL流程:prepare(准备数据) -> run(运行测试) -> cleanup(清理数据)

下面是目前社区提供的lua脚步(安装sysbench默认自带)

/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

以oltp_read_only.lua为例压测MySQL:

准备数据:

# sysbench /usr/share/sysbench/oltp_read_only.lua --mysql-host=192.168.10.94 --mysql-port=3307 --mysql-user=root --mysql-password='000000' --mysql-db=sbtest --db-driver=mysql --tables=10 --table-size=1000000 --report-interval=10 --threads=128 --time=120 prepare

压测:

# sysbench /usr/share/sysbench/oltp_read_only.lua --mysql-host=192.168.10.94 --mysql-port=3307 --mysql-user=root --mysql-password='000000' --mysql-db=sbtest --db-driver=mysql --tables=10 --table-size=1000000 --report-interval=10 --threads=128 --time=120 run

结果:

sysbench 1.0.9 (using system LuaJIT 2.0.4)

Running the test with following options:

Number of threads: 128

Report intermediate results every 10 second(s)

Initializing random number generator from current time

Initializing worker threads...

Threads started!

[ 10s ] thds: 128 tps: 154.50 qps: 2564.53 (r/w/o: 2244.70/0.00/319.83) lat (ms,95%): 1129.24 err/s: 0.00 reconn/s: 0.00

[ 20s ] thds: 128 tps: 194.87 qps: 3113.62 (r/w/o: 2723.98/0.00/389.64) lat (ms,95%): 1109.09 err/s: 0.00 reconn/s: 0.00

[ 30s ] thds: 128 tps: 199.90 qps: 3207.14 (r/w/o: 2807.33/0.00/399.80) lat (ms,95%): 926.33 err/s: 0.00 reconn/s: 0.00

[ 40s ] thds: 128 tps: 195.06 qps: 3125.39 (r/w/o: 2735.27/0.00/390.12) lat (ms,95%): 1050.76 err/s: 0.00 reconn/s: 0.00

[ 50s ] thds: 128 tps: 218.04 qps: 3477.01 (r/w/o: 3041.13/0.00/435.88) lat (ms,95%): 831.46 err/s: 0.00 reconn/s: 0.00

[ 60s ] thds: 128 tps: 213.20 qps: 3415.43 (r/w/o: 2988.84/0.00/426.59) lat (ms,95%): 893.56 err/s: 0.00 reconn/s: 0.00

[ 70s ] thds: 128 tps: 217.60 qps: 3485.39 (r/w/o: 3049.89/0.00/435.50) lat (ms,95%): 861.95 err/s: 0.00 reconn/s: 0.00

[ 80s ] thds: 128 tps: 214.42 qps: 3423.17 (r/w/o: 2994.74/0.00/428.43) lat (ms,95%): 893.56 err/s: 0.00 reconn/s: 0.00

[ 90s ] thds: 128 tps: 199.67 qps: 3200.28 (r/w/o: 2800.75/0.00/399.53) lat (ms,95%): 909.80 err/s: 0.00 reconn/s: 0.00

[ 100s ] thds: 128 tps: 144.34 qps: 2311.44 (r/w/o: 2022.65/0.00/288.79) lat (ms,95%): 2279.14 err/s: 0.00 reconn/s: 0.00

[ 110s ] thds: 128 tps: 190.31 qps: 3045.26 (r/w/o: 2664.82/0.00/380.44) lat (ms,95%): 1032.01 err/s: 0.00 reconn/s: 0.00

[ 120s ] thds: 128 tps: 249.47 qps: 3988.58 (r/w/o: 3489.98/0.00/498.60) lat (ms,95%): 909.80 err/s: 0.00 reconn/s: 0.00

SQL statistics:

    queries performed:

        read:                            336420    //总select数量

        write:                          0    //总update、insert、delete语句数量;

        other:                          48060    //commit、unlock tables以及其他mutex的数量;

        total:                          384480

    transactions:                        24030  (198.93 per sec.)    //TPS;

    queries:                            384480 (3182.80 per sec.)    //QPS;

    ignored errors:                      0      (0.00 per sec.)    //忽略的错误数;

    reconnects:                          0      (0.00 per sec.)

General statistics:

    total time:                          120.7959s    //即time指定的压测实际;

    total number of events:              24030    //总的事件数,一般与transactions相同;

Latency (ms):

        min:                                82.09    //最小响应时间;

        avg:                                641.06    //平均响应时间;

        max:                              14581.38    //最大响应时间;

        95th percentile:                  1013.60    //95%的语句的平均响应时间;

        sum:                            15404637.08    //总响应时间;

Threads fairness:

    events (avg/stddev):          187.7344/6.21

    execution time (avg/stddev):  120.3487/0.22

我们一般关注的指标主要有:

response time avg:平均响应时间(后面的95%的大小可以通过–percentile=98的方式去更改)。

transactions:精确的说是这一项后面的TPS,但如果使用了–skip-trx=on,这项事务数为0,需要用total number of events去除以总时间,得到tps(其实还可以分为读tps和写tps)。

queries:用它除以总时间,得到吞吐量QPS。

当然还有一些系统层面的cpu,io,mem相关指标。

清除数据:

# sysbench /usr/share/sysbench/oltp_read_only.lua --mysql-host=192.168.10.94 --mysql-port=3307 --mysql-user=root --mysql-password='000000' --mysql-db=sbtest --db-driver=mysql --tables=10 --table-size=1000000 --report-interval=10 --threads=128 --time=120 cleanup



实际示例:

sysbench /usr/share/sysbench/oltp_read_write.lua --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --mysql-password='z****8456' --mysql-db=sbtest --db-driver=mysql --tables=10 --table-size=100000 --report-interval=10 --threads=4 --time=3600 prepare
sysbench 1.0.9 (using system LuaJIT 2.0.4)

Initializing worker threads...

Creating table 'sbtest3'...
Creating table 'sbtest2'...Creating table 'sbtest1'...

Creating table 'sbtest4'...
Inserting 100000 records into 'sbtest4'
Inserting 100000 records into 'sbtest2'
Inserting 100000 records into 'sbtest3'
Inserting 100000 records into 'sbtest1'
Creating a secondary index on 'sbtest3'...
Creating a secondary index on 'sbtest2'...
Creating a secondary index on 'sbtest4'...
Creating table 'sbtest7'...
Creating a secondary index on 'sbtest1'...
Creating table 'sbtest6'...
Inserting 100000 records into 'sbtest7'
Inserting 100000 records into 'sbtest6'
Creating table 'sbtest8'...
Creating table 'sbtest5'...
Inserting 100000 records into 'sbtest8'
Inserting 100000 records into 'sbtest5'
Creating a secondary index on 'sbtest6'...
Creating table 'sbtest10'...
Creating a secondary index on 'sbtest7'...
Inserting 100000 records into 'sbtest10'
Creating a secondary index on 'sbtest8'...
Creating a secondary index on 'sbtest5'...
Creating table 'sbtest9'...
Inserting 100000 records into 'sbtest9'
Creating a secondary index on 'sbtest10'...
Creating a secondary index on 'sbtest9'...
sysbench /usr/share/sysbench/oltp_read_write.lua --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --mysql-password='z******56' --mysql-db=sbtest --db-driver=mysql --tables=10 --table-size=100000 --report-interval=10 --threads=4 --time=3600 run
sysbench 1.0.9 (using system LuaJIT 2.0.4)

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


Initializing worker threads...

Threads started!
show processlist;
+------+------+---------------------+----------------+---------+-------+----------+------------------------------------------------------------------------------------------------------+
| Id   | User | Host                | db             | Command | Time  | State    | Info                                                                                                 |
+------+------+---------------------+----------------+---------+-------+----------+------------------------------------------------------------------------------------------------------+
    | NULL                                                                                                 |
| 1440 | root | localhost           | NULL           | Sleep   |  4702 |          | NULL                                                                                                 |
| 1845 | root | localhost           | NULL           | Query   |     0 | init     | show processlist                                                                                     |
| 1851 | root | 127.0.0.1:51248     | sbtest         | Execute |     0 | update   | INSERT INTO sbtest5 (id, k, c, pad) VALUES (50374, 45438, '75070436389-27214864945-56233774250-65143 |
| 1852 | root | 127.0.0.1:51246     | sbtest         | Execute |     0 | update   | INSERT INTO sbtest6 (id, k, c, pad) VALUES (50468, 50426, '76218889497-54416747310-58929447688-63765 |
| 1853 | root | 127.0.0.1:51250     | sbtest         | Execute |     0 | updating | DELETE FROM sbtest6 WHERE id=49766                                                                   |
| 1854 | root | 127.0.0.1:51252     | sbtest         | Execute |     0 | updating | UPDATE sbtest10 SET c='71905504324-66806578758-49221258817-06632329054-91152507632-51828853433-94417 |
原文地址:https://www.cnblogs.com/weifeng1463/p/9574338.html