sysbench使用与mysql性能测试

Posted 境悟初

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sysbench使用与mysql性能测试相关的知识,希望对你有一定的参考价值。

使用sysbench

https://github.com/akopytov/sysbench

基本用法

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

Commands implemented by most tests: prepare run cleanup help

General options:
  --threads=N                     number of threads to use [1]
  --events=N                      limit for total number of events [0]
  --time=N                        limit for total execution time in seconds [10]
  --forced-shutdown=STRING        number of seconds to wait after the --time limit before forcing shutdown, or 'off' to disable [off]
  --thread-stack-size=SIZE        size of stack per thread [64K]
  --rate=N                        average transactions rate. 0 for unlimited rate [0]
  --report-interval=N             periodically report intermediate statistics with a specified interval in seconds. 0 disables intermediate reports [0]
  --report-checkpoints=[LIST,...] dump full statistics and reset all counters at specified points in time. The argument is a list of comma-separated values representing the amount of time in seconds elapsed from start of test when report checkpoint(s) must be performed. Report checkpoints are off by default. []
  --debug[=on|off]                print more debugging info [off]
  --validate[=on|off]             perform validation checks where possible [off]
  --help[=on|off]                 print help and exit [off]
  --version[=on|off]              print version and exit [off]
  --config-file=FILENAME          File containing command line options
  --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 random numbers distribution {uniform,gaussian,special,pareto} [special]
  --rand-spec-iter=N number of iterations used for numbers generation [12]
  --rand-spec-pct=N  percentage of values to be treated as 'special' (for special distribution) [1]
  --rand-spec-res=N  percentage of 'special' values to use (for special distribution) [75]
  --rand-seed=N      seed for random number generator. When 0, the current time is used as a RNG seed. [0]
  --rand-pareto-h=N  parameter h for pareto distribution [0.2]

Log options:
  --verbosity=N verbosity level {5 - debug, 0 - only critical messages} [3]

  --percentile=N       percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95]
  --histogram[=on|off] print latency histogram in report [off]

General database options:

  --db-driver=STRING  specifies database driver to use ('help' to get list of available drivers) [mysql]
  --db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]
  --db-debug[=on|off] print database-specific debug information [off]


Compiled-in database drivers:
  mysql - MySQL driver
  pgsql - PostgreSQL driver

mysql options:
  --mysql-host=[LIST,...]          MySQL server host [localhost]
  --mysql-port=[LIST,...]          MySQL server port [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-ssl[=on|off]             use SSL connections, if available in the client library [off]
  --mysql-ssl-cipher=STRING        use specific cipher for SSL connections []
  --mysql-compression[=on|off]     use compression, if available in the client library [off]
  --mysql-debug[=on|off]           trace all client library calls [off]
  --mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205]
  --mysql-dry-run[=on|off]         Dry run, pretend that all MySQL client API calls are successful without executing them [off]

pgsql options:
  --pgsql-host=STRING     PostgreSQL server host [localhost]
  --pgsql-port=N          PostgreSQL server port [5432]
  --pgsql-user=STRING     PostgreSQL user [sbtest]
  --pgsql-password=STRING PostgreSQL password []
  --pgsql-db=STRING       PostgreSQL database name [sbtest]

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

See 'sysbench <testname> help' for a list of options for each test.

OLTP测试

查看OLTP相关的脚本

# ll /usr/share/sysbench
total 64
-rwxr-xr-x 1 root root  1452 Apr 25  2020 bulk_insert.lua
-rw-r--r-- 1 root root 14369 Apr 25  2020 oltp_common.lua
-rwxr-xr-x 1 root root  1290 Apr 25  2020 oltp_delete.lua
-rwxr-xr-x 1 root root  2415 Apr 25  2020 oltp_insert.lua
-rwxr-xr-x 1 root root  1265 Apr 25  2020 oltp_point_select.lua
-rwxr-xr-x 1 root root  1649 Apr 25  2020 oltp_read_only.lua
-rwxr-xr-x 1 root root  1824 Apr 25  2020 oltp_read_write.lua
-rwxr-xr-x 1 root root  1118 Apr 25  2020 oltp_update_index.lua
-rwxr-xr-x 1 root root  1127 Apr 25  2020 oltp_update_non_index.lua
-rwxr-xr-x 1 root root  1440 Apr 25  2020 oltp_write_only.lua
-rwxr-xr-x 1 root root  1919 Apr 25  2020 select_random_points.lua
-rwxr-xr-x 1 root root  2118 Apr 25  2020 select_random_ranges.lua
drwxr-xr-x 4 root root  4096 Oct 23 16:34 tests

查看某个脚本的用法

# sysbench oltp_read_only.lua help
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

