数据库测压工具Sysbench的安装和使用

一.下载安装

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

如果上面可以看到sysbench的版本号,就说明安装成功了。

二.使用
我们需要在自己的数据库里创建好一个测试库,我们可以取个名字叫做test_db,同时创建好对应的测试账号,可以叫做
test_user,密码也是test_user,让这个用户有权限可以访问test_db。
我们将要基于sysbench构建20个测试表,每个表里有100万条数据,接着使用10个并发线程去对这个数据库发起访问,
连续访问5分钟,也就是300秒,然后对其进行压力测试。

> sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=127.0.0.1 --mysql-
port=3306 --mysql-user=test_user --mysql-password=test_user --mysql-db=test_db --tables=20 --
table_size=1000000 oltp_read_write --db-ps-mode=disable prepare
–db-driver=mysql:mysql的驱动去连接mysql数据库,或者oracle,或者sqlserver。
–time=300:这个就是说连续访问300秒
–threads=10:这个就是说用10个线程模拟并发访问
–report-interval=1:这个就是说每隔1秒输出一下压测情况
–mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=test_user --mysql-password=test_user:数据库链接信息
–mysql-db=test_db --tables=20 --table_size=1000000:这一串的意思,就是说在test_db这个库里,构造20个测试
表,每个测试表里构造100万条测试数据,测试表的名字会是类似于sbtest1,sbtest2这个样子的
oltp_read_write:这个就是说,执行oltp数据库的读写测试
–db-ps-mode=disable:这个就是禁止ps模式
prepare:意思是参照这个命令的设置去构造出来我们需要的数据库里的数据,他会自动创建20个测试表,每个表里创建100万条测试数据

测试数据库的综合读写TPS,使用的是oltp_read_write模式(大家看命令中最后不是prepare,是run了,就是运行压测):

sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=127.0.0.1 --mysql-
port=3306 --mysql-user=test_user --mysql-password=test_user --mysql-db=test_db --tables=20 --
table_size=1000000 oltp_read_write --db-ps-mode=disable run


测试数据库的只读性能,使用的是oltp_read_only模式(大家看命令中的oltp_read_write已经变为oltp_read_only了):

sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=127.0.0.1 --mysql-
port=3306 --mysql-user=test_user --mysql-password=test_user --mysql-db=test_db --tables=20 --
table_size=1000000 oltp_read_only --db-ps-mode=disable run


测试数据库的删除性能,使用的是oltp_delete模式:

sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=127.0.0.1 --mysql-
port=3306 --mysql-user=test_user --mysql-password=test_user --mysql-db=test_db --tables=20 --
table_size=1000000 oltp_delete --db-ps-mode=disable run


测试数据库的更新索引字段的性能,使用的是oltp_update_index模式:

sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=127.0.0.1 --mysql-
port=3306 --mysql-user=test_user --mysql-password=test_user --mysql-db=test_db --tables=20 --
table_size=1000000 oltp_update_index --db-ps-mode=disable run


测试数据库的更新非索引字段的性能,使用的是oltp_update_non_index模式:

sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=127.0.0.1 --mysql-
port=3306 --mysql-user=test_user --mysql-password=test_user --mysql-db=test_db --tables=20 --
table_size=1000000 oltp_update_non_index --db-ps-mode=disable run


测试数据库的更新非索引字段的性能,使用的是oltp_update_non_index模式:

sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=127.0.0.1 --mysql-
port=3306 --mysql-user=test_user --mysql-password=test_user --mysql-db=test_db --tables=20 --
table_size=1000000 oltp_update_non_index --db-ps-mode=disable run


测试数据库的插入性能,使用的是oltp_insert模式:

sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=127.0.0.1 --mysql-
port=3306 --mysql-user=test_user --mysql-password=test_user --mysql-db=test_db --tables=20 --
table_size=1000000 oltp_insert --db-ps-mode=disable run


测试数据库的写入性能,使用的是oltp_write_only模式:

sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=127.0.0.1 --mysql-
port=3306 --mysql-user=test_user --mysql-password=test_user --mysql-db=test_db --tables=20 --
table_size=1000000 oltp_write_only --db-ps-mode=disable run


使用上面的命令,sysbench工具会根据你的指令构造出各种各样的SQL语句去更新或者查询你的20张测试表里的数据,同时
监测出你的数据库的压测性能指标,最后完成压测之后,可以执行下面的cleanup命令,清理数据。

sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=127.0.0.1 --mysql-
port=3306 --mysql-user=test_user --mysql-password=test_user --mysql-db=test_db --tables=20 --
table_size=1000000 oltp_read_write --db-ps-mode=disable cleanup

三.结果分析
按照我们上面的命令,我们是让他每隔1秒都会输出一次压测报告的,此时他每隔一秒会输出类似下面的一段东西:

[ 22s ] thds: 10 tps: 380.99 qps: 7312.66 (r/w/o: 5132.99/1155.86/1321.35) lat (ms, 95%): 21.33 err/s: 0.00 reconn/s:
0.00

我来给大家解释一下这是什么意思,首先他说的这是第22s输出的一段压测统计报告,然后是其他的一些统计字段:

thds: 10,这个意思就是有10个线程在压测
tps: 380.99,这个意思就是每秒执行了380.99个事务
qps: 7610.20,这个意思就是每秒可以执行7610.20个请求
(r/w/o: 5132.99/1155.86/1321.35),这个意思就是说,在每秒7610.20个请求中,有5132.99个请求是读请求,1155.86个请
求是写请求,1321.35个请求是其他的请求,就是对QPS进行了拆解
lat (ms, 95%): 21.33,这个意思就是说,95%的请求的延迟都在21.33毫秒以下
err/s: 0.00 reconn/s: 0.00,这两个的意思就是说,每秒有0个请求是失败的,发生了0次网络重连

这个压测结果会根据每个人的机器的性能不同有很大的差距,你要是机器性能特别高,那你可以开很多的并发线程去压测,比
如100个线程,此时可能会发现数据库每秒的TPS有上千个,如果你的机器性能很低,可能压测出来你的TPS才二三十个,QPS
才几百个,这都是有可能的。

另外在完成压测之后,最后会显示一个总的压测报告,我把解释写在下面了:

SQL statistics:
queries performed:
read: 1480084 // 这就是说在300s的压测期间执行了148万多次的读请求
write: 298457 // 这是说在压测期间执行了29万多次的写请求
other: 325436 // 这是说在压测期间执行了30万多次的其他请求
total: 2103977 // 这是说一共执行了210万多次的请求
// 这是说一共执行了10万多个事务,每秒执行350多个事务
transactions: 105180( 350.6 per sec. )
// 这是说一共执行了210万多次的请求,每秒执行7000+请求
queries: 2103977 ( 7013.26 per sec. )
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
// 下面就是说,一共执行了300s的压测,执行了10万+的事务
General staticstics:
total time: 300.0052s
total number of events: 105180
Latency (ms):
min: 4.32 // 请求中延迟最小的是4.32ms
avg: 13.42 // 所有请求平均延迟是13.42ms
max: 45.56 // 延迟最大的请求是45.56ms
95th percentile: 21.33 // 95%的请求延迟都在21.33ms以内

下边的内容参考自:数据库性能测试:sysbench用法详解 - 走看看

1.简介和安装

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

官方站点:https://github.com/akopytov/sysbench/

rpm包下载:el/7/sysbench-1.0.15-1.el7.centos.x86_64.rpm - akopytov/sysbench · packagecloud

源码包下载:https://github.com/akopytov/sysbench/archive/1.0.15.tar.gz

如果是编译安装,需要先安装好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

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值