在运维工作中,压力测试是一项非常重要的工作。比如在一个网站上线之前,能承受多大访问量、在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验。

  但是,在压力测试中存在一个共性,那就是压力测试的结果与实际负载结果不会完全相同,就算压力测试工作做的再好,也不能保证100%和线上性能指标相同。面对这些问题,我们只能尽量去想方设法去模拟。所以,压力测试非常有必要,有了这些数据,我们就能对自己做维护的平台做到心中有数。

  目前较为常见的网站压力测试工具有webbench、ab(apache bench)、tcpcopy、loadrunner。

  webbench由Lionbridge公司开发,主要测试每秒钟请求数和每秒钟数据传输量,同时支持静态、动态、SSL,部署简单,静动态均可测试。适用于小型网站压力测试(单例最多可模拟3万并发) 。

  ab(apache bench)Apache自带的压力测试工具,主要功能用于测试网站每秒钟处理请求个数,多见用于静态压力测试,功能较弱,非专业压力测试工具。

  tcpcopy基于底层应用请求复制,可转发各种在线请求到测试服务器,具有分布式压力测试功能,所测试数据与实际生产数据较为接近后起之秀,主要用于中大型压力测试,所有基于tcp的packets均可测试。

  loadrunner压力测试界的泰斗,可以创建虚拟用户,可以模拟用户真实访问流程从而录制成脚本,其测试结果也最为逼真模拟最为逼真,并可进行独立的单元测试,但是部署配置较为复杂,需要专业人员才可以。

  下面,笔者就以webbench为例,来讲解一下网站在上线之前压力测试是如何做的。

安装webbench

<span class="sh_preproc" style="color: rgb(160, 32, 240);">#wget</span> http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz</span>
<span class="sh_preproc" style="color: rgb(160, 32, 240);">#tar</span> <span class="sh_usertype">zxvf</span><span class="sh_normal"> </span>webbench<span class="sh_number" style="color: rgb(255, 0, 255);">-1.5</span><span class="sh_symbol">.</span>tar<span class="sh_symbol">.</span>gz
<span class="sh_preproc" style="color: rgb(160, 32, 240);">#cd</span> webbench<span class="sh_number" style="color: rgb(255, 0, 255);">-1.5</span>
<span class="sh_preproc" style="color: rgb(160, 32, 240);">#make</span> <span class="sh_symbol">&&</span> make install

进行压力测试,并发200时。

<span class="sh_preproc" style="color: rgb(160, 32, 240);"># webbench</span> <span class="sh_symbol">-</span>c <span class="sh_number" style="color: rgb(255, 0, 255);">200</span> <span class="sh_symbol">-</span>t <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>

参数解释:-c为并发数,-t为时间(秒)

Webbench <span class="sh_symbol">-</span> Simple Web Benchmark <span class="sh_number" style="color: rgb(255, 0, 255);">1.5</span>
<span class="sh_function" style="font-weight: bold;">Copyright</span> <span class="sh_symbol">(</span>c<span class="sh_symbol">)</span> Radim Kolar <span class="sh_number" style="color: rgb(255, 0, 255);">1997-2004</span><span class="sh_symbol">,</span> GPL Open <span class="sh_usertype">Source</span><span class="sh_normal"> </span>Software<span class="sh_symbol">.</span>
Benchmarking<span class="sh_symbol">:</span> <span class="sh_usertype">GET</span><span class="sh_normal"> </span>http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
<span class="sh_number" style="color: rgb(255, 0, 255);">200</span> clients<span class="sh_symbol">,</span> running <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> sec<span class="sh_symbol">.</span>
Speed<span class="sh_symbol">=</span><span class="sh_number" style="color: rgb(255, 0, 255);">1454</span> pages<span class="sh_symbol">/</span>min<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">2153340</span> bytes<span class="sh_symbol">/</span>sec<span class="sh_symbol">.</span>
Requests<span class="sh_symbol">:</span> <span class="sh_number" style="color: rgb(255, 0, 255);">1454</span> susceed<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">0</span> failed<span class="sh_symbol">.</span>

当并发200时,网站访问速度正常

并发800时

