【无标题】

转发~~~
原文:
https://blog.csdn.net/qq_39208536/article/details/119672946
参考博客:
sysbench - 老僧观天下 - 博客园

sysbench 压测 详解_俗人linux_51CTO博客

sysbench工具使用 - ZhenXing_Yu - 博客园 |QQ:327488733@qq.com

sysbench使用指南 - AhaoMu - 博客园

MySql性能测试工具-sysbench_oahz4699092zhao的专栏-CSDN博客_sysbench

sysbench是一个非常经典的综合性能测试工具,它支持CPU,IO,内存,尤其是数据库的性能测试。

1、sysbench安装
安装:

sysbench-1.0:

wget https://github.com/akopytov/sysbench/archive/1.0.zip -O “sysbench-1.0.zip”
下载之后的名字是1.0.zip。

报错处理:

如果报错,无法建立 SSL 连接。

把https 换成http就好了,如果没报错,请忽略这一段。

解压

unzip sysbench-1.0.zip

进入文件

cd sysbench-1.0
1.安装依赖库

yum install automake libtool -y
2.开始安装sysbench

[root@iz2ze2w3v37sit3vf71kuez sysbench-1.0]# ./autogen.sh
[root@iz2ze2w3v37sit3vf71kuez sysbench-1.0]# ./configure
注意:

执行./configure,我这里报错了。

报错信息:

configure: error: mysql_config executable not found


ERROR: cannot find MySQL libraries. If you want to compile with MySQL support,
please install the package containing MySQL client libraries and headers.
On Debian-based systems the package name is libmysqlclient-dev.
On RedHat-based systems, it is mysql-devel.
If you have those libraries installed in non-standard locations,
you must either specify file locations explicitly using
–with-mysql-includes and --with-mysql-libs options, or make sure path to
mysql_config is listed in your PATH environment variable. If you want to
disable MySQL support, use --without-mysql option.
#ERROR: cannot find MySQL libraries. If you want to compile with MySQL support

没找到mysql库 需要用参数指定下 --with-mysql-includes和–with-mysql-libs

这里需要指定你mysql安装的路径

shell> ./configure --with-mysql-includes=/alidata/server/mysql5.7/include/ --with-mysql-libs=/alidata/server/mysql5.7/lib/
我刚刚试了上面的方法,依然在报错

[root@iz2ze2w3v37sit3vf71kuez sysbench-1.0]# ./configure --with-mysql-includes=/var/lib/mysql/mysql --with-mysql-libs=/var/lib/mysql/mysql/
报错信息:

checking for library containing mysql_real_connect… no
configure: error: cannot find MySQL client libraries in /var/lib/mysql/mysql/

这里依然没找到mysql的这个文件,可能是我记错了mysql的路径了。

放弃依赖mysql

因为我使用sysbench单纯的做linux压测,不压mysql。所以我们此时安装,放弃依赖mysql

[root@iz2ze2w3v37sit3vf71kuez sysbench-1.0]# ./configure --without-mysql
然后执行make和make install

[root@iz2ze2w3v37sit3vf71kuez sysbench-1.0]# make
[root@iz2ze2w3v37sit3vf71kuez sysbench-1.0]# make install
[root@iz2ze2w3v37sit3vf71kuez sysbench-1.0]# sysbench --version
sysbench 1.0.20
这样就安装成功了。

2、sysbench介绍:
sysbench是一个开源的、模块化的、跨平台的多线程性能测试工具,可以用来进行CPU、内存、磁盘I/O、线程、数据库的性能测试。

目前支持的数据库有MySQL、Oracle和PostgreSQL。

当前功能允许测试的系统参数有:

CPU运算性能

file I/O performance (磁盘IO性能)

scheduler performance (调度程序性能)

memory allocation and transfer speed (内存分配及传输速度)

POSIX threads implementation performance (POSIX线程性能)

database server performance (OLTP benchmark) (数据库服务器性能,OLTP基准测试)

