- ab 是 Apache 自带的压力测试工具。ab 非常实用,它不仅可以对 apache 服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如 Nginx、Tomcat、IIS 等
- 实验环境:VMware Workstation 15.5、X Shell 6、Centos 7.6、Windows10专业版
- 本实验所用软件包版本:apr-1.6.2、apr-util-1.6.0、httpd-2.4.29、Fiddler4
- 软件包下载地址:apr、apr-util、httpd:https://wwa.lanzous.com/i2khafi4yzg
Fiddler4:https://wwa.lanzous.com/iiI72fcuoqh
1、关闭防火墙及核心防护:
systemctl stop firewalld
setenforce 0
★关闭防火墙后,要先安装DNS服务并进行域名解析,具体步骤在前几篇博文中已经多次详述请参考之前的文章,DNS设置解析完成再进行如下步骤(我这里解析的是www.test.com这个域名)
2、安装Apache服务必须的依赖包
yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
zlib-devel
3、进入httpd软件目录进行编译安装前配置
cd /opt/httpd-2.4.29/
进入目录后执行以下命令:
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate ## 必须要有!
4、编译&安装
make && make install
5、将ab工具建立软链接
ln -s /usr/local/httpd/bin/ab /usr/sbin/
6、建一个站点页面,在其中添加图片
cd /usr/local/httpd/htdocs/ ## 进入站点目录
上传一张jpg图片到该目录下,这里我上传一张cat.jpg
vim index.html ## 编辑站点页面文件
在文件末尾追加一行:
<img src="cat.jpg"/>
7、编辑http配置文件,将这三个模块开启
vim /usr/local/httpd/conf/httpd.conf
确保以下三个模块前面无注释符号#,如果有就删去:
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so
8、编辑完成后使用命令验证一下配置文件的语法
cd /usr/local/httpd/bin | httpd -t
如果返回值为:Syntax OK 就没有问题
9、开启服务(还是在步骤8的目录下直接执行命令)
./apachectl start
在win10机中打开浏览器,访问http://192.168.50.134/index.html,查看网页是否能正常打开
10、执行以下命令开始压力测试
ab -r -n 3000 -c 1000 www.test.com/index.html
11、稍等片刻测试结束,将测试结果中的 Time taken for tests 一项结果数据复制(测试5次取平均值)
[root@localhost bin]# ab -r -n 3000 -c 1000 www.test.com/index.html
测试结果如下:
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.test.com (be patient)
Completed 300 requests
Completed 600 requests
Completed 900 requests
Completed 1200 requests
Completed 1500 requests
Completed 1800 requests
Completed 2100 requests
Completed 2400 requests
Completed 2700 requests
Completed 3000 requests
Finished 3000 requests
Server Software:
Server Hostname: www.test.com
Server Port: 80
Document Path: /index.html
Document Length: 0 bytes
Concurrency Level: 1000
Time taken for tests: 50.423 seconds ## 看测试花费的时长大小
Complete requests: 3000
Failed requests: 6000
(Connect: 0, Receive: 3000, Length: 0, Exceptions: 3000)
Total transferred: 0 bytes
HTML transferred: 0 bytes
Requests per second: 59.50 [#/sec] (mean)
Time per request: 16807.637 [ms] (mean)
Time per request: 16.808 [ms] (mean, across all concurrent requests)
Transfer rate: 0.00 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 780 2303.7 0 16323
Processing: 10 13272 10129.2 21004 21035
Waiting: 0 0 0.0 0 0
Total: 253 14052 9339.9 21004 21035
Percentage of the requests served within a certain time (ms)
50% 21004
66% 21009
75% 21011
80% 21015
90% 21023
95% 21025
98% 21026
99% 21033
100% 21035 (longest request)
12、现在模拟对网站进行优化,开启gzip压缩功能
vim /usr/local/httpd/conf/httpd.conf ## 编辑http配置文件
在文件末尾追加写入以下内容开启gzip压缩:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/html text/javascript text/jpg text/png
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
</IfModule>
13、因为修改过了配置文件,所以要重启Apache服务(在是在步骤8的目录下执行以下命令)
./apachectl stop ## 停止服务
./apachectl start ## 开启服务
在win10机中打开浏览器,清空缓存并重启浏览器,重新访问http://192.168.50.134/index.html
通过抓包工具fiddler进行抓包可以看出图片已经经过gzip压缩了
14、再次进行ab测试
ab -r -n 3000 -c 1000 www.test.com/index.html
测试并记录五次测试结果
对比这10次测试结果,可以较明显看出开始gzip页面压缩后,测试时间明显减少了
【未开启gzip压缩】
Time taken for tests: 63.044 seconds
Time taken for tests: 42.104 seconds
Time taken for tests: 60.218 seconds
Time taken for tests: 63.095 seconds
Time taken for tests: 63.022 seconds
【开启gzip压缩后】
Time taken for tests: 42.100 seconds
Time taken for tests: 49.156 seconds
Time taken for tests: 57.101 seconds
Time taken for tests: 50.423 seconds
Time taken for tests: 49.108 seconds
所以说,在网站搭建完成后对其进行相应优化可以提高服务器的处理性能