关于java:最牛逼的数据库性能测试工具没有之一

3次阅读

共计 8432 个字符,预计需要花费 22 分钟才能阅读完成。

作者:骏马金龙 \
出处:https://www.cnblogs.com/f-ck-…

1. 简介和装置

sysbench 是一个很不错的数据库性能测试工具。

官方站点:

https://github.com/akopytov/s…

rpm 包下载:

https://packagecloud.io/akopy…

源码包下载:

https://github.com/akopytov/s…

如果是编译装置,须要先装置好 mysql 的开发包(只管编译谬误时提醒的是短少 Mysql 库文件)。

yum -y install mysql-community-devel

tar xf 1.0.15.tar.gz
cd sysbench-1.0.15
./autogen.sh
./configure
make -j
make install

装置后,只有一个二进制文件 sysbench,还提供了很多个 lua 脚本。

[root@s1 ~]# rpm -ql sysbench | grep 'bin\|lua'  
/usr/bin/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
/usr/share/sysbench/tests/include/inspect.lua
/usr/share/sysbench/tests/include/oltp_legacy/bulk_insert.lua
/usr/share/sysbench/tests/include/oltp_legacy/common.lua
/usr/share/sysbench/tests/include/oltp_legacy/delete.lua
/usr/share/sysbench/tests/include/oltp_legacy/insert.lua
/usr/share/sysbench/tests/include/oltp_legacy/oltp.lua
/usr/share/sysbench/tests/include/oltp_legacy/oltp_simple.lua
/usr/share/sysbench/tests/include/oltp_legacy/parallel_prepare.lua
/usr/share/sysbench/tests/include/oltp_legacy/select.lua
/usr/share/sysbench/tests/include/oltp_legacy/select_random_points.lua
/usr/share/sysbench/tests/include/oltp_legacy/select_random_ranges.lua
/usr/share/sysbench/tests/include/oltp_legacy/update_index.lua
/usr/share/sysbench/tests/include/oltp_legacy/update_non_index.lua

