Linux下使用压力测试工具stress
一:stress的安装
首先解压安装包到/usr/local/src/下
mv stress-1.0.4.tar.gz /usr/local/src
tar -zxf stress-1.0.4.tar.gz
直接进入目录,开始编译安装:
cd stress-1.0.4/
./configure
make && make install
查看当前版本:
stress --version
二:开始压力测试
1.针对CPU:如果是单核cpu则会压满,使用top命令可看出达到100%
stress --cpu 1
如果是4核,使用top命令则会发现cpu使用率达到25%
2.针对内存:先 free -h 查看内存大小,再使用如下命令增加内存:
stress -i 4 --vm 3 --vm-bytes 4G --vm-hang 100
这样就达到新增三个进程,每个进程占用4G的内存的效果了。
三:通过脚本后台压测
使用nohup运行将输出丢到日志,后台运行
#!/bin/bash
echo “start”
nohup `stress –cpu 1` > test.log 2>1 &
nohup `stress -i 4 –vm 5 –vm-bytes 1G –vm-hang 100` > test1.log 2>1 &
echo “end”
结束压测方法:可以在stress命令中加-t或–timeout后接秒数,也可以ps -ef | grep stress查出并杀死进程
四:结合工具Cpulimit来限制cpu想达到的使用率百分比
安装cpulimit:(具体位置无要求)
tar -zxf cpulimit-1.1.tar.gz
cd cpulimit-1.1/
make
cp cpulimit /usr/sbin/
接着就可以使用命令了
-p:根据pid进程号限制cpu使用率
-l:限制到百分之多少
我们以此来优化脚本:
#!/bin/bash
echo “start”
nohup `stress –cpu 1` > test.log 2>1 &
echo “正在限制cpu占用率”
sleep 2
pids=$(pidof stress)
echo “Stress pids $pids”
for pid in $pids
do
echo “Set limit for pid $pid”
cpulimit -p $pid -l 45 > test2.log 2>1 &
done
nohup `stress –cpu 3` > test.log 2>1 &
nohup `stress -i 4 –vm 2 –vm-bytes 7G –vm-hang 100` > test1.log 2>1 &
echo “end”