一、命令介绍

stress是一个linux系统压力测试工具,顾名思义主要用来进行压力测试。

选项 含义
-t, –timeout 指定运行多少秒停止
–backoff 指定等待N微妙后开始运行
-c, –cpu 指定产生N个处理sqrt()函数的CPU进程,也就是每个进程不停计算随机数的平方根
-i, –io 指定产生N个处理sync()函数的磁盘I/O进程,sync()用于将内存上的内容写到硬盘上
-m, –vm 指定产生n个进程,每个进程不断调用内存分配malloc函数和内存释放free函数
–vm-bytes 指定调用malloc函数时分配内存的字节数 (默认256MB)
–vm-stride 应该是给内存赋值,touch a byte every B bytes (default is 4096)
–vm-hang 指定malloc函数分配的内存转入睡眠状态 N 秒,然后free()释放掉,一直重复执行这个过程
–vm-keep 冗余内存,而不是释放和重新分配
-d, –hdd 指定产生N个不断执行 write 和 unlink 函数的进程(创建文件,写入内容,删除文件)
–hdd-bytes 指定写入文件大小,默认为1GB

二、实例演示

(1)使用-c选项进行cpu压力测试

[root@server ~]# stress -c 1 -t 100
stress: info: [19891] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd

使用mpstat和pidstat命令可以看到此时cpu使用率为100%,97.03%为被stress进程所占用。

[root@server ~]# mpstat -P ALL 1 1
Linux 3.10.0-514.26.2.el7.x86_64 (server)       12/29/2019      _x86_64_        (1 CPU)

01:42:33 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
01:42:34 PM  all  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
01:42:34 PM    0  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00

Average:     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
Average:     all  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
Average:       0  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
[root@server ~]# pidstat 1 1  
Linux 3.10.0-514.26.2.el7.x86_64 (server)       12/29/2019      _x86_64_        (1 CPU)

01:42:43 PM   UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
01:42:44 PM     0     18768    0.00    0.99    0.00    6.93    0.99     0  AliYunDun
01:42:44 PM     0     19925   97.03    0.00    0.00    2.97   97.03     0  stress
01:42:44 PM     0     19931    0.00    0.99    0.00    1.98    0.99     0  pidstat

Average:      UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
Average:        0     18768    0.00    0.99    0.00    6.93    0.99     -  AliYunDun
Average:        0     19925   97.03    0.00    0.00    2.97   97.03     -  stress
Average:        0     19931    0.00    0.99    0.00    1.98    0.99     -  pidstat

(2)使用-i选项进行IO压力测试

  注意sync()是将内存缓冲区中的数据立即写入磁盘中,对于新建的虚拟机缓冲区内本来就没多少数据,那写到磁盘中的数据也就不多,也就没法产生 I/O 压力。这样大部分就都是系统调用的消耗了,可以看到%iowait只有16%,而%system却有81%(%system:表示内核进程所使用cpu的百分比;%iowait表示等待IO所使用cpu的百分比)

[root@server ~]# stress -i 1 -t 100
stress: info: [20174] dispatching hogs: 0 cpu, 1 io, 0 vm, 0 hdd

[root@server ~]# mpstat -P ALL 2 1
Linux 3.10.0-514.26.2.el7.x86_64 (server)       12/30/2019      _x86_64_        (1 CPU)

03:24:31 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
03:24:33 PM  all    1.52    0.00   81.82   16.67    0.00    0.00    0.00    0.00    0.00    0.00
03:24:33 PM    0    1.52    0.00   81.82   16.67    0.00    0.00    0.00    0.00    0.00    0.00

Average:     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
Average:     all    1.52    0.00   81.82   16.67    0.00    0.00    0.00    0.00    0.00    0.00
Average:       0    1.52    0.00   81.82   16.67    0.00    0.00    0.00    0.00    0.00    0.00
[root@server ~]# pidstat  -p 3210 2 1
Linux 3.10.0-514.26.2.el7.x86_64 (server)       12/30/2019      _x86_64_        (1 CPU)

03:27:43 PM   UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
03:27:45 PM     0      3210    0.00   72.40    0.00    6.77   72.40     0  stress
Average:        0      3210    0.00   72.40    0.00    6.77   72.40     -  stress

对于这种情况我们要去测IO,可以使用-d选项,可以看到%iowait直接飙升到了86.91%。

[root@server ~]# stress -d 1
stress: info: [3273] dispatching hogs: 0 cpu, 0 io, 0 vm, 1 hdd

[root@server ~]# mpstat -P ALL 2 1
Linux 3.10.0-514.26.2.el7.x86_64 (server)       12/30/2019      _x86_64_        (1 CPU)

03:33:06 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
03:33:08 PM  all    2.09    0.00   10.47   86.91    0.00    0.52    0.00    0.00    0.00    0.00
03:33:08 PM    0    2.09    0.00   10.47   86.91    0.00    0.52    0.00    0.00    0.00    0.00

Average:     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
Average:     all    2.09    0.00   10.47   86.91    0.00    0.52    0.00    0.00    0.00    0.00
Average:       0    2.09    0.00   10.47   86.91    0.00    0.52    0.00    0.00    0.00    0.00

(3)使用-m选项进行内存压力测试

[root@cp ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        131M        1.5G        9.5M        169M        1.5G
Swap:          2.0G          0B        2.0G
[root@cp ~]# stress -m 1 --vm-bytes 1G --vm-hang 10
stress: info: [8150] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd

[root@cp ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        1.1G        492M        9.5M        169M        482M
Swap:          2.0G          0B        2.0G
[root@cp ~]# pidstat -C "stress"  -p ALL -r 2 1
Linux 3.10.0-957.el7.x86_64 (cp)        12/30/2019      _x86_64_        (1 CPU)

03:23:48 AM   UID       PID  minflt/s  majflt/s     VSZ     RSS   %MEM  Command
03:23:50 AM     0      8321      0.00      0.00    7312     428   0.02  stress
03:23:50 AM     0      8322      0.00      0.00 1055892 1048780  56.29  stress

Average:      UID       PID  minflt/s  majflt/s     VSZ     RSS   %MEM  Command
Average:        0      8321      0.00      0.00    7312     428   0.02  stress
Average:        0      8322      0.00      0.00 1055892 1048780  56.29  stress

(4)使用-d选项进行磁盘IO压力测试

[root@cp ~]# stress -d 1 --hdd-bytes 1024G
stress: info: [8820] dispatching hogs: 0 cpu, 0 io, 0 vm, 1 hdd

[root@cp ~]# pidstat -C "stress"  -p ALL -d 2 1
Linux 3.10.0-957.el7.x86_64 (cp)        12/30/2019      _x86_64_        (1 CPU)

03:53:07 AM   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s iodelay  Command
03:53:09 AM     0      8833      0.00      0.00      0.00       0  stress
03:53:09 AM     0      8834      0.00  80991.04      0.00     192  stress

Average:      UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s iodelay  Command
Average:        0      8833      0.00      0.00      0.00       0  stress
Average:        0      8834      0.00  80991.04      0.00     192  stress
[root@cp ~]# iostat  2 2
Linux 3.10.0-957.el7.x86_64 (cp)        12/30/2019      _x86_64_        (1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           3.94    0.00   23.51    6.44    0.00   66.11

Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
dm-0             10.61        31.52      4966.08         0.00     268893   42365272          0
dm-1            250.27         6.14       995.19         0.00      52348    8489904          0
sda              13.13        41.15      5952.57         0.00     351078   50780932          0
scd0              0.00         0.12         0.00         0.00       1028          0          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00   12.30   87.70    0.00    0.00

Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
dm-0            149.50         0.00     76544.00         0.00          0     153088          0
dm-1              0.00         0.00         0.00         0.00          0          0          0
sda             156.00         0.00     79617.75         0.00          0     159235          0
scd0              0.00         0.00         0.00         0.00          0          0          0

发表回复

验证码: 33 − = 27