客观侠 發表於 2020-10-22 13:35:00

Linux:linux服务器稳定性压力测试工具stress安装与使用

<p><span style="font-size: 18px"><strong>stress是一个linux下的压力测试工具,专门为那些想要测试自己的系统,完全高负荷和监督这些设备运行的用户。</strong></span></p>
<h3><span style="font-size: 18px"><strong>1. stress1.0.4下载地址</strong></span></h3>
<p><span style="font-size: 16px"><strong>下载:https://fossies.org/linux/privat/stress-1.0.4.tar.gz</strong></span></p>
<h3><span style="font-size: 18px">2.安装stress</span></h3>
<pre><span style="font-size: 16px"><strong>将下载的stress-1.0.4.tar.gz复制到“/usr/local/src/”目录下</strong></span></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#切换目录
cd </span>/usr/local/<span style="color: rgba(0, 0, 0, 1)">src

#解压tar.gz包
</span><span style="color: rgba(0, 0, 255, 1)">tar</span> -xzpvf stress-<span style="color: rgba(128, 0, 128, 1)">1.0</span>.<span style="color: rgba(128, 0, 128, 1)">4</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span><span style="color: rgba(0, 0, 0, 1)">.gz

#进入解压目录
cd stress</span>-<span style="color: rgba(128, 0, 128, 1)">1.0</span>.<span style="color: rgba(128, 0, 128, 1)">4</span><span style="color: rgba(0, 0, 0, 1)">

#开始安装
.</span>/<span style="color: rgba(0, 0, 0, 1)">configure

</span><span style="color: rgba(0, 0, 255, 1)">make</span>

<span style="color: rgba(0, 0, 255, 1)">make</span><span style="color: rgba(0, 0, 0, 1)"> check

</span><span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span>

<span style="color: rgba(0, 0, 255, 1)">make</span> clean</pre>
</div>
<h3><span style="font-size: 18px"><strong>&nbsp;3.stress使用说明</strong></span></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">Stress 选项说明:

</span>-?

--<span style="color: rgba(0, 0, 0, 1)">help #显示帮助信息

</span>----------------------------------------------

--<span style="color: rgba(0, 0, 0, 1)">version #显示软件版本信息

</span>----------------------------------------------

-<span style="color: rgba(0, 0, 0, 1)">v

</span>--<span style="color: rgba(0, 0, 0, 1)">verbose #显示详细运行信息

</span>----------------------------------------------

-<span style="color: rgba(0, 0, 0, 1)">q

</span>--<span style="color: rgba(0, 0, 0, 1)">quiet#不显示运行信息

</span>----------------------------------------------

-<span style="color: rgba(0, 0, 0, 1)">n

</span>--dry-<span style="color: rgba(0, 0, 0, 1)">run#显示已经完成的指令执行情况

</span>----------------------------------------------

-<span style="color: rgba(0, 0, 0, 1)">t secs

</span>--<span style="color: rgba(0, 0, 0, 1)">timeout secs#指定运行多少秒

</span>----------------------------------------------

--<span style="color: rgba(0, 0, 0, 1)">backoff usecs#等待usecs微秒后才开始执行

</span>----------------------------------------------

-<span style="color: rgba(0, 0, 0, 1)">c forks

</span>--<span style="color: rgba(0, 0, 0, 1)">cpu forks#产生多个处理sqrt()函数的CPU进程

</span>----------------------------------------------

-<span style="color: rgba(0, 0, 0, 1)">i forks

</span>--io forks#产生多个处理sync()函数的磁盘I/<span style="color: rgba(0, 0, 0, 1)">O进程

</span>----------------------------------------------

-<span style="color: rgba(0, 0, 0, 1)">m forks

</span>--<span style="color: rgba(0, 0, 0, 1)">vm forks#产生多个处理malloc()内存分配函数的进程

</span>----------------------------------------------

--vm-bytes bytes#指定内存的Bytes数,默认值是1(与--vm-<span style="color: rgba(0, 0, 0, 1)">hang配合使用)

</span>----------------------------------------------

--vm-<span style="color: rgba(0, 0, 0, 1)">hang#指示每个消耗内存的进程在分配到内存后转入休眠状态,与正常的无限分配和释放内存的处理相反,这有利于模拟只有少量内存的机器;例如,下面命令运行时就会分配到并一直持有256M内存,直到进程被终止;

</span>% stress --vm <span style="color: rgba(128, 0, 128, 1)">2</span> --vm-bytes 128M --vm-<span style="color: rgba(0, 0, 0, 1)">hang

</span>----------------------------------------------

-<span style="color: rgba(0, 0, 0, 1)">d forks

</span>--<span style="color: rgba(0, 0, 0, 1)">hdd forks#产生多个执行write()函数的进程

</span>----------------------------------------------

--hdd-<span style="color: rgba(0, 0, 0, 1)">bytes bytes#指定写的Bytes数,默认是1GB

</span>----------------------------------------------

--hdd-<span style="color: rgba(0, 0, 0, 1)">noclean#不要将写入随机ASCII数据的文件Unlink
注意:时间单位可以为秒s,分m,小时h,天d,年y,文件大小单位可以为K,M,G

示例:
一个简单的例子就是你只想给系统一个随机负载,此命令产生13个进程,每个进程都反复不停的计算由rand()产生随机数的平方根

</span>----------------------------------------------

% stress -c <span style="color: rgba(128, 0, 128, 1)">13</span><span style="color: rgba(0, 0, 0, 1)">#Stress支持很多选项,可以指定仅显示有限的运行信息,以下命令产生1024个进程,仅显示出错信息

</span>----------------------------------------------

% stress --quiet --cpu 1k#为了观察系统何时达到I/O极限,可以使用选项“-i”,以下指令产生4个进程,每个进程反复调用sync(),<span style="color: rgba(0, 0, 255, 1)">sync</span><span style="color: rgba(0, 0, 0, 1)">()用于将内存上的内容写到硬盘上

</span>----------------------------------------------

% stress -i <span style="color: rgba(128, 0, 128, 1)">4</span>#可以在一条指令中使用多个消耗资源的选项,如下指令表示显示运行信息,一分钟后终止运行(13个CPU进程,4个I/<span style="color: rgba(0, 0, 0, 1)">O进程)

</span>----------------------------------------------

% stress -c <span style="color: rgba(128, 0, 128, 1)">13</span> -i <span style="color: rgba(128, 0, 128, 1)">4</span> --verbose --timeout 1m#你也可以指定向磁盘中写入固定大小的文件,这个文件通过调用mkstemp()产生并保存在当前目录下,默认是文件产生后就被执行unlink(清除)操作,但是你可以使用“--hdd-<span style="color: rgba(0, 0, 0, 1)">bytes”选项将产生的文件全部保存在当前目录下,这会将你的磁盘空间逐步耗尽

</span>----------------------------------------------

% stress -d <span style="color: rgba(128, 0, 128, 1)">1</span> --hdd-noclean --hdd-bytes <span style="color: rgba(128, 0, 128, 1)">13</span><span style="color: rgba(0, 0, 0, 1)">#可以支持生成大文件

</span>% stress -d <span style="color: rgba(128, 0, 128, 1)">1</span> --hdd-noclean --hdd-<span style="color: rgba(0, 0, 0, 1)">bytes 3G

</span>----------------------------------------------</pre>
</div>
<h3><span style="font-size: 18px"><strong>4.测试方式</strong></span></h3>
<p><span style="font-size: 16px; color: rgba(255, 153, 0, 1)"><strong>建议开启三个</strong><strong>&nbsp;Xshell连接终端。</strong></span></p>
<p><span style="font-size: 16px"><strong><span style="color: rgba(0, 128, 0, 1)">第一个终端</span>:</strong></span></p>
<p><span style="font-size: 16px; color: rgba(0, 0, 255, 1)"><strong>测试一</strong></span></p>
<p><span style="font-size: 16px"><strong>运行 stress 命令,模拟一个 CPU 使用率 100%,运行10分钟的场景</strong></span></p>
<div class="cnblogs_code">
<pre>#一个cpu使用率100%<span style="color: rgba(0, 0, 0, 1)">,运行10分钟场景
stress </span>--cpu <span style="color: rgba(128, 0, 128, 1)">1</span> --timeout <span style="color: rgba(128, 0, 128, 1)">600</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/1582099/202010/1582099-20201022125035645-1246534374.png" alt="" loading="lazy"></p>
<p><span style="font-size: 16px; color: rgba(0, 0, 255, 1)"><strong>测试二</strong></span></p>
<p><span style="font-size: 16px"><strong>主要测试硬盘,生成文件放到硬盘内</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#var下创建测试目录
</span><span style="color: rgba(0, 0, 255, 1)">mkdir</span> -p /var/<span style="color: rgba(0, 0, 0, 1)">test

#切换到test目录
cd </span>/var/<span style="color: rgba(0, 0, 0, 1)">test

#测试硬盘,生成800k大小的文件,写到硬盘内
stress </span>-d <span style="color: rgba(128, 0, 128, 1)">1</span> --hdd-noclean --hdd-bytes 800k </pre>
</div>
<p><span style="font-size: 16px"><strong><span style="color: rgba(0, 0, 255, 1)">测试三</span></strong></span></p>
<p><span style="font-size: 16px"><strong>提升cpu与cpu使用率的测试 ,生成文件放到硬盘内</strong></span></p>
<div class="cnblogs_code">
<pre>#还是切换到 /var/test/<span style="color: rgba(0, 0, 0, 1)"> 测试目录下,没有的新建一个
cd </span>/var/<span style="color: rgba(0, 0, 0, 1)">test

