【原】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

原文地址:https://www.cnblogs.com/lyongerr/p/5048464.html