stress简介

stress,顾名思义是一款压力测试工具。你可以用它来对系统CPU,内存,以及磁盘IO生成负载。

安装stress

sudo yum install -y epel-release

sudo yum install -y stress

使用说明

[root@server1 ~]# 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).

实例

压测CPU

​ 压测3个CPU

[root@server1 ~]# stress -c 3   
stress: info: [13088] dispatching hogs: 3 cpu, 0 io, 0 vm, 0 hdd

​ 压测效果

top - 17:46:37 up 14 days, 15:38,  2 users,  load average: 2.02, 2.03, 2.13
Tasks: 209 total,   4 running, 205 sleeping,   0 stopped,   0 zombie
%Cpu0  :  1.7 us,  0.3 sy,  0.0 ni, 97.6 id,  0.0 wa,  0.0 hi,  0.3 si,  0.0 st
%Cpu1  :  6.7 us,  0.3 sy,  0.0 ni, 93.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  6.3 us,  0.3 sy,  0.0 ni, 93.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  : 92.7 us,  0.0 sy,  0.0 ni,  7.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu4  : 94.3 us,  0.0 sy,  0.0 ni,  5.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu5  : 93.0 us,  0.0 sy,  0.0 ni,  7.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu6  :  7.3 us,  0.0 sy,  0.0 ni, 92.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu7  :  0.3 us,  0.0 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
压测内存
  • ​ 对每个vm worker分配1G内存
    [root@server1 ~]# stress --vm 3 --vm-bytes 1G --timeout 1000s
    stress: info: [16048] dispatching hogs: 0 cpu, 0 io, 3 vm, 0 hdd
    
  • 压测前内存使用
    KiB Mem : 16266128 total, 11839996 free,   198444 used,  4227688 buff/cache
    KiB Swap:  2097148 total,  2032292 free,    64856 used. 15442488 avail Mem
    
  • 压测后内存使用
    KiB Mem : 16266128 total,  9048376 free,  2990060 used,  4227692 buff/cache
    KiB Swap:  2097148 total,  2032292 free,    64856 used. 12650868 avail Mem 
    
    压测IO

    对磁盘压力测试有两个参数:

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

    stress -d N 会产生N个进程,每个进程往当前目录中写入固定大小的临时文件,然后执行unlink操作删除该临时文件。 临时文件的大小默认为1G,但可以通过 --hdd-bytes 设置临时文件的大小。比如

    stress -i 2 -d 4 --hdd-bytes 512M
    

    你会发现压力测试时,当前目录所在可用空间少了2G,如下所示:

    [root@server1 ~]# df -m
    Filesystem              1M-blocks  Used Available Use% Mounted on
    /dev/mapper/centos-root     99272 66451     32821  67% /
    devtmpfs                     7931     0      7931   0% /dev
    tmpfs                        7943     0      7943   0% /dev/shm
    tmpfs                        7943   273      7670   4% /run
    tmpfs                        7943     0      7943   0% /sys/fs/cgroup
    /dev/sda1                    1014   146       869  15% /boot
    tmpfs                        1589     0      1589   0% /run/user/1000
    tmpfs                        1589     0      1589   0% /run/user/0
    [root@server1 ~]# stress -i 2 -d 4 --hdd-bytes 512M &
    [1] 16290
    [root@server1 ~]# stress: info: [16290] dispatching hogs: 0 cpu, 2 io, 0 vm, 4 hdd
    
    [root@server1 ~]# df -m
    Filesystem              1M-blocks  Used Available Use% Mounted on
    /dev/mapper/centos-root     99272 68279     30993  69% /
    devtmpfs                     7931     0      7931   0% /dev
    tmpfs                        7943     0      7943   0% /dev/shm
    tmpfs                        7943   273      7670   4% /run
    tmpfs                        7943     0      7943   0% /sys/fs/cgroup
    /dev/sda1                    1014   146       869  15% /boot
    tmpfs                        1589     0      1589   0% /run/user/1000
    tmpfs                        1589     0      1589   0% /run/user/0
    

发表评论

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