3、使用说明
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],创建测试线程的数目。默认为1.
–events=N limit for total number of events [0],请求的最大数目,0代表不限制。
–time=N limit for total execution time in seconds [10],最大执行时间,单位是s。默认是10s
–forced-shutdown=STRING number of seconds to wait after the --time limit before forcing shutdown, or ‘off’ to disable [off],超过max-time强制中断。默认是off。
–thread-stack-size=SIZE size of stack per thread [64K],每个线程的堆栈大小。默认是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],是否显示更多的调试信息。默认是off。
–validate[=on|off] perform validation checks where possible [off],#在可能情况下执行验证检查。默认是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],日志级别,默认为3,5=debug,0=只包含重要信息

–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 help’ for a list of options for each test.

– 查看总体帮助文档

#sysbench --help
– 查测试cpu的帮助文档

#sysbench --test=cpu help
–cpu-max-prime=N 最大质数发生器数量。默认是10000

– 查看IO测试的帮助文档

[root@iz2ze2w3v37sit3vf71kuez ~]# sysbench --test=fileio help
WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
sysbench 1.0.17 (using system LuaJIT 2.0.4)

fileio options:
–file-num=N number of files to create [128],创建测试文件的数量。默认是128
–file-block-size=N block size to use in all IO operations [16384],测试时文件块的大小。默认是16384(16K)
–file-total-size=SIZE total size of files to create [2G],测试文件的总大小。默认是2G
–file-test-mode=STRING 文件测试模式{seqwr(顺序写), seqrewr(顺序读写), seqrd(顺序读), rndrd(随机读), rndwr(随机写), rndrw(随机读写)}
–file-io-mode=STRING 文件操作模式{sync(同步),async(异步),fastmmap(快速map映射),slowmmap(慢map映射)}。默认是sync
–file-async-backlog=N number of asynchronous operatons to queue per thread [128]
–file-extra-flags=[LIST,…] 使用额外的标志来打开文件{sync,dsync,direct} 。默认为空
–file-fsync-freq=N 执行fsync()的频率。(0 – 不使用fsync())。默认是100
–file-fsync-all[=on|off] 每执行完一次写操作就执行一次fsync。默认是off
–file-fsync-end[=on|off] 在测试结束时才执行fsync。默认是on
–file-fsync-mode=STRING 使用哪种方法进行同步{fsync, fdatasync}。默认是fsync
–file-merged-requests=N 如果可以,合并最多的IO请求数(0 – 表示不合并)。默认是0
–file-rw-ratio=N 测试时的读写比例,默认时为1.5,即可3:2。
– 查看测试内存的帮助文档

#sysbench --test=memory help

[root@iz2ze2w3v37sit3vf71kuez ~]# sysbench --test=memory help
WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
sysbench 1.0.17 (using system LuaJIT 2.0.4)

memory options:
–memory-block-size=SIZE 测试时内存块大小。默认是1K
–memory-total-size=SIZE 传输数据的总大小。默认是100G
–memory-scope=STRING 内存访问范围{global,local}。默认是global
–memory-hugetlb=[on|off] 从HugeTLB池内存分配。默认是off
–memory-oper=STRING 内存操作类型。{read, write, none} 默认是write
–memory-access-mode=STRING存储器存取方式{seq,rnd} 默认是seq
– 查看测试线程的帮助文档

#sysbench threads help

[root@iz2ze2w3v37sit3vf71kuez ~]# sysbench threads help
sysbench 1.0.17 (using system LuaJIT 2.0.4)

threads options:
–thread-yields=N number of yields to do per request [1000],每个请求产生多少个线程。默认是1000
–thread-locks=N number of locks per thread [8],每个线程的锁的数量。默认是8
– 查看mutex的帮助文档

#sysbench mutex help

[root@iz2ze2w3v37sit3vf71kuez ~]# sysbench mutex help
sysbench 1.0.17 (using system LuaJIT 2.0.4)

mutex options:
–mutex-num=N total size of mutex array [4096],数组互斥的总大小。默认是4096
–mutex-locks=N number of mutex locks to do per thread [50000],每个线程互斥锁的数量。默认是50000
–mutex-loops=N number of empty loops to do outside mutex lock [10000],内部互斥锁的空循环数量。默认是10000
root@db2:~# sysbench

Missing required command argument.

Usage: #使用方法

sysbench的基本命令格式为:

sysbench –test=< test-name> [options]… < command>