#增大“c”值提高cpu数;增大“i”值提高CPU的使用率;增大“d”值会减少CPU的使用率,从而增加对硬盘的数据写入量,但是对内存要求不高
stress </span>-c <span style="color: rgba(128, 0, 128, 1)">4</span> -i <span style="color: rgba(128, 0, 128, 1)">4</span> -d <span style="color: rgba(128, 0, 128, 1)">1</span> --hdd-noclean --hdd-bytes 800k</pre>
</div>
<p><span style="font-size: 16px; color: rgba(0, 0, 255, 1)"><strong>测试四</strong></span></p>
<p><span style="font-size: 16px"><strong>提升vm值,测试内存使用率,生成文件放到硬盘内</strong></span></p>
<div class="cnblogs_code">
<pre>#还是切换到 /var/test/<span style="color: rgba(0, 0, 0, 1)"> 测试目录下,没有的新建一个
cd </span>/var/<span style="color: rgba(0, 0, 0, 1)">test

#增大“vm”、“</span>--vm-bytes” 或 “—vm-<span style="color: rgba(0, 0, 0, 1)">hang” 选项的值都会提高内存的使用率
stress </span>-c <span style="color: rgba(128, 0, 128, 1)">1</span> -i <span style="color: rgba(128, 0, 128, 1)">1</span> --vm <span style="color: rgba(128, 0, 128, 1)">6</span> --vm-bytes 128M -d <span style="color: rgba(128, 0, 128, 1)">2</span> --hdd-noclean --hdd-<span style="color: rgba(0, 0, 0, 1)">bytes 800k

#注意:
#运行过程中,您可以通过进入其他终端,执行“</span><span style="color: rgba(0, 0, 255, 1)">df</span>”命令,查看磁盘是否已满(也就是/var/<span style="color: rgba(0, 0, 0, 1)">test是否已满),如果磁盘已满,则程序“stress”会报错并终止。
#如果报错终止执行以下操作,清空测试目录,如果有需要;进入到测试目录下(</span>/var/<span style="color: rgba(0, 0, 0, 1)">test或者别的目录),继续运行“stress”进行测试

#给</span>/var/<span style="color: rgba(0, 0, 0, 1)">test赋权限
</span><span style="color: rgba(0, 0, 255, 1)">chmod</span> -R <span style="color: rgba(128, 0, 128, 1)">755</span> /var/<span style="color: rgba(0, 0, 0, 1)">test

#清空目录
</span><span style="color: rgba(0, 0, 255, 1)">rm</span> -R -f /var/test/<span style="color: rgba(0, 0, 0, 1)">

#切换目录
cd </span>/var/test/<span style="color: rgba(0, 0, 0, 1)">

#接着测试(vm</span>-<span style="color: rgba(0, 0, 0, 1)">bytes可以下降点继续测试)
stress </span>-c <span style="color: rgba(128, 0, 128, 1)">1</span> -i <span style="color: rgba(128, 0, 128, 1)">1</span> --vm <span style="color: rgba(128, 0, 128, 1)">6</span> --vm-bytes 128M -d <span style="color: rgba(128, 0, 128, 1)">2</span> --hdd-noclean --hdd-bytes 800k</pre>
</div>
<p>&nbsp;</p>
<p><strong><span style="color: rgba(0, 128, 0, 1); font-size: 16px">第二个终端:</span></strong></p>
<p><span style="font-size: 16px"><strong>运行 uptime 查看平均负载的变化情况</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#查看平均负载变化
</span><span style="color: rgba(0, 0, 255, 1)">uptime</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/1582099/202010/1582099-20201022125234971-996137379.png" alt="" loading="lazy"></p>
<p><strong><span style="font-size: 16px">查看动态平均负载情况命令如下:</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#动态平均负载情况
watch </span>-d <span style="color: rgba(0, 0, 255, 1)">uptime</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 16px; color: rgba(0, 128, 0, 1)"><strong>第三个终端:</strong></span></p>
<p><span style="font-size: 16px; color: rgba(0, 0, 255, 1)"><strong><strong>mpstat命令监控cpu</strong></strong></span></p>
<p><span style="font-size: 16px"><strong>运行 mpstat 查看 CPU 使用率的变化情况</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#查看cpu使用率的变化情况
mpstat</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/1582099/202010/1582099-20201022125656859-1578277276.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>监控所有cpu,每隔一段时间输出一组数据</strong></span></p>
<div class="cnblogs_code">
<pre>#监控所有 CPU,后面数字 <span style="color: rgba(128, 0, 128, 1)">5</span> 表示间隔 <span style="color: rgba(128, 0, 128, 1)">5</span><span style="color: rgba(0, 0, 0, 1)"> 秒后输出一组数据
mpstat </span>-P ALL <span style="color: rgba(128, 0, 128, 1)">5</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/1582099/202010/1582099-20201022125925985-865245000.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px; color: rgba(0, 0, 255, 1)"><strong>pidstat命令监控cpu</strong></span></p>
<p><span style="font-size: 16px"><strong>pidstat是sysstat工具的一个命令,用于监控全部或指定进程的cpu、内存、线程、设备IO等系统资源的占用情况。</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#pidstat 的用法:

pidstat [ 选项 ] [ </span>&lt;时间间隔&gt; ] [ &lt;次数&gt; ]<br><br></pre>
<p>常用的参数:</p>
<ul>
<ul>
<li>-u:默认的参数,显示各个进程的cpu使用统计</li>
<li>-r:显示各个进程的内存使用统计</li>
<li>-d:显示各个进程的IO使用情况</li>
<li>-p:指定进程号</li>
<li>-w:显示每个进程的上下文切换情况</li>
<li>-t:显示选择任务的线程的统计信息外的额外信息</li>
<li>-T { TASK | CHILD | ALL }<br>这个选项指定了pidstat监控的。TASK表示报告独立的task,CHILD关键字表示报告进程下所有线程统计信息。ALL表示报告独立的task和task下面的所有线程。<br>注意:task和子线程的全局的统计信息和pidstat选项无关。这些统计信息不会对应到当前的统计间隔,这些统计信息只有在子线程kill或者完成的时候才会被收集。</li>
<li>-V:版本号</li>
<li>-h:在一行上显示了所有活动,这样其他程序可以容易解析。</li>
<li>-I:在SMP环境,表示任务的CPU使用率/内核数量</li>
<li>-l:显示命令名和所有参数</li>






</ul>





</ul>





</div>
<p><img src="https://img2020.cnblogs.com/blog/1582099/202010/1582099-20201022130302829-1948950910.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>查看所有CPU使用情况</strong></span></p>
<div class="cnblogs_code">
<pre>#查看所有进程的 CPU 使用情况( -u -<span style="color: rgba(0, 0, 0, 1)">p ALL)
#pidstat 和 pidstat </span>-u -<span style="color: rgba(0, 0, 0, 1)">p ALL 是等效的。
#pidstat 默认显示了所有进程的cpu使用率。

pidstat

pidstat </span>-u -p ALL</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>5秒查询一次,共查询一次,所有cpu使用情况</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#设置5秒间隔,共查询1次
pidstat </span><span style="color: rgba(128, 0, 128, 1)">5</span> <span style="color: rgba(128, 0, 128, 1)">1</span></pre>
</div>
<p><img src="https://img2020.cnblogs.com/blog/1582099/202010/1582099-20201022130620861-1314719827.png" alt="" loading="lazy"></p>
<h3><span style="font-size: 18px"><strong>5.卸载stress方式</strong></span></h3>
<p><span style="font-size: 18px"><strong>如果不想保留在服务器内,可以通过如下方式卸载</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">#卸载命令

</span><span style="color: rgba(0, 0, 255, 1)">rm</span> -R -f /usr/local/src/stress-<span style="color: rgba(128, 0, 128, 1)">0.18</span>.<span style="color: rgba(128, 0, 128, 1)">1</span>/

<span style="color: rgba(0, 0, 255, 1)">rm</span> -f /usr/local/bin/<span style="color: rgba(0, 0, 0, 1)">stress

</span><span style="color: rgba(0, 0, 255, 1)">rm</span> -f /usr/local/src/stress-<span style="color: rgba(128, 0, 128, 1)">0</span>[<span style="color: rgba(128, 0, 128, 1)">1</span>].<span style="color: rgba(128, 0, 128, 1)">18.1</span>.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz</pre>
</div>
<h3><span style="font-size: 18px"><strong>6.保存监测数据</strong></span></h3>
<p><span style="font-size: 16px"><strong>可以查看我的另一篇文章:</strong></span><span style="font-size: 16px"><strong>Linux:监测收集linux服务器性能数据工具Sysstat的使用与安装</strong></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px"><strong>文章整合至:https://blog.csdn.net/cojn52/article/details/89680093、https://www.cnblogs.com/wx170119/p/11411312.html</strong></span></p>

</div>
<div id="MySignature" role="contentinfo">
    -----------------------------------
作者:怒吼的萝卜
链接:http://www.cnblogs.com/nhdlb/
-----------------------------------<br><br>
来源:https://www.cnblogs.com/nhdlb/p/13857744.html
頁: [1]
查看完整版本: Linux:linux服务器稳定性压力测试工具stress安装与使用