oltp_read_only.lua options:
  --auto_inc[=on|off]           Use AUTO_INCREMENT column as Primary Key (for MySQL), or its alternatives in other DBMS. When disabled, use client-generated IDs [on]
  --create_secondary[=on|off]   Create a secondary index in addition to the PRIMARY KEY [on]
  --delete_inserts=N            Number of DELETE/INSERT combinations per transaction [1]
  --distinct_ranges=N           Number of SELECT DISTINCT queries per transaction [1]
  --index_updates=N             Number of UPDATE index queries per transaction [1]
  --mysql_storage_engine=STRING Storage engine, if MySQL is used [innodb]
  --non_index_updates=N         Number of UPDATE non-index queries per transaction [1]
  --order_ranges=N              Number of SELECT ORDER BY queries per transaction [1]
  --pgsql_variant=STRING        Use this PostgreSQL variant when running with the PostgreSQL driver. The only currently supported variant is 'redshift'. When enabled, create_secondary is automatically disabled, and delete_inserts is set to 0
  --point_selects=N             Number of point SELECT queries per transaction [10]
  --range_selects[=on|off]      Enable/disable all range SELECT queries [on]
  --range_size=N                Range size for range SELECT queries [100]
  --secondary[=on|off]          Use a secondary index in place of the PRIMARY KEY [off]
  --simple_ranges=N             Number of simple range SELECT queries per transaction [1]
  --skip_trx[=on|off]           Don't start explicit transactions and execute all queries in the AUTOCOMMIT mode [off]
  --sum_ranges=N                Number of SELECT SUM() queries per transaction [1]
  --table_size=N                Number of rows per table [10000]
  --tables=N                    Number of tables [1]

创建测试数据库

create schema oltp_test_db;

然后运行命令准备测试数据

$ sysbench --db-driver=mysql --mysql-user=root --mysql-password=xxx\\!e \\
  --mysql-db=oltp_test_db --range_size=100 \\
  --table_size=1000 --tables=2 --threads=1 --events=0 --time=60 \\
  --rand-type=uniform /usr/share/sysbench/oltp_read_only.lua prepare

/sysbench/oltp_read_only.lua prepare
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Creating table 'sbtest1'...
Inserting 1000 records into 'sbtest1'
Creating a secondary index on 'sbtest1'...
Creating table 'sbtest2'...
Inserting 1000 records into 'sbtest2'
Creating a secondary index on 'sbtest2'...

查看数据

mysql> show tables;
+------------------------+
| Tables_in_oltp_test_db |
+------------------------+
| sbtest1                |
| sbtest2                |
+------------------------+
2 rows in set (0.00 sec)

mysql> select * from sbtest1 limit 1;
+----+-----+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| id | k   | c                                                                                                                       | pad                                                         |
+----+-----+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
|  1 | 429 | 83868641912-28773972837-60736120486-75162659906-27563526494-20381887404-41576422241-93426793964-56405065102-33518432330 | 67847967377-48000963322-62604785301-91415491898-96926520291 |
+----+-----+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from sbtest2 limit 1;
+----+-----+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| id | k   | c                                                                                                                       | pad                                                         |
+----+-----+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
|  1 | 904 | 17995306333-37142896538-49380301242-28196013457-87893486508-68580578060-80994653574-69477183705-07249821405-21023566633 | 15521214518-88383453756-15377829850-16568473946-86487734024 |
+----+-----+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select count(1) from sbtest2 limit 1;
+----------+
| count(1) |
+----------+
|     1000 |
+----------+
1 row in set (0.00 sec)

接着运行测试

我们将命令换成run来运行测试:

$ sysbench --db-driver=mysql --mysql-user=root --mysql-password=xxx\\!e \\
  --mysql-db=oltp_test_db --range_size=100 \\
  --table_size=1000 --tables=2 --threads=1 --events=0 --time=60 \\
  --rand-type=uniform /usr/share/sysbench/oltp_read_only.lua 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


Initializing worker threads...

Threads started!

SQL statistics:
    queries performed:
        read:                            919464
        write:                           0
        other:                           131352
        total:                           1050816
    transactions:                        65676  (1094.55 per sec.)
    queries:                             1050816 (17512.82 per sec.)
    ignored errors:                      0      (0.00 per sec.)
    reconnects:                          0      (0.00 per sec.)

General statistics:
    total time:                          60.0011s
    total number of events:              65676

Latency (ms):
         min:                                    0.70
         avg:                                    0.91
         max:                                    2.37
         95th percentile:                        1.01
         sum:                                59942.96

Threads fairness:
    events (avg/stddev):           65676.0000/0.00
    execution time (avg/stddev):   59.9430/0.00

最后清理数据

$ sysbench --db-driver=mysql --mysql-user=root --mysql-password=xxx\\!e \\
  --mysql-db=oltp_test_db --tables=2 /usr/share/sysbench/oltp_common.lua cleanup

Dropping table 'sbtest1'...
Dropping table 'sbtest2'...  

继续

上面只用了读测试,还有写测试、读写测试,增删改测试。

以上是关于sysbench使用与mysql性能测试的主要内容,如果未能解决你的问题,请参考以下文章

sysbench压测Oracle(安装与使用示例)

MySQL压测使用sysbench对MySQL进行压力测试

Mysql 性能测试工具 sysbench的安装和使用

使用sysbench对mysql压力测试

MySQL性能测试工具sysbench的安装和使用

Mysql多线程性能测试工具sysbench 安装使用和测试