Linux系统架构-----Apache的ab压力测试

一.ab工具的测试的概述

  • ab工具是apache自带的压力测试工具,可以模拟多线程并发请求,测试服务器负载压力
  • ab工具还可以其他类型的服务器进行压力测试,如nginx、tomcat、IIS
  • ab对发出负载的计算机要求很低,但是会对目标服务器造成巨大的负载
  • 在带宽不足的情况下,最好是本机测试,远程对web服务器进行压力测试,网络延时过大或带宽不足,得到的测试结果并不准确

二.ab工具命令格式和结果参数

  • ab的命令格式是:ab [options] [http://] hostname [:port] /path
  • "[options]",表示ab工具的参数
  • “[http]" :表示http前缀可以省略
  • “hostname”:表示访问的主机
  • “port” : 表示端口号可以省略
  • “/path ” 表示请求的资源路劲

      ab命令参数表

参数 描述
-n 测试会话中所执行的请求总数,默认是仅执行一个请求
-t 测试所进行的最大时间(s)
-c 并发产生的请求个数,默认是一次一个
-v 设置显示信息的详细程度

ab测试结果参数表

参数 描述
Server Software http响应数据的头信息
Server Hostname 请求的地址中的主机名称
Server port web服务器软件的监听端口
Document path 请求的url根的绝对路径
Document Length http响应数据的正文长度
Concurrency Level 并发用户数量
Time taken for tests 所有请求处理完成的时间
Complete requests 总的请求数量
Failed transferred 失败的请求数量
Total transferred 请求的响应数据长度总和
requests per second 服务的吞吐率,每秒处理的请求数
Time per request 用户平均的等待时间

注:我们一般查看ab测试的结果,是查看所有请求处理的完成的时间

三.ab测试的具体操作

  • 手工安装apache服务
#获取apache的相关软件包
#解压到/opt目录下
tar zxvf apr-1.6.2.tar.gz -C /opt
tar zxvf apr-util-1.6.0.tar.gz -C /opt
tar jxvf httpd-2.4.29.tar.bz2 -C /opt

#将apr和apr-util两个组件放到httpd下的srclib目录中去
cd /opt
mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
mv apr-1.6.2/ httpd-2.4.29/srclib/apr

#安装环境包,其中gcc和gcc-c++,perl是编译软件,zlib是压缩工具,make是编译工具
yum install gcc gcc-c++ pcre pcre-devel zlib-devel expat-devel make perl -y

#配置apache服务,prefix表示安装路径,deflate表示压缩模块,rewrite表示重写模块,cgi表示通用网关接口,charset-lite是字符集
./configure \
--prefix=/usr/local/httpd \
--enable-deflate \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi 

#编译且安装
make && make install

#优化apache的开启方式
grep -v "#" /usr/local/httpd/bin/apachectl > /etc/init.d/httpd
vim /etc/init.d/httpd
#!/bin/bash
# chkconfig:2345 85 15
# description:Apache is a World Wide Web server.
chmod +x /etc/init.d/httpd                //添加执行权限
chkconfig --add httpd                     //添加到执行列表
chkconfig --list httpd
chkconfig --level 35 httpd on              //添加开启自启


#编辑主配置文件
vim /usr/local/httpd/conf/httpd.conf

Listen 192.168.43.217:80
#Listen 80
ServerName www.kgc.com:80

#添加本机的域名,为ab压测做准备
echo "192.168.43.217  www.kgc.com" >> /etc/hosts

#在apache的主页面中加入图片
vim /usr/local/httpd/htdocs/index.html
<html><body><h1>It works!</h1>
<img src="error.png"/>
<img src="1.jpg"/>
</body></html>

#关闭防火墙,开启服务
systemctl stop firewalld
setenforce 0
service httpd start
  • 页面验证

  • 未优化的ab测试
[root@localhost bin]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@localhost bin]# ab -n 2000 -c 800 www.kgc.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.kgc.com (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests


Server Software:        Apache/2.4.29
Server Hostname:        www.kgc.com
Server Port:            80

Document Path:          /index.html
Document Length:        107 bytes

Concurrency Level:      800
Time taken for tests:   20.285 seconds
Complete requests:      2000
Failed requests:        129
   (Connect: 0, Receive: 0, Length: 129, Exceptions: 0)
Total transferred:      659296 bytes
HTML transferred:       200411 bytes
Requests per second:    98.60 [#/sec] (mean)
Time per request:       8113.821 [ms] (mean)
Time per request:       10.142 [ms] (mean, across all concurrent requests)
Transfer rate:          31.74 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    7   8.7      3      31
Processing:    10 1307 4877.0     16   20031
Waiting:        1   46  74.8     11     218
Total:         14 1314 4875.3     19   20031

Percentage of the requests served within a certain time (ms)
  50%     19
  66%     24
  75%     41
  80%     47
  90%    251
  95%  20029
  98%  20030
  99%  20031
 100%  20031 (longest request)
[root@localhost bin]# 

Time taken for tests:   20.285 seconds,总的请求时间为20s左右

  • 优化apache
vim /usr/local/httpd/conf/httpd.conf
#去掉deflate模块前的#号
LoadModule deflate_module modules/mod_deflate.so

#在主配置文件最后加入
<IfModule mod_deflate.c>
	AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png
	DeflateCompressionLevel 6
	SetOutputFilter DEFLATE

#重启服务
[root@localhost bin]# service httpd stop
[root@localhost bin]# service httpd start 
[root@localhost bin]# 
  • 优化之后的ab测试
[root@localhost bin]# ab -n 2000 -c 800 www.kgc.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.kgc.com (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests


Server Software:        Apache/2.4.29
Server Hostname:        www.kgc.com
Server Port:            80

Document Path:          /index.html
Document Length:        107 bytes

Concurrency Level:      800
Time taken for tests:   0.704 seconds
Complete requests:      2000
Failed requests:        0
Total transferred:      750000 bytes
HTML transferred:       214000 bytes
Requests per second:    2841.66 [#/sec] (mean)
Time per request:       281.525 [ms] (mean)
Time per request:       0.352 [ms] (mean, across all concurrent requests)
Transfer rate:          1040.65 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    7   6.8      4      23
Processing:     4   64  99.7     17     466
Waiting:        4   61 100.2     12     464
Total:          8   71 102.6     20     486

Percentage of the requests served within a certain time (ms)
  50%     20
  66%     34
  75%     45
  80%     56
  90%    244
  95%    246
  98%    454
  99%    485
 100%    486 (longest request)
[root@localhost bin]# 

Time taken for tests:   0.704 seconds,总的花费时间为0.7s,由于测试有误差需要多次压测后,取平均值

发布了94 篇原创文章 · 获赞 108 · 访问量 6405

猜你喜欢

转载自blog.csdn.net/qq_42761527/article/details/103599187