sysbench [general-options]… --test= [test-options]… command

General options: #通用选项

–num-threads=N number of threads to use [1] #创建测试线程的数目。默认为1.

–max-requests=N limit for total number of requests [10000] #请求的最大数目。默认为10000,0代表不限制。

–max-time=N limit for total execution time in seconds [0] #最大执行时间,单位是s。默认是0,不限制。

–forced-shutdown=STRING amount of time to wait after --max-time before forcing shutdown [off] #超过max-time强制中断。默认是off。

–thread-stack-size=SIZE size of stack per thread [32K] #每个线程的堆栈大小。默认是32K。

–init-rng=[on|off] initialize random number generator [off] #在测试开始时是否初始化随机数发生器。默认是off。

–test=STRING test to run #指定测试类型。

fileio

cpu

memory

threads

mutex

–debug=[on|off] print more debugging info [off] #是否显示更多的调试信息。默认是off。 --validate=[on|off] perform validation checks where possible [off] #在可能情况下执行验证检查。默认是off。

–help=[on|off] print help and exit #帮助信息。 --version=[on|off] print version and exit #版本信息。

–report-interval --指定每多少秒在屏幕上输出一次结果

–日志选项

–verbosity=N --日志级别,默认为3,5=debug,0=只包含重要信息

Compiled-in tests: #测试项目

fileio - File I/O test #IO

cpu - CPU performance test #CPU

memory - Memory functions speed test #内存

threads - Threads subsystem performance test #线程

mutex - Mutex performance test #互斥性能测试

oltp - OLTP test # 数据库,事务处理

Commands:

测试的步骤

prepare:测试前准备工作(生成测试需要的数据);

run:进行性能测试

cleanup:测试后删掉测试数据

help version

See ‘sysbench --test= help’ for a list of options for each test. #查看每个测试项目的更多选项列表

二、压测实战
2.1 cpu性能测试
常用参数

–cpu-max-prime: 素数生成数量的上限
–threads: 线程数
–time: 运行时长,单位秒
–events: event上限次数
结果分析

[root@iz2ze2w3v37sit3vf71kuez ~]# sysbench cpu --cpu-max-prime=20000 --threads=2 run
sysbench 1.0.17 (using system LuaJIT 2.0.4)

Running the test with following options:
Number of threads: 2//线程数为2
Initializing random number generator from current time

Prime numbers limit: 20000//2万个素数

Initializing worker threads…

Threads started!

CPU speed:
events per second: 303.63

General statistics:
total time: 10.0040s //花费10s
total number of events: 3038 //10秒内所有线程一共完成了3038次event

Latency (ms):
min: 2.78 //最小耗时2.78ms
avg: 6.57 //平均耗时6.57ms
max: 43.05 //最大耗时43.05ms
95th percentile: 26.20 //95%在26.20ms完成
sum: 19968.27

Threads fairness:
events (avg/stddev): 1519.0000/2.00
execution time (avg/stddev): 9.9841/0.00
2.2 磁盘IO性能测试
#准备数据

sysbench --test=fileio --file-total-size=2G prepare
#运行测试

WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
WARNING: --max-time is deprecated, use --time instead
sysbench 1.0.18 (using bundled LuaJIT 2.1.0-beta2)

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

Extra file open flags: (none)
128 files, 16MiB each
2GiB total file size
Block size 16KiB
Number of IO requests: 0
Read/Write ratio for combined random IO test: 1.50
Periodic FSYNC enabled, calling fsync() each 100 requests.
Calling fsync() at the end of test, Enabled.
Using synchronous I/O mode
Doing random r/w test
Initializing worker threads…

Threads started!

File operations:
reads/s: 612.68
writes/s: 408.44
fsyncs/s: 1307.77

Throughput:
read, MiB/s: 9.57 //读吞吐
written, MiB/s: 6.38 //写吞吐

General statistics:
total time: 30.0436s
total number of events: 69851

Latency (ms):
min: 0.00
avg: 0.43
max: 213.95
95th percentile: 1.76
sum: 29921.46

Threads fairness:
events (avg/stddev): 69851.0000/0.00
execution time (avg/stddev): 29.9215/0.00

#清理数据

sysbench --test=fileio --file-total-size=2G cleanup
我们再来一次,压测IO。然后使用top命令查看服务器状态。

我们再复习一下top展示含义

0.7 us

用户进程消耗的cpu,一般小于70%/80%是正常的

1.0 sy

系统内核进程消耗的cpu(操作系统的底层资源是在内核完成的,比如读写磁盘。超过10%就很高了,可能是IO、中断、上下文切换导致的

0.0 ni

改变过优先级的进程占用CPU的百分比

98.3 id

空闲cpu百分比

0.0 wa

等待IO的进程所消耗的占CPU的百分比,直观说就是 如果这个值很高 就代表 cpu利用率不高 但是io很繁忙,需要去查一下io

0.0 hi

硬件中断所消耗的CPU占比(硬中断 Hardware IRQ)

0.0 si

软件中断所消耗的CPU占比(软中断 Software Interrupts)

通过分析得知

负载处于正常值

cpu行,us 正常,sy 8.1有一点点大 wa 为90.2%,这个值非常大。

wa的值非常高,等待IO的进程所消耗的占CPU的百分比,直观说就是 如果这个值很高 就代表 cpu利用率不高 但是io很繁忙,需要去查一下io。

所以,这里断定是io导致wa的值非常高。

下一步我们去查看,哪个进程,在大量的io

iostat -x
avg-cpu: %user %nice %system %iowait %steal %idle
1.27 0.00 8.40 90.33 0.00 0.00

Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
vda 0.00 140.46 1073.03 2497.46 57397.46 10141.48 37.83 1.71 0.48 0.73 0.37 0.27 96.51
字段释意:

avgqu-sz:
平均i/o队列长度,建议不超过1
svctm:
磁盘本身(磁盘读写)的耗时<4ms。平均每次设备的IO操作的服务时间(毫秒),即delta(use)/delta(rio+wio)
await:
操作系统每次IO的平均耗时,等待磁盘读写时间+svctm之和,单位是毫秒。
一般情况下await大于svctm,他们的差值越小,则说明队列越短。反之,差值越大,队列越长,说明系统出了问题。
%util:
磁盘繁忙程度。i/O请求占cpu的百分比,比率越大,说明越饱和。当%util达到1时,说明设备带宽已经被占满。
rrqm/s:
每秒进行merge的读操作次数。即 rmerge/s
wrqm/s:
每秒进行merge的写操作次数。即 wmerge/s
r/s:
每秒完成的读 I/O 设备次数。即 rio/s
w/s:
每秒完成的写 I/O 设备次数。即 wio/s
rkB/s:
每秒读K字节数。是 rsect/s 的一半,因为每扇区大小为512字节。
wkB/s:
每秒写K字节数。是 wsect/s 的一半。
avgrq-sz:
平均每次设备I/O操作的数据大小 (扇区)。
%iowait: CPU等待输入输出完成时间的百分比。

%util: 磁盘繁忙程度。i/O请求占cpu的百分比,比率越大,说明越饱和。当%util达到1时,说明设备带宽已经被占满。

%iowait 90.33

%util 96.51

根据这两个值,断定磁盘在大量io。

到底是在读呢,还是在写呢,根据rkB/s与wkB/s判断。

通过rkB/s与wkB/s可以判断出,如果io很高的话,到底是读导致的还是写导致的。如果是读导致的,应该是内存不足。如果写占比大,说明某个应用程序在写。此时可以查看哪个程序在写东西,(写文件或者写日志)

rkB/s :57397.46

wkB/s :10141.48

我们对比一下平时磁盘的,io情况。

相比,我们此时读和写都很大。

确认了IO负载过高后,可以使用iotop工具具体查看IO负载主要是落在哪个进程上了。

使用 pidstat -d 来查看进程的io情况

pidstat -d 2 2秒更新一次

[root@iz2ze2w3v37sit3vf71kuez ~]# pidstat -d 2
Linux 3.10.0-514.26.2.el7.x86_64 (iz2ze2w3v37sit3vf71kuez) 2021年12月09日 x86_64 (1 CPU)

16时02分12秒 UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
16时02分14秒 0 258 0.00 22.34 0.00 jbd2/vda1-8
16时02分14秒 0 4261 5398.98 0.00 0.00 java
16时02分14秒 0 31978 70495.43 10387.82 0.00 sysbench

16时02分14秒 UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
16时02分16秒 99 4376 2111.34 0.00 0.00 nginx
16时02分16秒 0 31978 73797.94 10886.60 0.00 sysbench
发现,sysbench 在大量读写。

字段释意:

字段说明
PID:进程id
kB_rd/s:每秒从磁盘读取的KB
kB_wr/s:每秒写入磁盘KB
kB_ccwr/s:任务取消的写入磁盘的KB。当任务截断脏的pagecache的时候会发生。
COMMAND:task的命令名
iodelay: IO的延迟时间

IO延迟是指控制器将IO指令发出后,到IO完成的过程中所消耗的时间。
pidstat -d 查看哪个进程的IO有问题,主要查看iodelay(io的延迟时间),一般小于1(单位ms)。
如果持续大于10,说明磁盘有瓶颈
例子1

1)生成测试文件,prepare阶段,生成需要的测试文件,完成后会在当前目录下生成很多小文件。

