性能工具之stress工具使用教程(带源码说明)

stress是一个在linux下的压力测试小工具。

我看到有些人用这个工具来描述一些资源耗尽的场景,也有人用它来做混沌测试中。请使用者要注意,这个工具并不是模拟业务问题的,是模拟系统级问题的。所以用它来模拟的时候,和业务真实场景差别还是很大的。


因为在性能工作中,经常有人因为不理解工具而误用工具,所以这里我把这个工具拿出来说明一下。

安装stress

  •  
yum install -y stress

stress参数

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

参数可以说非常简单了。大概看一眼就可以知道,它可以模拟CPU、IO、内存、磁盘这些常见又重要的资源消耗。

下面就一一来看一下。

模拟CPU

 

[root@7DGroupT1 ~]# stress -c 4 -t 100
top - 10:48:11 up 63 days, 23:57,  2 users,  load average: 0.67, 1.41, 4.21
Tasks: 122 total,   5 running, 117 sleeping,   0 stopped,   0 zombie
%Cpu0  : 99.7 us,  0.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8010528 total,  5550792 free,  1866688 used,   593048 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  5762564 avail Mem

模拟CPU的参数很简洁。打印一下栈看一下:

  •  
  •  
  •  
  •  
  •  
  •  
[root@s6 ~]# pstack 29253#0  0x00007f123634761b in random () from /usr/lib64/libc.so.6#1  0x00007f1236347b39 in rand () from /usr/lib64/libc.so.6#2  0x0000557e9ea32dbd in hogcpu ()#3  0x0000557e9ea3180a in main ()[root@s6 ~]

 

其实代码很简单,就是一个hogcpu函数。源码是这样的:

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
inthogcpu (void){
  
    while (1)    sqrt (rand ());
  return 0;}

是不是看了之后觉得自己都能写一个了?不就是一个while吗?


模拟内存

 

[root@7DGroupT1 ~]# stress --vm 30 --vm-bytes 1G --vm-hang 50 --timeout 50s
[root@7DGroupT1 ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 5606796   6828 534736    0    0  4548   212  457  710  0  0 99  1  0
 0  0      0 5597852   6976 544360    0    0  9712    52  66
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值