<span class="sh_preproc" style="color: rgb(160, 32, 240);">#webbench</span> <span class="sh_symbol">-</span>c <span class="sh_number" style="color: rgb(255, 0, 255);">800</span> <span class="sh_symbol">-</span>t <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
Webbench <span class="sh_symbol">-</span> Simple Web Benchmark <span class="sh_number" style="color: rgb(255, 0, 255);">1.5</span>
<span class="sh_function" style="font-weight: bold;">Copyright</span> <span class="sh_symbol">(</span>c<span class="sh_symbol">)</span> Radim Kolar <span class="sh_number" style="color: rgb(255, 0, 255);">1997-2004</span><span class="sh_symbol">,</span> GPL Open <span class="sh_usertype">Source</span><span class="sh_normal"> </span>Software<span class="sh_symbol">.</span>
Benchmarking<span class="sh_symbol">:</span> <span class="sh_usertype">GET</span><span class="sh_normal"> </span>http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
<span class="sh_number" style="color: rgb(255, 0, 255);">800</span> clients<span class="sh_symbol">,</span> running <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> sec<span class="sh_symbol">.</span>
Speed<span class="sh_symbol">=</span><span class="sh_number" style="color: rgb(255, 0, 255);">1194</span> pages<span class="sh_symbol">/</span>min<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">2057881</span> bytes<span class="sh_symbol">/</span>sec<span class="sh_symbol">.</span>
Requests<span class="sh_symbol">:</span> <span class="sh_number" style="color: rgb(255, 0, 255);">1185</span> susceed<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">9</span> failed<span class="sh_symbol">.</span>

当并发连接为800时,网站访问速度稍慢

并发1600时

<span class="sh_preproc" style="color: rgb(160, 32, 240);">#webbench</span> <span class="sh_symbol">-</span>c <span class="sh_number" style="color: rgb(255, 0, 255);">1600</span> <span class="sh_symbol">-</span>t <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
Webbench <span class="sh_symbol">-</span> Simple Web Benchmark <span class="sh_number" style="color: rgb(255, 0, 255);">1.5</span>
<span class="sh_function" style="font-weight: bold;">Copyright</span> <span class="sh_symbol">(</span>c<span class="sh_symbol">)</span> Radim Kolar <span class="sh_number" style="color: rgb(255, 0, 255);">1997-2004</span><span class="sh_symbol">,</span> GPL Open <span class="sh_usertype">Source</span><span class="sh_normal"> </span>Software<span class="sh_symbol">.</span>
Benchmarking<span class="sh_symbol">:</span> <span class="sh_usertype">GET</span><span class="sh_normal"> </span>http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
<span class="sh_number" style="color: rgb(255, 0, 255);">1600</span> clients<span class="sh_symbol">,</span> running <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> sec<span class="sh_symbol">.</span>
Speed<span class="sh_symbol">=</span><span class="sh_number" style="color: rgb(255, 0, 255);">1256</span> pages<span class="sh_symbol">/</span>min<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">1983506</span> bytes<span class="sh_symbol">/</span>sec<span class="sh_symbol">.</span>
Requests<span class="sh_symbol">:</span> <span class="sh_number" style="color: rgb(255, 0, 255);">1183</span> susceed<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">73</span> failed<span class="sh_symbol">.</span>

当并发连接为1600时,网站访问速度便非常慢了

并发2000时

<span class="sh_preproc" style="color: rgb(160, 32, 240);">#webbench</span> <span class="sh_symbol">-</span>c <span class="sh_number" style="color: rgb(255, 0, 255);">2000</span> <span class="sh_symbol">-</span>t <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
Webbench <span class="sh_symbol">-</span> Simple Web Benchmark <span class="sh_number" style="color: rgb(255, 0, 255);">1.5</span>
<span class="sh_function" style="font-weight: bold;">Copyright</span> <span class="sh_symbol">(</span>c<span class="sh_symbol">)</span> Radim Kolar <span class="sh_number" style="color: rgb(255, 0, 255);">1997-2004</span><span class="sh_symbol">,</span> GPL Open <span class="sh_usertype">Source</span><span class="sh_normal"> </span>Software<span class="sh_symbol">.</span>
Benchmarking<span class="sh_symbol">:</span> <span class="sh_usertype">GET</span><span class="sh_normal"> </span>http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
<span class="sh_number" style="color: rgb(255, 0, 255);">2000</span> clients<span class="sh_symbol">,</span> running <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> sec<span class="sh_symbol">.</span>
Speed<span class="sh_symbol">=</span><span class="sh_number" style="color: rgb(255, 0, 255);">2154</span> pages<span class="sh_symbol">/</span>min<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">1968292</span> bytes<span class="sh_symbol">/</span>sec<span class="sh_symbol">.</span>
Requests<span class="sh_symbol">:</span> <span class="sh_number" style="color: rgb(255, 0, 255);">2076</span> susceed<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">78</span> failed<span class="sh_symbol">.</span>

当并发2000时,网站便出现"502 Bad Gateway",由此可见web服务器已无法再处理用户访问请求

总结:

1、压力测试工作应该放到产品上线之前,而不是上线以后

2、测试时尽量跨公网进行,而不是内网

3、测试时并发应当由小逐渐加大,比如并发100时观察一下网站负载是多少、打开是否流程,并发200时又是多少、网站打开缓慢时并发是多少、网站打不开时并发又是多少

4、 应尽量进行单元测试,如B2C网站可以着重测试购物车、推广页面等,因为这些页面占整个网站访问量比重较大

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