–num-threads 开启的线程 --file-total-size 总的文件大小

sysbench --test=fileio --file-num=10 --file-total-size=5G prepare 表示生成10个5G的文件

2)测试,run阶段

sysbench --test=fileio --file-total-size=5G --file-test-mode=rndrw --max-time=180 --max-requests=100000000 --num-threads=16 --init-rng=on --file-num=10 --file-extra-flags=direct --file-fsync-freq=0 --file-block-size=16384 run

3)清除测试数据

sysbench --test=fileio --file-num=10 --file-total-size=5G cleanup

例子2 IO随机读测试样例

–创建10G的文件,分成4个,测试16K块大小,使用direct方式读,测试600秒(10分钟),启用64个线程,每3秒输出一次结果

prepare

#sysbench --test=fileio --file-num=4 --file-block-size=16384 --file-total-size=10G --file-test-mode=rndrd --file-extra-flags=direct --max-requests=0 --max-time=600 --num-threads=64 --report-interval=3 prepare

run

#sysbench --test=fileio --file-num=4 --file-block-size=16384 --file-total-size=10G --file-test-mode=rndrd --file-extra-flags=direct --max-requests=0 --max-time=600 --num-threads=64 --report-interval=3 run

cleanup

#sysbench --test=fileio --file-num=4 --file-block-size=16384 --file-total-size=10G --file-test-mode=rndrd --file-extra-flags=direct --max-requests=0 --max-time=600 --num-threads=64 --report-interval=3 cleanup

例子3

prepare阶段,生成需要的测试文件,完成后会在当前目录下生成很多小文件。

sysbench --test=fileio --num-threads=16 --file-total-size=2G --file-test-mode=rndrw prepare

run阶段

sysbench --test=fileio --num-threads=20 --file-total-size=2G --file-test-mode=rndrw run

清理测试时生成的文件

sysbench --test=fileio --num-threads=20 --file-total-size=2G --file-test-mode=rndrw cleanup

b、cpu测试
参数详解:

–cpu-max-prime=N 用来选项指定最大的素数,具体参数可以根据CPU的性能来设置,默认为10000

sysbench --test=cpu --cpu-max-prime=20000 run

c、内存分配及传输速度(测试了内存的连续读写性能)
参数详解:

–memory-block-size=SIZE 测试内存块的大小,默认为1K

–memory-total-size=SIZE 数据传输的总大小,默认为100G

–memory-scope=STRING 内存访问的范围,包括全局和本地范围,默认为global

–memory-hugetlb=[on|off] 是否从HugeTLB池分配内存的开关,默认为off

–memory-oper=STRING 内存操作的类型,包括read, write, none,默认为write

–memory-access-mode=STRING 内存访问模式,包括seq,rnd两种模式,默认为seq

例子1

sysbench --test=memory --memory-block-size=8K --memory-total-size=1G --num-threads=16 run

d、线程测试
sysbench --test=threads --num-threads=500 --thread-yields=100 --thread-locks=4 run

sysbench --test=threads --num-threads=64 --thread-yields=100 --thread-locks=2 run

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值