Linux压力测试工具Stress如何使用

By | 2019-10-19

stress是一个linux下的压力测试工具, 为希望完全在高负载下测试其系统并监控运行状况的用户而设计 。

系统环境:Ubuntu 18.04

下载安装 stress

apt install stress

stress 工具使用

stress 的命令参数

`stress' imposes certain types of compute stress on your system

Usage: stress [OPTION [ARG]] ...
 -?, --help         show this help statement
     --version      show version statement
 -v, --verbose      be verbose
 -q, --quiet        be quiet
 -n, --dry-run      show what would have been done
 -t, --timeout N    timeout after N seconds
     --backoff N    wait factor of N microseconds before work starts
 -c, --cpu N        spawn N workers spinning on sqrt()
 -i, --io N         spawn N workers spinning on sync()
 -m, --vm N         spawn N workers spinning on malloc()/free()
     --vm-bytes B   malloc B bytes per vm worker (default is 256MB)
     --vm-stride B  touch a byte every B bytes (default is 4096)
     --vm-hang N    sleep N secs before free (default none, 0 is inf)
     --vm-keep      redirty memory instead of freeing and reallocating
 -d, --hdd N        spawn N workers spinning on write()/unlink()
     --hdd-bytes B  write B bytes per hdd worker (default is 1GB)

Example: stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 10s

Note: Numbers may be suffixed with s,m,h,d,y (time) or B,K,M,G (size).

stress 参数说明:

-? 显示帮助信息

-v 显示版本号

-q 不显示运行信息

-n 显示已完成的指令情况

-t --timeout N 指定运行N秒后停止

--backoff N 等待N微妙后开始运行

-c 产生n个进程 每个进程都反复不停的计算随机数的平方根

-i 产生n个进程 每个进程反复调用sync(),sync()用于将内存上的内容写到硬盘上

-m --vm n 产生n个进程,每个进程不断调用内存分配malloc和内存释放free函数

--vm-bytes B 指定malloc时内存的字节数 (默认256MB)

--vm-hang N 指定在free钱的秒数

-d --hadd n 产生n个执行write和unlink函数的进程

-hadd-bytes B 指定写的字节数

--hadd-noclean 不unlink

时间单位可以为秒s,分m,小时h,天d,年y,文件大小单位可以为K,M,G

stress 测试场景举例:

1、测试CPU负荷
stress -c 4 -t 10
增加4个cpu进程,处理sqrt()函数函数,以提高系统CPU负荷,测试10s

2、内存测试
stress -i 4 –vm 10 –vm-bytes 1G –vm-hang 100 –timeout 100s
新增4个io进程,10个内存分配进程,每次分配大小1G,分配后100s释放

3、磁盘I/O测试
stress –d 1 –hdd-bytes 3G
新增1个写进程,每次写3G文件块

4、硬盘测试
stress -i 1 -d 10 –hdd-bytes 3G –hdd-noclean
新增1个IO进程,10个写进程,每次写入3G文件块,且不清除,会逐步将硬盘耗尽

安装htop监控进程

apt install htop
#htop的具体使用方法请自行搜索

安装sensors监控温度

apt install lm-sensors
#输入sensors即可检测温度和风扇转速
root@ubuntu:~# sensors|grep -E 'Core|Package'
Package id 0:  +46.0°C  (high = +100.0°C, crit = +100.0°C)
Core 0:        +42.0°C  (high = +100.0°C, crit = +100.0°C)
Core 1:        +44.0°C  (high = +100.0°C, crit = +100.0°C)

stress 测试脚本

输入测试时间,每秒钟打印当前CPU温度 ,代码如下:

#!/bin/sh
echo -n "Input test time:"
read TIME
echo "===================STRESS TEST START==================="
stress -c 4 --vm 10 --vm-hang 100 -t $TIME &

while [ "$TIME" -gt 0 ]; do
	sensors|grep -E "Package|Core"
	sleep 1
	TIME=$(($TIME-1))
	echo "==============================time:$TIME"
done

测试截屏

stress

发表评论

邮箱地址不会被公开。 必填项已用*标注