本文介绍的是新版本 sysbench oltp lua 脚本的用法(/usr/share/sysbench/*.lua),所以不波及传统的 lua(tests/include/oltp_legacy/*.lua),如果想要理解这些传统 Lua 脚本的用法,网上轻易找。

2.sysbench 应用办法

以下列出了 sysbench 对测试 mysql 时罕用的选项。

[root@xuexi ~]# sysbench --help
Usage:
  sysbench [options]... [test_lua] [lua_options] [command]

Commands implemented by most tests: prepare run cleanup help

通用选项:以下中括号中的值示意默认值
  --threads=N                     指定线程数[1]
  --events=N                      限度最大申请数,0 示意不限度[0]
  --time=N                        限度最长执行工夫,0 示意不限度[10]
                    --events 和 --time 二者选一即可
  --forced-shutdown=STRING        达到最长执行工夫后还需期待多久敞开 sysbench
                                  off 示意禁用该性能[off]
  --thread-stack-size=SIZE        每个线程应用的栈空间大小[64K]
  --rate=N                        均匀事务处理速率,0 示意不限度[0]
  --report-interval=N             每隔几秒报告一次后果,0 示意禁用距离报告[0]
  --config-file=FILENAME          从文件中读取命令行选项
  --tx-rate=N                     已废除,是 --rate 的别名[0]
  --max-requests=N                已废除,是 --events 的别名[0]
  --max-time=N                    已废除,是 --time 的别名[0]
  --num-threads=N                 已废除,是 --threads 的别名[1]
  --db-ps-mode=STRING             是否应用 prepare 模式的语句 {auto, disable} [auto]

mysql 相干选项:--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-ignore-errors=[LIST,...] 要疏忽的错误代码,值可为 "all" [1213,1020,1205]

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

其中,command 局部有 4 类:prepare run cleanup 和 help:

  • prepare:筹备数据的命令。例如,在 sysbench 压力测试之前,须要先筹备好测试库、测试表以及测试表中的数据。具体用法见后文。
  • run:示意进行压力测试。
  • cleanup:革除测试时产生的数据。
  • help:输入给定 lua 脚本的帮忙信息。

test_lua 是想要应用的 lua 脚本,如果是 rpm 包装置的 sysbench,则这些脚本都是 /usr/share/sysbench 目录下。对于个别的数据库测试,只需应用和 oltp 无关的 lua 脚本就足够。

options 和 lua_options 是不同的,options 是 sysbench 的选项,lua_options 是 lua 脚本的选项,lua_options 应该要放在 test_lua 的前面(非必须,但倡议)。

例如,要查看 oltp_common.lua 的用法,能够:

sysbench /usr/share/sysbench/oltp_common.lua help

3. 筹备测试数据

首先创立 sysbench 所需数据库sbtest(这是 sysbench 默认应用的库名,必须创立测试库)。

mysqladmin -h127.0.0.1 -uroot -pP@ssword1! -P3306 create sbtest;

而后,筹备测试所用的表,这些测试表放在测试库 sbtest 中。这里应用的 lua 脚本为/usr/share/sysbench/oltp_common.lua

sysbench --mysql-host=127.0.0.1 \
         --mysql-port=3306 \
         --mysql-user=root \
         --mysql-password=P@ssword1! \
         /usr/share/sysbench/oltp_common.lua \
         --tables=10 \
         --table_size=100000 \
         prepare

其中 --tables=10 示意创立 10 个测试表,--table_size=100000示意每个表中插入 10W 行数据,prepare示意这是筹备数的过程。

mysql> show tables from sbtest;
+------------------+
| Tables_in_sbtest |
+------------------+
| sbtest1          |
| sbtest10         |
| sbtest2          |
| sbtest3          |
| sbtest4          |
| sbtest5          |
| sbtest6          |
| sbtest7          |
| sbtest8          |
| sbtest9          |
+------------------+

mysql> select count(*) from sbtest.sbtest1;
+----------+
| count(*) |
+----------+
|   100000 |
+----------+

如果想要革除这 10 个表,可应用 cleanup 命令。

sysbench --mysql-host=127.0.0.1 \
         --mysql-port=3306 \
         --mysql-user=root \
         --mysql-password=P@ssword1! \
         /usr/share/sysbench/oltp_common.lua \
         --tables=10 \
         cleanup

4. 数据库测试和后果剖析

略微批改下之前筹备数据的语句,就能够拿来测试了。

须要留神的是,之前应用的 lua 脚本为oltp_common.lua,它是一个通用脚本,是被其它 lua 脚本调用的,它不能间接拿来测试。

所以,我这里用 oltp_read_write.lua 脚本来做读、写测试。还有很多其它类型的测试,比方只读测试、只写测试、删除测试、大批量插入测试等等。可找到对应的 lua 脚本进行调用即可。

sysbench --threads=4 \
         --time=20 \
         --report-interval=5 \
         --mysql-host=127.0.0.1 \
         --mysql-port=3306 \
         --mysql-user=root \
         --mysql-password=P@ssword1! \
         /usr/share/sysbench/oltp_read_write.lua \
         --tables=10 \
         --table_size=100000 \
         run

以下是测试返回的后果:

Initializing worker threads...

Threads started!


#### 以下是每 5 秒返回一次的后果,统计的指标包含:#### 线程数、tps(每秒事务数)、qps(每秒查问数)、#### 每秒的读 / 写 / 其它次数、提早、每秒谬误数、每秒重连次数
[5s] thds: 4 tps: 130.16 qps: 2606.30 (r/w/o: 1824.51/520.66/261.13) lat (ms,95%): 104.84 err/s: 0.00 reconn/s: 0.00
[10s] thds: 4 tps: 126.74 qps: 2539.17 (r/w/o: 1778.17/507.52/253.47) lat (ms,95%): 108.68 err/s: 0.00 reconn/s: 0.00
[15s] thds: 4 tps: 136.54 qps: 2736.34 (r/w/o: 1915.25/548.01/273.07) lat (ms,95%): 102.97 err/s: 0.00 reconn/s: 0.00
[20s] thds: 4 tps: 107.44 qps: 2148.65 (r/w/o: 1505.60/428.17/214.89) lat (ms,95%): 132.49 err/s: 0.00 reconn/s: 0.00

SQL statistics:
    queries performed:
        read:          35098   # 执行的读操作数量
        write:         10028   # 执行的写操作数量
        other:         5014    # 执行的其它操作数量
        total:         50140
    transactions:      2507   (124.29 per sec.)    # 执行事务的均匀速率
    queries:           50140  (2485.82 per sec.)   # 均匀每秒能执行多少次查问
    ignored errors:    0      (0.00 per sec.)
    reconnects:        0      (0.00 per sec.)

General statistics:
    total time:                  20.1694s     # 总耗费工夫
    total number of events:      2507         # 总申请数量(读、写、其它)

Latency (ms):
         min:                            2.32
         avg:                           32.13
         max:                          575.78
         95th percentile:              118.92    # 采样计算的均匀提早
         sum:                        80554.96

Threads fairness:
    events (avg/stddev):           626.7500/2.49
    execution time (avg/stddev):   20.1387/0.04

5.cpu/io/ 内存等测试

sysbench 内置了几个测试指标。

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

可间接 help 输入测试方法。例如,fileio 测试。

[root@xuexi ~]# sysbench fileio help
sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2)

fileio options:
  --file-num=N                  number of files to create [128]
  --file-block-size=N           block size to use in all IO operations [16384]
  --file-total-size=SIZE        total size of files to create [2G]
  --file-test-mode=STRING       test mode {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw}
  --file-io-mode=STRING         file operations mode {sync,async,mmap} [sync]
  --file-async-backlog=N        number of asynchronous operatons to queue per thread [128]
  --file-extra-flags=[LIST,...] list of additional flags to use to open files {sync,dsync,direct} []
  --file-fsync-freq=N           do fsync() after this number of requests (0 - don't use fsync()) [100]
  --file-fsync-all[=on|off]     do fsync() after each write operation [off]
  --file-fsync-end[=on|off]     do fsync() at the end of test [on]
  --file-fsync-mode=STRING      which method to use for synchronization {fsync, fdatasync} [fsync]
  --file-merged-requests=N      merge at most this number of IO requests if possible (0 - don't merge) [0]
  --file-rw-ratio=N             reads/writes ratio for combined test [1.5]

例如,创立 5 个文件,总共 1G,每个文件大略 200M。

sysbench fileio --file-num=5 --file-total-size=1G prepare

[root@xuexi ~]# ls -lh test*
-rw------- 1 root root 205M Jul  8 12:15 test_file.0
-rw------- 1 root root 205M Jul  8 12:15 test_file.1
-rw------- 1 root root 205M Jul  8 12:15 test_file.2
-rw------- 1 root root 205M Jul  8 12:15 test_file.3
-rw------- 1 root root 205M Jul  8 12:15 test_file.4

而后,运行测试。

sysbench --events=5000 \
         --threads=16 \
         fileio \
         --file-num=5 \
         --file-total-size=1G \
         --file-test-mode=rndrw \
         --file-fsync-freq=0 \
         --file-block-size=16384 \
         run

后果:

File operations:
    reads/s:                      98.67
    writes/s:                     66.85
    fsyncs/s:                     6.26

Throughput:     # 吞吐量
    read, MiB/s:                  1.54  # 示意读的带宽
    written, MiB/s:               1.04  # 示意读的带宽

General statistics:
    total time:                   12.7426s
    total number of events:       2117

Latency (ms):
         min:                          0.00
         avg:                         86.66
         max:                       2919.41
         95th percentile:            646.19
         sum:                     183460.80

Threads fairness:
    events (avg/stddev):          132.3125/24.19
    execution time (avg/stddev):  11.4663/1.09

再比例 cpu 性能测试:

[root@xuexi ~]# sysbench cpu --threads=40 --events=10000 --cpu-max-prime=20000 run
sysbench 1.0.15 (using bundled LuaJIT 2.1.0-beta2)

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


Prime numbers limit: 20000

Initializing worker threads...

Threads started!

CPU speed:
    events per second:  2127.81

General statistics:
    total time:                          4.6986s
    total number of events:              10000

Latency (ms):
         min:                                    1.72
         avg:                                   18.16
         max:                                  302.17
         95th percentile:                      110.66
         sum:                               181628.49

Threads fairness:
    events (avg/stddev):           250.0000/30.81
    execution time (avg/stddev):   4.5407/0.10

近期热文举荐:

1.600+ 道 Java 面试题及答案整顿(2021 最新版)

2. 终于靠开源我的项目弄到 IntelliJ IDEA 激活码了,真香!

3. 阿里 Mock 工具正式开源,干掉市面上所有 Mock 工具!

4.Spring Cloud 2020.0.0 正式公布,全新颠覆性版本!

5.《Java 开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞 + 转发哦!

正文完
 0