sysbench的基本用法和结果绘图

sysbench简介

sysbench是一个基于LuaJIT的可编写脚本的多线程基准测试工具。它最常用于数据库基准测试,但也可用于创建不涉及数据库服务器的任意复杂工作负载。

本文来研究一下sysbench的安装使用方法,以及测试结果的绘图方法。

安装部署

参考Sysbench环境搭建sysbench项目,进行sysbench的安装部署。

安装mysql

1、安装mysql数据库。
sudo apt-get install mysql-server mysql-client

接下来三步是可选操作。

2、sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf,修改绑定IP

1
bind-address            = 0.0.0.0

3、重启mysql
sudo service mysql restart

4、测试登录
mysql -u root -h <hostip> -p

安装sysbench

1、安装sysbench

1
2
curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.deb.sh | sudo bash
sudo apt -y install sysbench

务必先下载脚本并执行,否则直接apt install安装的sysbench版本太低,不支持report-interval参数。

2、mysql -u root -p,创建数据库

1
create database sbtest;

3、验证安装
sysbench --version

sysbench指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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.

sysbench实践

参考How to Benchmark Your System (CPU, File IO, MySQL) with Sysbench使用sysbench对MySQL进行压力测试进行性能测试。

CPU

使用以下命令测试CPU性能:
sysbench cpu --cpu-max-prime=20000 run
total time越小,说明CPU性能越好。

Mysql

1、创建测试数据

1
2
3
4
5
6
7
8
9
10
11
12
sysbench /usr/share/sysbench/oltp_read_only.lua \
--db-driver=mysql \
--mysql-host=127.0.0.1 \
--mysql-db=sbtest \
--mysql-user=root \
--mysql-password=voidking \
--mysql-socket=/var/run/mysqld/mysqld.sock \
--tables=10 \
--table-size=100000 \
--threads=8 \
--events=100000 \
prepare

2、测试

1
2
3
4
5
6
7
8
9
10
11
12
time sysbench /usr/share/sysbench/oltp_read_only.lua \
--db-driver=mysql \
--mysql-host=127.0.0.1 \
--mysql-db=sbtest \
--mysql-user=root \
--mysql-password=voidking \
--mysql-socket=/var/run/mysqld/mysqld.sock \
--tables=10 \
--table-size=100000 \
--threads=8 \
--events=100000 \
run

每秒的transactions越高,说明性能越好。

3、测试命令2

1
2
3
4
5
6
7
8
9
10
11
12
13
time sysbench /usr/share/sysbench/oltp_read_only.lua \
--db-driver=mysql \
--mysql-host=127.0.0.1 \
--mysql-db=sbtest \
--mysql-user=root \
--mysql-password=voidking \
--mysql-socket=/var/run/mysqld/mysqld.sock \
--tables=10 \
--table-size=100000 \
--threads=8 \
--events=0 \
--time=30 \
run

4、清理数据(可选)

1
2
3
4
5
6
7
8
9
10
11
12
13
time sysbench /usr/share/sysbench/oltp_read_only.lua \
--db-driver=mysql \
--mysql-host=127.0.0.1 \
--mysql-db=sbtest \
--mysql-user=root \
--mysql-password=voidking \
--mysql-socket=/var/run/mysqld/mysqld.sock \
--tables=10 \
--table-size=100000 \
--threads=8 \
--events=0 \
--time=30 \
cleanup

绘图

sysbench统计数据

测试搞定了,数据呢?每秒钟的transactions数据呢?关键在于report-interval参数。修改测试命令为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sysbench /usr/share/sysbench/oltp_read_only.lua \
--db-driver=mysql \
--mysql-host=127.0.0.1 \
--mysql-db=sbtest \
--mysql-user=root \
--mysql-password=voidking \
--mysql-socket=/var/run/mysqld/mysqld.sock \
--tables=10 \
--table-size=100000 \
--threads=8 \
--events=0 \
--time=30 \
--report-interval=5 \
run >> sysbench.log

sysbench.log内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
sysbench 1.0.16 (using bundled LuaJIT 2.1.0-beta2)

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


Initializing worker threads...

Threads started!

[ 5s ] thds: 8 tps: 792.49 qps: 12698.90 (r/w/o: 11112.31/0.00/1586.59) lat (ms,95%): 11.24 err/s: 0.00 reconn/s: 0.00
[ 10s ] thds: 8 tps: 782.20 qps: 12511.60 (r/w/o: 10947.20/0.00/1564.40) lat (ms,95%): 11.04 err/s: 0.00 reconn/s: 0.00
[ 15s ] thds: 8 tps: 784.80 qps: 12561.53 (r/w/o: 10991.94/0.00/1569.59) lat (ms,95%): 11.04 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 8 tps: 808.85 qps: 12935.60 (r/w/o: 11317.90/0.00/1617.70) lat (ms,95%): 10.84 err/s: 0.00 reconn/s: 0.00
[ 25s ] thds: 8 tps: 795.95 qps: 12737.76 (r/w/o: 11145.86/0.00/1591.89) lat (ms,95%): 10.84 err/s: 0.00 reconn/s: 0.00
[ 30s ] thds: 8 tps: 816.80 qps: 13065.00 (r/w/o: 11431.60/0.00/1633.40) lat (ms,95%): 10.65 err/s: 0.00 reconn/s: 0.00
SQL statistics:
queries performed:
read: 334810
write: 0
other: 47830
total: 382640
transactions: 23915 (796.79 per sec.)
queries: 382640 (12748.68 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)

General statistics:
total time: 30.0123s
total number of events: 23915

Latency (ms):
min: 7.65
avg: 10.04
max: 32.13
95th percentile: 10.84
sum: 240031.54

Threads fairness:
events (avg/stddev): 2989.3750/3.74
execution time (avg/stddev): 30.0039/0.00

然后把可以绘图的数据单独提取出来:
cat sysbench.log | grep tps > sysbench.dat

gnuplot绘图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# graph title
set title ""
set key below box 1

# x-axis label
set xlabel "time(ms)"

# y-axis label
set ylabel "queries per second"
set yrange[10000:15000]
set ytics 500

plot "sysbench.dat" using 2:9 with linespoints title "sysbench0",\

pause mouse

后记

至此,sysbench的使用方法和绘图方法就基本掌握了。更详细和高阶的用法,需要的时候再去学习。

0%