使用sysbench进行压力测试

时间:2022-11-08 11:41:47

sysbench是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试。数据库目前支持MySQL/Oracle/PostgreSQL/Drizzle,它主要包括以下几种方式的测试:

  • cpu性能
  • 磁盘io性能
  • 调度程序性能
  • 内存分配及传输速度
  • POSIX线程性能
  • 数据库性能(OLTP基准测试)

机器配置

[root@localhost ~]# grep -iE 'MemTotal|MemFree' /proc/meminfo   
MemTotal: 8058060 kB
MemFree: 2213240 kB
[root@localhost ~]# grep "processor" /proc/cpuinfo | wc -l
4
[root@localhost ~]# uname -a
Linux localhost 2.6.32-431.11.2.el6.x86_64 #1 SMP Tue Mar 25 19:59:55 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)

一、安装

官方地址:https://github.com/akopytov/sysbench

下载方法:git clone https://github.com/akopytov/sysbench.git

安装

./autogen.sh
./configure
make && make install

以上命令的编译SysBench默认需要Mysql库的支持,如果你机器已安装Mysql但没有指定位置(在PATH下找不到mysql_config),你必须在./configure得时候指定–with-mysql-includes和–with-mysql-libs

也可以使用–without-mysql命令,取消Mysql的支持

command命令的简单描述及作用

prepare:为测试执行准备工作,例如为fileis测试在磁盘上创建必要的文件,为OLTP测试准备测试数据
run: 执行完整的测试,必须指定–-test选项
cleanup: 在测试运行结束移除prepare准备的临时数据
help:显示不同测试(根据–-test选项)的帮助信息,你也可以使用sysbench help命令(而不用–-test)去显示帮助信息

一些参数的解释

--num-threads  #创建测试线程的数目 
--max-requests #请求的最大数目,0表示不限制
--max-time #最大执行时间,单位秒,0表示不限制
--thread-stack-size #每个线程的堆栈大小32K
--init-rng #在测试开始之前指定是否需要初始化随机数发生器off
--report-interval #隔多久打印一次统计信息,注意的是统计信息统计的是间隔时间而不是累计时间的数据。单位秒,0表示关闭该功能
--test #指定测试项目名称Required
--debug #是否显示更多的调试信息off
--validate #在可能的情况下是否进行验证检查off
--help #帮助信息off
--verbosity #详细级别,0-严重信息,5-调试信息
--percentile #表示设定采样比例,默认是 95%,即丢弃1%的长请求,在剩余的99%里取最大值

二、CPU的基准测试

CPU测试使用64位整数,测试计算质数直到某个最大值所需要的时间,cpu测试主要是进行素数的加法运算,在上面的例子中,指定了最大的素数为 20000,自己可以根据机器cpu的性能来适当调整数值,主要看total time所花费的时间

[root@localhost sysbench]# sysbench --test=cpu --cpu-max-prime=10000 run
sysbench 0.5: multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 1
Random number generator seed is 0 and will be ignored


Primer numbers limit: 10000

Threads started!


General statistics:
total time: 14.5690s
total number of events: 10000
total time taken by event execution: 14.5616s
response time:
min: 1.40ms
avg: 1.46ms
max: 10.59ms
approx. 95 percentile: 1.52ms

Threads fairness:
events (avg/stddev): 10000.0000/0.00
execution time (avg/stddev): 14.5616/0.00

相关选项

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

 三、线程基准测试

thread-locks小于线程数除以2,lock越少,处理时间越长

[root@localhost sysbench]# sysbench  --test=threads --num-threads=800 --thread-yields=100 --thread-locks=10 run 
sysbench 0.5: multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 800
Random number generator seed is 0 and will be ignored


Threads started!


General statistics:
total time: 0.4644s
total number of events: 10000
total time taken by event execution: 350.9839s
response time:
min: 0.04ms
avg: 35.10ms
max: 456.96ms
approx. 95 percentile: 144.17ms

Threads fairness:
events (avg/stddev): 12.5000/6.45
execution time (avg/stddev): 0.4387/0.01

相关参数

sysbench --test=threads help
sysbench 0.5: multi-threaded system evaluation benchmark

threads options:
--thread-yields=N 每个请求产生多少个线程。默认是1000
--thread-locks=N 每个线程的锁的数量。默认是8

四、文件IO基准测试

