【原】Nginx添加Content-MD5頭部壓測分析


如需轉載,必須注明原文地址,請尊重作者勞動成果。 http://www.cnblogs.com/lyongerr/p/5048464.html

 本文介紹了webbenck安裝,但是最后使用的是ab工具進行壓測,安裝apache以后,就自帶了ab工具。

1 webbench工具安裝

1.1 簡介

Webbench是知名的網站壓力測試工具,它是由Lionbridge公司(http://www.lionbridge.com)開發。Webbench能測試處在相同硬件上,不同服務的性能以及不同硬件上同一個服務的運行狀況。webbench的標准測試可以向我們展示服務器的兩項內容:每秒鍾相應請求數和每秒鍾傳輸數據量。webbench不但能具有便准靜態頁面的測試能力,還能對動態頁面(ASP,PHP,JAVA,CGI)進 行測試的能力。還有就是他支持對含有SSL的安全網站例如電子商務網站進行靜態或動態的性能測試。Webbench最多可以模擬3萬個並發連接去測試網站的負載能力。

1.2 安裝

#wget http://home.tiscali.cz/~cz210552/webbench.html/webbench-1.5.tar.gz

#tar zxvf webbench-1.5.tar.gz

#cd webbench-1.5

#make

#make install

1.3 webbench使用

webbench -c 1000 -t 60 http://test.my4399.com/download/md5.html

webbench -c 並發數 -t 運行測試時間 URL

2 nginx添加file-md5模塊

2.1 簡介

HTTP協議新增了Content-MD5 HTTP頭,但是nginx並不支持這個功能,而且官方也明確表示不會增加這項功能,為什么呢?因為每次請求都需要讀取整個文件來計算MD5值,以性能著稱的nginx絕對不願意干出違背軟件宗旨的事情。但是有些應用中,需要驗證文件的正確性,有些人通過下載當前文件,然后計算MD5值來比對當前文件是否正確。不僅僅浪費帶寬資源也浪費了大把的時間。有需求就有解決方案,網友開發了file-md5模塊。

2.2 下載file-md5模塊

#cd /usr/local/src

# wget https://github.com/cfsego/file-md5/archive/master.zip -O file-md5-master.zip

# unzip file-md5-master.zip

2.3 下載nginx-1.4.2

# wget http://nginx.org/download/nginx-1.4.2.tar.gz

# tar -xzf nginx-1.4.2.tar.gz

# cd nginx-1.4.2

2.4 下載pcre-8.38.tar.gz

tar xvf pcre-8.38.tar.gz -C /usr/local/src/

2.5 編譯file-md5模塊

#./configure --prefix=/usr/local/nginx-1.4.2 --add-module=../file-md5-master --with-pcre=../pcre-8.38/ --with-openssl=/usr/include

#make

#make install

2.6 配置vhost文件

server {

listen 80;

server_name test.my4399.com;

root /data/web/test;

# for add content-md5 to http header

location ~ /download

{

add_header Content-MD5 $file_md5;

}

}

2.7 啟動nginx

#cd /usr/local/nginx-1.4.2

#./sbin/nginx -c conf/nginx.conf

2.8 測試Content-MD5功能

image

3 nginx添加perl模塊

https://gist.github.com/sivel/1870822

3.1 准備perl模塊相關文件

#cd /usr/local/nginx-1.4.2/

將以下ContentMD5.pm文件存放於/usr/local/nginx-1.4.2/perl/lib/下,文件名必須是ContentMD5.pm

# nginx Embedded Perl module for adding a Content-MD5 HTTP header

#

# This perl module, will output an MD5 of a requested file using the

# Content-MD5 HTTP header, by either pulling it from a file of the

# same name with .md5 appended to the end, if it exists, or will

# calculate the MD5 hex hash on the fly

#

# Author: Matt Martz <matt@sivel.net>

# Link: https://gist.github.com/1870822#file_content_md5.pm

# License: http://www.nginx.org/LICENSE

package ContentMD5;

use nginx;

use Digest::MD5;

sub handler {

my $r = shift;

my $filename = $r->filename;

return DECLINED unless -f $filename;

my $content_length = -s $filename;

my $md5;

if ( -f "$filename.md5" ) {

open( MD5FILE, "$filename.md5" ) or return DECLINED;

$md5 = <MD5FILE>;

close( MD5FILE );

$md5 =~ s/^\s+//;

$md5 =~ s/\s+$//;

$md5 =~ s/\ .*//;

} else {

open( FILE, $filename ) or return DECLINED;

my $ctx = Digest::MD5->new;

$ctx->addfile( *FILE );

$md5 = $ctx->hexdigest;

close( FILE );

}

$r->header_out( "Content-MD5", $md5 ) unless ! $md5;

return DECLINED;

}

1;

3.2 編譯

#cd /usr/local/src/nginx-1.4.2

# ./configure --prefix=/usr/local/nginx-1.4.2 --with-pcre=../pcre-8.38/ --with-openssl=/usr/include --with-http_stub_status_module --with-http_perl_module --with-http_addition_module --with-http_realip_module --with-http_sub_module

#make

#make install

3.3 編輯nginx.conf

在http字段里面添加

perl_modules perl/lib;

perl_require ContentMD5.pm;

3.4 編輯vhost

server {

listen 80;

server_name test.my4399.com;

root /data/web/test;

location /download {

perl ContentMD5::handler;

}

}

3.5 啟動nginx

#chown www.www /usr/local/nginx-1.4.2 -R

#cd /usr/local/nginx-1.4.2

#./sbin/nginx -c conf/nginx.conf

3.6 測試Content-MD5功能

image

4 壓測分析

測試文件的大小均為同一文件,15M。基於同一文件,均使用ab工具共請求1000次,每次1000並發。

4.1 使用file-md5模塊

ab -n 1000 -c 1000 http://test.my4399.com/download/test.html

4.2 使用perl模塊

ab -n 1000 -c 1000 http://test.my4399.com/download/test.html

4.3 數據統計

4.3.1 file-md5

ab -n 1000 -c 1000 http://test.my4399.com/download/test.html

指標

第一次

第二次

第三次

第四次

第五次

第六次

平均值

Failed requests

0

0

1

1

1

0

0.5

longest request

145177ms

132901ms

139602ms

129589ms

137383ms

133433ms

136347.5ms

ab -n 200 -c 200 http://test.my4399.com/download/test.html

指標

第一次

第二次

第三次

第四次

第五次

第六次

平均值

Failed requests

0

0

0

0

0

0

0

longest request

24757ms

23835ms

24284ms

24207ms

24380ms

24837ms

24383.3ms

4.3.2 perl模塊

ab -n 1000 -c 1000 http://test.my4399.com/download/test.html

指標

第一次

第二次

第三次

第四次

第五次

第六次

平均值

Failed requests

0

0

0

1

78

1

 

longest request

160995ms

158246ms

158178ms

168085ms

154494ms

163422ms

160570ms

ab -n 200 -c 200 http://test.my4399.com/download/test.html

指標

第一次

第二次

第三次

第四次

第五次

第六次

平均值

Failed requests

0

0

0

0

0

0

0

longest request

31651ms

33463ms

30368ms

29941ms

29749ms

29430ms

30767ms

從上圖可以看出,logest request time 相差 6383.7ms,也就是6秒多。

5 參考資料

http://www.ttlsa.com/nginx/nginx-modules-ngx_file_md5

https://gist.github.com/sivel/1870822


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM