关闭

stress-ng 压力测试环境模拟及分析

发表于:2024-1-08 09:16

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:谢艺华    来源:CSDN

  背景
  在cpulimit设计理念及其思考中,使用到了stress工具模拟系统CPU使用率比较高的场景,并验证cpulimit工具的效果。之前在学习性能优化章节时,还是比较熟悉的。由于太久没接触,又不太熟悉了。好记性不如烂笔头,本篇文章用于记录stress常见的几种模拟场景,方便以后查阅。
  介绍
  stress 是一个linux压力测试工具,专门用于对设备的CPU、IO、内存、负载、磁盘等进行压测。另一个版本是其升级版本stress-ng。在模拟io_wait性能场景时,stress一直表现不好,本文以stress-ng为例,进行介绍。
  安装
  模拟环境可能是我们的虚拟机或目标宿主机。若是虚拟机,我们可以根据系统自带的软件包下载地址进行安装。比如我的环境时ubuntu,执行命令:
  sudo apt install stress-ng
  若是虚拟机,则需要下载源码,交叉编译。下载地址:
  git clone https://github.com/ColinIanKing/stress-ng.git
  交叉编译,一般source交叉编译的环境变量即可。或参考文章工作中如何编译开源工具(gdb)。
  模拟测试
  本文通过stress-ng模拟2个性能场景:CPU密集型进程、I/O密集型进程。以及分析思路。
  CPU 密集型进程
  压测命令:stress-ng --cpu 4 --timeout 600。其中,--timeout 600表示运行时长。
  分析:通过mpstat -P ALL 5 1命令查看CPU使用情况。可知系统的CPU使用率已经被占满。如下:
  yihua@ubuntu:~$ mpstat -P ALL  1
  Linux 4.15.0-213-generic (ubuntu)       12/20/2023      _x86_64_        (4 CPU)
  07:46:38 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
  07:46:39 PM  all   99.75    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:39 PM    0  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:39 PM    1   99.01    0.00    0.99    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:39 PM    2  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:39 PM    3  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:39 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
  07:46:40 PM  all  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:40 PM    0  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:40 PM    1  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:40 PM    2  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:40 PM    3  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:40 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
  07:46:41 PM  all   99.75    0.00    0.25    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:41 PM    0  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:41 PM    1   99.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:41 PM    2  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  07:46:41 PM    3  100.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
  定位:通过pidstat -u 5查看各进程占用CPU使用率的情况。可知,主要是被stress-ng进程占用。如下:
  yihua@ubuntu:~$ pidstat -u 1
  Linux 4.15.0-213-generic (ubuntu)       12/20/2023      _x86_64_        (4 CPU)
  07:49:18 PM   UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
  07:49:19 PM     0       956    0.97    0.00    0.00    0.00    0.97     1  containerd
  07:49:19 PM  1000      2844  100.00    0.00    0.00    0.00  100.00     0  stress-ng-cpu
  07:49:19 PM  1000      2845   99.03    0.00    0.00    5.83   99.03     2  stress-ng-cpu
  07:49:19 PM  1000      2846  100.00    0.97    0.00    0.00  100.00     1  stress-ng-cpu
  07:49:19 PM  1000      2847  100.00    0.00    0.00    0.00  100.00     3  stress-ng-cpu
  07:49:19 PM  1000      2888    0.00    5.83    0.00    3.88    5.83     1  pidstat
  07:49:19 PM   UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
  07:49:20 PM  1000      2094    0.00    1.00    0.00    0.00    1.00     2  sshd
  07:49:20 PM  1000      2844   94.00    0.00    0.00    0.00   94.00     0  stress-ng-cpu
  07:49:20 PM  1000      2845   93.00    0.00    0.00    0.00   93.00     2  stress-ng-cpu
  07:49:20 PM  1000      2846   91.00    1.00    0.00    1.00   92.00     1  stress-ng-cpu
  07:49:20 PM  1000      2847   93.00    0.00    0.00    0.00   93.00     3  stress-ng-cpu
  07:49:20 PM  1000      2888    0.00    1.00    0.00    0.00    1.00     1  pidstat
  07:49:20 PM   UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
  07:49:21 PM  1000      2844   99.00    0.00    0.00    0.00   99.00     0  stress-ng-cpu
  07:49:21 PM  1000      2845   99.00    0.00    0.00    0.00   99.00     2  stress-ng-cpu
  07:49:21 PM  1000      2846   99.00    0.00    0.00    1.00   99.00     1  stress-ng-cpu
  07:49:21 PM  1000      2847   99.00    0.00    0.00    0.00   99.00     3  stress-ng-cpu
  07:49:21 PM  1000      2888    1.00    0.00    0.00    0.00    1.00     1  pidstat
  总结:通过mpstat和pidstat工具,可以快速定位到是因为stress-ng引起的CPU性能问题。
  I/O密集型进程
  压测命令: stress-ng -d 4 --timeout 600。其中-d 4,表示启动4个进程对磁盘进行write操作。
  分析:通过mpstat -P ALL 1命令查看I/O使用情况,可知CPU的消耗主要集中在等待I/O上。如下:
  yihua@ubuntu:~$ mpstat -P ALL 1
  Linux 4.15.0-213-generic (ubuntu)       12/20/2023      _x86_64_        (4 CPU)
  09:56:15 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
  09:56:16 PM  all    0.00    0.00    0.77   83.63    0.00    0.51    0.00    0.00    0.00   15.09
  09:56:16 PM    0    0.00    0.00    1.03   97.94    0.00    1.03    0.00    0.00    0.00    0.00
  09:56:16 PM    1    0.00    0.00    0.98   96.08    0.00    0.98    0.00    0.00    0.00    1.96
  09:56:16 PM    2    0.00    0.00    0.00   40.21    0.00    0.00    0.00    0.00    0.00   59.79
  09:56:16 PM    3    0.00    0.00    0.00   98.97    0.00    0.00    0.00    0.00    0.00    1.03
  09:56:16 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
  09:56:17 PM  all    0.00    0.00    0.51   97.22    0.00    0.00    0.00    0.00    0.00    2.27
  09:56:17 PM    0    0.00    0.00    1.02   98.98    0.00    0.00    0.00    0.00    0.00    0.00
  09:56:17 PM    1    0.00    0.00    1.01   98.99    0.00    0.00    0.00    0.00    0.00    0.00
  09:56:17 PM    2    0.00    0.00    0.00  100.00    0.00    0.00    0.00    0.00    0.00    0.00
  09:56:17 PM    3    0.00    0.00    0.00   90.00    0.00    1.00    0.00    0.00    0.00    9.00
  09:56:17 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
  09:56:18 PM  all    0.00    0.00    1.01   95.96    0.00    0.51    0.00    0.00    0.00    2.53
  09:56:18 PM    0    0.00    0.00    2.06   97.94    0.00    0.00    0.00    0.00    0.00    0.00
  09:56:18 PM    1    0.00    0.00    1.00   92.00    0.00    1.00    0.00    0.00    0.00    6.00
  09:56:18 PM    2    0.00    0.00    0.00   99.00    0.00    1.00    0.00    0.00    0.00    0.00
  09:56:18 PM    3    0.00    0.00    0.00   95.92    0.00    0.00    0.00    0.00    0.00    4.08
  定位:通过pidstat -d 1命令可以查看各个进程读写状态,如下:
  yihua@ubuntu:~/data_gather$ pidstat -d 1
  Linux 4.15.0-213-generic (ubuntu)       12/20/2023      _x86_64_        (4 CPU)
  10:01:32 PM   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s iodelay  Command
  10:01:33 PM     0       314     -1.00     -1.00     -1.00     124  jbd2/sda1-8
  10:01:33 PM     0      3343     -1.00     -1.00     -1.00      72  kworker/u256:1
  10:01:33 PM  1000      3429      0.00  27597.30      0.00     106  stress-ng-hdd
  10:01:33 PM  1000      3430      0.00      0.00      0.00      72  stress-ng-hdd
  10:01:33 PM  1000      3431      0.00      0.00      0.00      84  stress-ng-hdd
  10:01:33 PM  1000      3432      0.00   9764.86      0.00      34  stress-ng-hdd
  10:01:33 PM   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s iodelay  Command
  10:01:34 PM     0       314     -1.00     -1.00     -1.00      57  jbd2/sda1-8
  10:01:34 PM     0      3343     -1.00     -1.00     -1.00      78  kworker/u256:1
  10:01:34 PM  1000      3429      0.00  50132.00      0.00      98  stress-ng-hdd
  10:01:34 PM  1000      3430      0.00      0.00      0.00     100  stress-ng-hdd
  10:01:34 PM  1000      3431      0.00      0.00      0.00      78  stress-ng-hdd
  10:01:34 PM  1000      3432      0.00      0.00      0.00      94  stress-ng-hdd
  总结
  stress-ng工具,在性能测试,或压力测试场景,是一个很实用的工具。希望大家能够掌握基本用法。
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号