磁盘IO性能测试[主要看每秒请求数(request)和总体的吞吐量(total)

[root@localhost sysbench]# sysbench --test=fileio --num-threads=16 --file-total-size=2G --file-test-mode=rndrw prepare
sysbench 0.5: multi-threaded system evaluation benchmark

128 files, 16384Kb each, 2048Mb total
Creating files for the test...
Extra file open flags: 0
Creating file test_file.0
Creating file test_file.1
……
Creating file test_file.127
2147483648 bytes written in 67.56 seconds (30.32 MB/sec).
[root@localhost sysbench]# sysbench --test=fileio --num-threads=20 --file-total-size=2G --file-test-mode=rndrw run
sysbench 0.5: multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 20
Random number generator seed is 0 and will be ignored


Extra file open flags: 0
128 files, 16Mb each
2Gb total file size
Block size 16Kb
Number of IO requests: 10000
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
Threads started!

Operations performed: 5999 reads, 4001 writes, 12800 Other = 22800 Total
Read 93.734Mb Written 62.516Mb Total transferred 156.25Mb (19.3Mb/sec)
1235.21 Requests/sec executed

General statistics:
total time: 8.0958s
total number of events: 10000
total time taken by event execution: 1.4557s
response time:
min: 0.00ms
avg: 0.15ms
max: 1319.03ms
approx. 95 percentile: 0.02ms

Threads fairness:
events (avg/stddev): 500.0000/186.14
execution time (avg/stddev): 0.0728/0.29

相关参数

sysbench  --test=fileio help
sysbench 0.5: multi-threaded system evaluation benchmark

fileio options:
--file-num=N 创建测试文件的数量。默认是128
--file-block-size=N 测试时文件块的大小。默认是16384(16K)
--file-total-size=SIZE 测试文件的总大小。默认是2G
--file-test-mode=STRING 文件测试模式{seqwr(顺序写), seqrewr(顺序读写), seqrd(顺序读), rndrd(随机读), rndwr(随机写), rndrw(随机读写)}
--file-io-mode=STRING 文件操作模式{sync(同步),async(异步),fastmmap(快速map映射),slowmmap(慢map映射)}。默认是sync
--file-extra-flags=STRING 使用额外的标志来打开文件{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

五、内存基准测试

测试了内存的连续读写性能

[root@localhost sysbench]# sysbench --test=memory --memory-block-size=8k --memory-total-size=1G run
sysbench 0.5: multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 1
Random number generator seed is 0 and will be ignored


Threads started!

Operations performed: 131072 (769179.70 ops/sec)

1024.00 MB transferred (6009.22 MB/sec)


General statistics:
total time: 0.1704s
total number of events: 131072
total time taken by event execution: 0.1387s
response time:
min: 0.00ms
avg: 0.00ms
max: 2.12ms
approx. 95 percentile: 0.00ms

Threads fairness:
events (avg/stddev): 131072.0000/0.00
execution time (avg/stddev): 0.1387/0.00

相关参数

sysbench  --test=memory help
sysbench 0.5: multi-threaded system evaluation benchmark

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 

六、互斥锁基准测试

[root@localhost sysbench]# sysbench --test=mutex --num-threads=100 --mutex-num=1000 --mutex-locks=100000 --mutex-loops=10000 run    
sysbench 0.5: multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 100
Random number generator seed is 0 and will be ignored


Threads started!


General statistics:
total time: 4.9822s
total number of events: 100
total time taken by event execution: 474.6478s
response time:
min: 1601.28ms
avg: 4746.48ms
max: 4981.76ms
approx. 95 percentile: 4973.56ms

Threads fairness:
events (avg/stddev): 1.0000/0.00
execution time (avg/stddev): 4.7465/0.40

相关参数

sysbench  --test=mutex help
sysbench 0.5: multi-threaded system evaluation benchmark

mutex options:

--mutex-num=N 数组互斥的总大小。默认是4096
--mutex-locks=N 每个线程互斥锁的数量。默认是50000
--mutex-loops=N 内部互斥锁的空循环数量。默认是10000 

七、OLTP基准测试

完整过程分为3个阶段:prepare->run->cleanup,即分别为准备数据,测试,清理测试数据

[root@localhost sysbench]# sysbench --mysql-socket=/var/lib/mysql/mysql.sock  --oltp-table-size=100000 --test=sysbench/tests/db/oltp.lua --mysql-user=root --mysql-password=123456 --report-interval=5 --num-threads=100  --oltp_tables_count=10  --rand-type=uniform  --percentile=99 run
sysbench 0.5: multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 100
Report intermediate results every 5 second(s)
Random number generator seed is 0 and will be ignored


Threads started!

[ 5s] threads: 100, tps: 894.57, reads/s: 12790.38, writes/s: 3646.48, response time: 340.91ms (99%)
[ 10s] threads: 100, tps: 973.20, reads/s: 13608.42, writes/s: 3860.61, response time: 238.96ms (99%)
OLTP test statistics:
queries performed:
read: 140000
write: 40000
other: 20000
total: 200000
transactions: 10000 (932.07 per sec.)
deadlocks: 0 (0.00 per sec.)
read/write requests: 180000 (16777.18 per sec.)
other operations: 20000 (1864.13 per sec.)

General statistics:
total time: 10.7289s
total number of events: 10000
total time taken by event execution: 1067.3242s
response time:
min: 9.75ms
avg: 106.73ms
max: 450.30ms
approx. 99 percentile: 284.52ms

Threads fairness:
events (avg/stddev): 100.0000/4.15
execution time (avg/stddev): 10.6732/0.05

相关参数

sysbench --test=oltp help
sysbench 0.5: multi-threaded system evaluation benchmark

oltp options:
--oltp-test-mode=STRING 执行模式{simple,complex(advanced transactional),nontrx(non-transactional),sp}。默认是complex
--oltp-reconnect-mode=STRING 重新连接模式{session(不使用重新连接。每个线程断开只在测试结束),transaction(在每次事务结束后重新连接),query(在每个SQL语句执行完重新连接),random(对于每个事务随机选择以上重新连接模式)}。默认是session
--oltp-sp-name=STRING 存储过程的名称。默认为空
--oltp-read-only=[on|off] 只读模式。Update,delete,insert语句不可执行。默认是off
--oltp-skip-trx=[on|off] 省略begin/commit语句。默认是off
--oltp-range-size=N 查询范围。默认是100
--oltp-point-selects=N number of point selects [10]
--oltp-simple-ranges=N number of simple ranges [1]
--oltp-sum-ranges=N number of sum ranges [1]
--oltp-order-ranges=N number of ordered ranges [1]
--oltp-distinct-ranges=N number of distinct ranges [1]
--oltp-index-updates=N number of index update [1]
--oltp-non-index-updates=N number of non-index updates [1]
--oltp-nontrx-mode=STRING 查询类型对于非事务执行模式{select, update_key, update_nokey, insert, delete} [select]
--oltp-auto-inc=[on|off] AUTO_INCREMENT是否开启。默认是on
--oltp-connect-delay=N 在多少微秒后连接数据库。默认是10000
--oltp-user-delay-min=N 每个请求最短等待时间。单位是ms。默认是0
--oltp-user-delay-max=N 每个请求最长等待时间。单位是ms。默认是0
--oltp-table-name=STRING 测试时使用到的表名。默认是sbtest
--oltp-table-size=N 测试表的记录数。默认是10000
--oltp-dist-type=STRING 分布的随机数{uniform(均匀分布),Gaussian(高斯分布),special(空间分布)}。默认是special
--oltp-dist-iter=N 产生数的迭代次数。默认是12
--oltp-dist-pct=N 值的百分比被视为'special' (for special distribution)。默认是1
--oltp-dist-res=N ‘special’的百分比值。默认是75

General database options:
--db-driver=STRING 指定数据库驱动程序('help' to get list of available drivers)
--db-ps-mode=STRING 编制报表使用模式{auto, disable} [auto]
Compiled-in database drivers:
mysql - MySQL driver
mysql options:
--mysql-host=[LIST,...] 数据库地址
--mysql-port=N 端口号,默认3306
--mysql-user=STRING 用户名
--mysql-password=STRING 密码
--mysql-db=STRING 测试数据库名
--mysql-table-engine=STRING 数据库存储引擎,默认innodb{myisam,innodb,bdb,heap,ndbcluster,federated}
--mysql-socket=STRING socket文件的位置
--mysql-engine-trx=STRING 是否使用事务,与存储引擎选择有关,默认auto{yes,no,auto}
--mysql-ssl=[on|off] 在客户端是否使用SSL连接,默认off
--myisam-max-rows=N MyISAM表最大行数,默认1000000
--mysql-create-options=STRING additional options passed to CREATE TABLE []

 

参考地址

https://launchpad.net/sysbench

https://github.com/akopytov/sysbench