简介
apache
ab是apachebench命令的缩写,ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。
简易安装-yum源安装
yum install httpd
启动
service httpd start #启动
service httpd stop #停止
Apache服务目录:/etc/httpd
源码安装方式
安装准备
apache官网
https://httpd.apache.org/ # apache官网
# 下载安装包
wget https://downloads.apache.org//httpd/httpd-2.4.49.tar.gz # apache源码包
wget https://dlcdn.apache.org//apr/apr-1.7.0.tar.gz #apache依赖
wget https://dlcdn.apache.org//apr/apr-util-1.6.1.tar.gz # apache依赖及apr依赖
# 解压安装包
tar -zxvf httpd-2.4.49.tar.gz # apache源码包解压
tar -zxvf apr-1.7.0.tar.gz # pache的依赖
tar -zxvf apr-util-1.6.1.tar.gz #apache的依赖,及apr的依赖
# 将两个依赖包移动到/httpd-2.4.49/srclib/下
mv apr-1.7.0 httpd-2.4.49/srclib/apr #注意路径
mv apr-util-1.6.1 httpd-2.4.49/srclib/apr-util #注意路径
安装编译工具
yum install -y gcc gcc-c++
安装源码包的依赖
yum -y install openssl openssl-devel zlib zlib-devel pcre pcre-devel
配置Apache
cd httpd-2.4.49/
# 编译解压的原文件
./configure --prefix=/usr/local/apache2
# 详细配置
./configure --prefix=/usr/local/apache2 --enable-rewrite --enable-so --enable-headers --enable-expires --with-mpm=worker --enable-modules=most --enable-deflate --enable-ssl
# --prefix 安装路径
# --enable-rewrite 开启地址重写
# --enable-so 开启 dso (动态共享对象)
# --enable-headers 允许修改 http 请求头部
# --enable-expires 允许客户端缓存
# --with-mpm=worker 指定当前的进程管理方式为 worker模式
# Apache进程工作方式有三种:prefork(默认)一个管理进程管理多个工作进程,每个工作进程管理一个线程,每个线程维持一个连接
# worker:一个管理进程管理多个工作进程,每个工作进程管理多个线程,每个线程维持一个连接
# event:会有一个专门的线程来管理这些 keep-alive 类型的线程,当有真实请求过来的时候,将请求传递给服务线程,执行完毕后,又允许它释放。这样,一个线程就能处理几个请求了,实现了异步非阻塞。
编译安装
make && make install
安装完成
# 路径
/usr/local/apache2
apache2下的重要目录
bin:存放Apache的系统命令
conf:存放Apache的配置文件
error :错误信息存放目录
htdocs :默认的Apache的网页目录
icons:存放网站标识
logs:存放Apache的日志
apache 服务关闭/启动命令
/usr/local/apache2/bin/apachectl start | stop | restart #Apache启动关闭相关命令
#可以做软连接,方便条用命令
ln -s /usr/local/apache2/bin/* /usr/bin/ #软连接
#直接调用命令
apachectl start | stop | restart
ab 的使用方式
[your metal~]# ab
ab: wrong number of arguments
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
-c concurrency Number of multiple requests to make at a time
-t timelimit Seconds to max. to spend on benchmarking
This implies -n 50000
-s timeout Seconds to max. wait for each response
Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-p postfile File containing data to POST. Remember also to set -T
-u putfile File containing data to PUT. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg.
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-i Use HEAD instead of GET
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
-X proxy:port Proxyserver and port number to use
-V Print version number and exit
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-q Do not show progress when doing more than 150 requests
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-r Don't exit on socket receive errors.
-h Display usage information (this message)
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
# ab常用参数的介绍:
-n :总共的请求执行数,缺省是1;
-c: 并发数,缺省是1;
-t:测试所进行的总时间,秒为单位,缺省50000s
-p:POST时的数据文件
-w: 以HTML表的格式输出结果
ab 压测示例
# http短链接
ab -r -c 10 -n 2000 -X [your ip]:80 http://www.test.top/test/index.html
# https长连接
# ab没办法添加https解析, 需要在hosts里面直接把域名解析成nginx节点
ab -r -k -c 10 -n 2000 -X [your ip]:443 http://www.test1.top/test/index.html
# 添加host
vim /etc/hosts
[your ip] [host name]
# 将并发请求数量设置为100,并使用较小的请求数量和较短的时间限制来实现高QPS:
ab -n 1000 -c 100 -H "Host: tt.tt.com" -s 5 http://10.1.1.1:8080/patient-records/pages/archive
# 其中,-n表示请求数量,这里设置为1000;-c表示并发请求数量,这里设置为100;-H表示设置请求头部信息,这里设置为Host: tt.tt.com;-s表示等待服务器响应的时间(单位为秒),这里设置为5秒;最后是请求的URL地址。
# 使用ab发送该请求,并验证返回状态码是否为200:
ab -n 100 -c 10 -H "Host: tt.tt.com" -s 5 -v 2 -r 'http://10.1.1.1:8080/patient-records/pages/archive' | grep 'HTTP/1.1 200 OK' | wc -l
# 其中,-n表示请求数量,这里设置为100;-c表示并发请求数量,这里设置为10;-H表示设置请求头部信息,这里设置为Host: tt.tt.com;-s表示等待服务器响应的时间(单位为秒),这里设置为5秒;-v 2表示输出详细的调试信息;-r表示不停止测试程序,在测试结束后继续获取数据;grep 'HTTP/1.1 200 OK'表示只输出HTTP状态码为200的请求;wc -l用于计算输出的行数,也就是符合条件的请求数量。
如果输出的行数等于请求数量,就说明所有请求的返回状态码都是200,反之则有请求返回状态码不为200。
结果分析
[your metal]# ab -n100 -c10 http://www.test.com/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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).....done
Server Software: nginx #被测试服务器软件名称
Server Hostname: www.test.com #服务器主机名
Server Port: 80 #服务器端口
Document Path: / #请求的URL中的根绝对路径
Document Length: 34180 bytes #HTTP响应的正文长度
Concurrency Level: 10 #并发数
Time taken for tests: 1.020 seconds #整个测试耗时
Complete requests: 100 #总共完成的请求数量
Failed requests: 0 #失败请求数99
(Connect: 0, Receive: 0, Length: 99, Exceptions: 0)
Total transferred: 3473840 bytes #测试过程中产生的网络传输总量
HTML transferred: 3411340 bytes #测试过程中产生的HTML传输量
Requests per second: 1.92 [#/sec] (mean) #表示服务器吞吐量,每秒事务数,括号中的 mean 表示这是一个平均值
Time per request: 2.031 [ms] (mean) #表示用户请求的平均响应时间,后面括号中的mean表示这是一个平均值
Time per request: 1.203 [ms] (mean, across all concurrent requests) #表示服务器请求平均处理时间,即实际运行时间的平均值
Transfer rate: 612.37 [Kbytes/sec] received #表示这些请求在单位时间内从服务器获取的数据长度,可以帮助排除是否存在网络流量过大导致响应时间延长的问题
Connection Times (ms) #min最小值、mean平均值、[+/-sd]方差、median中位数、maxz最大值
min mean[+/-sd] median max
Connect: 20 24 2.6 25 32 #socket链路建立消耗,代表网络状况好
Processing: 60 436 80.5 430 614 #写入缓冲区消耗+链路消耗+服务器消耗
Waiting: 55 81 24.1 75 200 #写入缓冲区消耗+链路消耗+服务器消耗+读取数据消耗
Total: 288 461 80.0 455 636 #单个事务总时间
#网络上消耗的时间的分解,表示这些请求在单位时间内从服务器获取的数据长度
Percentage of the requests served within a certain time (ms)
50% 455
66% 500
75% 534
80% 548
90% 565
95% 581
98% 609
99% 636
100% 636 (longest request)
#整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间,其中50%的用户响应时间小于455毫秒,60%的用户响应时间小于500毫秒,最大的响应时间小于636 毫秒
可能的问题
1、编译安装apr-util时报错:fatal error: expat.h: No such file or directory
解决办法:
yum install expat-devel # 安装依赖
2、make install问题
libtool: install: error: cannot install `libaprutil-1.la' to a directory not ending in/usr/local/apache2/lib
解决办法:
# 清理安装环境
make clean
# 删除源代码目录,重新安装
rm -rf httpd-2.4.49/
rm -rf /usr/local/apache2