针对系统和数据库压测是项目上线前必做的一项,这里使用的是最新版本的sysbench做的压测使用详解。sysbench可以做系统层面的压力测试(CPU、内存、硬盘IO、互斥锁、Thead),也可以做数据库(MySQL/Oracle/PG)层面的压力测试。安装容易,操作便捷,是压测使用的重要工具之一。
一、sysbench下载
项目地址:https://launchpad.net/sysbench
下载地址:https://github.com/akopytov/sysbench/archive/1.0.3.tar.gz
二、sysbench安装
[root@DB_TEST tools]# tar -xzvf sysbench-1.0.3.tar.gz
[root@DB_TEST tools]# cd sysbench-1.0.3
[root@DB_TEST sysbench-1.0.3]# ./autogen.sh
[root@DB_TEST sysbench-1.0.3]#./configure --prefix=/usr/local/sysbench --with-mysql
[root@DB_TEST sysbench-1.0.3]# make
[root@DB_TEST sysbench-1.0.3]# make install
[root@DB_TEST sysbench-1.0.3]# cp -rp /usr/local/sysbench/bin/sysbench /usr/bin/
[root@DB_TEST sysbench-1.0.3]# /usr/local/sysbench/bin/sysbench --help
点击(此处)折叠或打开
三、sysbench使用
1、CPU性能测试
cpu性能测试:找范围内最大素数{时间越短越好}
cpu性能测试主要是根据素数的加法运算,这里指定最大素数(质数)为80000,--num-threads=`grep "processor" /proc/cpuinfo | wc -l`指定线程数,默认是1
/usr/local/sysbench/bin/sysbench --test=cpu --cpu-max-prime=80000 run
点击(此处)折叠或打开
实际环境CPU拷机压测脚本如下:
echo -e "进行CPU压力测试: 寻找小于1千万的最大质数,并发线程数10,最大请求数100 "
/usr/local/bin/sysbench --num-threads=10 --max-requests=100 --test=cpu --debug --cpu-max-prime=10000000 run
2、内存性能测试
sysbench 测试memory的时候是顺序读或写内存的。根据选项的不同,每次操作过程中,每个线程可以获取global或本地的数据块
内存:以不同块大小传输一定数量的数据吞吐量大小{越大越好}
点击(此处)折叠或打开
示例:测试传输数据总量为5G,每个块大小是1K(一般设置是8K),连续读写的情况下,吞吐量3435.87 MiB/sec
[root@db_test fileio]# /usr/local/sysbench/bin/sysbench --test=memory --memory-block-size=1K --memory-total-size=5G run
WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
sysbench 1.0.3 (using bundled LuaJIT 2.1.0-beta2)
Running the test with following options:
Number of threads: 1
Initializing random number generator from current time
Initializing worker threads...
Threads started!
Operations performed: 5242880 (3518332.06 ops/sec)
5120.00 MiB transferred (3435.87 MiB/sec)
General statistics:
total time: 1.4883s
total number of events: 5242880
Latency (ms):
min: 0.00
avg: 0.00
max: 0.03
95th percentile: 0.00
sum: 631.01
Threads fairness:
events (avg/stddev): 5242880.0000/0.00
execution time (avg/stddev): 0.6310/0.00
实际内存压力测试脚本:
echo -e "进行内存压力测试: 测试范围32G,并发线程数10,最大请求数100, 读 "
/usr/local/bin/sysbench --num-threads=10 --max-requests=100 --test=memory --memory-block-size=8K --memory-total-size=${MEM}G --memory-oper=read run
echo -e "进行内存压力测试: 测试范围32G,并发线程数10,最大请求数100, 写 "
/usr/local/bin/sysbench --num-threads=10 --max-requests=100 --test=memory --memory-block-size=8K --memory-total-size=${MEM}G --memory-oper=write run
3、磁盘IO性能测试
IO性能测试:不同场景下IOPS{越大越好}
点击(此处)折叠或打开
--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,mmap} [sync]
--file-extra-flags=STRING 使用额外的标志打开文件 {sync,dsync,direct} []
--file-fsync-freq=N 执行fsync()频率,0表示不使用,默认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表示不合并
--file-rw-ratio=N 测试读写比例,默认[1.5]
seqwr 顺序写入
seqrewr 顺序重写
seqrd 顺序读取
rndrd 随机读取
rndwr 随机写入
rndrw 混合随机读/写
FileIO示例01:prepare 命令创建了128个文件总共大小为10G ,文件读写模式为随机读写混合方式。run 命令则进行测试,并返回结果,cleanup 删除测试产生的文件!
##Fileio准备工作,生成测试数据(最好比内存的2倍大)
[root@db_test fileio]# /usr/local/sysbench/bin/sysbench --num-threads=16 --test=fileio --file-total-size=10G --file-test-mode=rndrw prepare
WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
WARNING: --num-threads is deprecated, use --threads instead
sysbench 1.0.3 (using bundled LuaJIT 2.1.0-beta2)
128 files, 81920Kb each, 10240Mb total
Creating files for the test...
Extra file open flags: 0
Creating file test_file.0
...
Creating file test_file.127
10737418240 bytes written in 9.96 seconds (1027.97 MiB/sec).
##正式执行FileIO测试
[root@db_test fileio]# /usr/local/sysbench/bin/sysbench --num-threads=16 --test=fileio --file-total-size=10G --file-test-mode=rndrw run
WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
WARNING: --num-threads is deprecated, use --threads instead
sysbench 1.0.3 (using bundled LuaJIT 2.1.0-beta2)
Running the test with following options:
Number of threads: 16
Initializing random number generator from current time
Extra file open flags: 0
128 files, 80MiB each
10GiB 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: 1449.16
writes/s: 965.91
fsyncs/s: 3083.55
Throughput:
read, MiB/s: 22.64
written, MiB/s: 15.09
General statistics:
total time: 10.4176s
total number of events: 57291
Latency (ms):
min: 0.00
avg: 2.87
max: 825.08
95th percentile: 0.17
sum: 164312.66
Threads fairness:
events (avg/stddev): 3580.6875/469.53
execution time (avg/stddev): 10.2695/0.08
IO密切相关的包括每秒请求数和总吞吐量,其中请求数是(1449.16+965.91+3083.55)/s,吞吐量是(22.64+15.09)MB/s
##清理创建的文件
[root@db_test fileio]# /usr/local/sysbench/bin/sysbench --num-threads=16 --test=fileio --file-total-size=10G --file-test-mode=rndrw cleanup
WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
WARNING: --num-threads is deprecated, use --threads instead
sysbench 1.0.3 (using bundled LuaJIT 2.1.0-beta2)
Removing test files...
实际环境FILEIO压力测试脚本:
echo -e "进行IO压力测试: 20个文件,每个10GB,随机读写 "
/usr/local/bin/sysbench --file-num=20 --num-threads=20 --test=fileio --file-total-size=${HDD}G --max-requests=1000000 --file-test-mode=rndrw prepare
/usr/local/bin/sysbench --file-num=20 --num-threads=20 --test=fileio --file-total-size=${HDD}G --max-requests=1000000 --file-test-mode=rndrw run
/usr/local/bin/sysbench --file-num=20 --num-threads=20 --test=fileio --file-total-size=${HDD}G --max-requests=1000000 --file-test-mode=rndrw cleanup
4、thread测试
线程调度:线程并发执行,循环响应信号量花费的时间{越少越好}
测试线程调度器的性能。对于高负载情况下测试线程调度器的行为非常有用
点击(此处)折叠或打开
示例:
[root@db_test fileio]# /usr/local/sysbench/bin/sysbench --num-threads=64 --test=threads --thread-yields=100 --thread-locks=2 run
WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
WARNING: --num-threads is deprecated, use --threads instead
sysbench 1.0.3 (using bundled LuaJIT 2.1.0-beta2)
Running the test with following options:
Number of threads: 64
Initializing random number generator from current time
Initializing worker threads...
Threads started!
General statistics:
total time: 10.0043s
total number of events: 102842
Latency (ms):
min: 0.02
avg: 6.22
max: 28.83
95th percentile: 15.00
sum: 640039.96
Threads fairness:
events (avg/stddev): 1606.9062/31.79
execution time (avg/stddev): 10.0006/0.00
5、互斥锁测试
互斥锁:并发线程同时申请互斥锁循环一定次数花费的时间{越少越好}
测试互斥锁的性能,方式是模拟所有线程在同一时刻并发运行,并都短暂请求互斥锁
点击(此处)折叠或打开
所有线程同时执行,获取短时间的mutex lock,以便测试mutex的实现!
示例:
[root@db_test fileio]# /usr/local/sysbench/bin/sysbench --test=mutex --mutex-num=4096 --mutex-locks=50000 --mutex-loops=20000 run
WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
sysbench 1.0.3 (using bundled LuaJIT 2.1.0-beta2)
Running the test with following options:
Number of threads: 1
Initializing random number generator from current time
Initializing worker threads...
Threads started!
General statistics:
total time: 0.0021s
total number of events: 1
Latency (ms):
min: 2.05
avg: 2.05
max: 2.05
95th percentile: 2.03
sum: 2.05
Threads fairness:
events (avg/stddev): 1.0000/0.00
execution time (avg/stddev): 0.0020/0.00
mysql之 sysbench1.0.3 安装与系统压力测试
原文:https://www.cnblogs.com/andy6/p/9862204.html