目标
通过对网站的压测,让大家了解Apache与Nginx的性能差距。
实施
1、安装服务
bash
dnf -y install httpd httpd-tools nginx
2、针对Apache进行压测
2.1、启动Apache
bash
systemctl start httpd
2.2、压测
bash
[root@localhost ~]# # 使用ab测试(10万请求,并发100)
[root@localhost ~]# ab -n 100000 -c 100 http://localhost:80/
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software: Apache/2.4.58
Server Hostname: localhost
Server Port: 80
Document Path: /
Document Length: 3929 bytes
Concurrency Level: 100
Time taken for tests: 8.632 seconds
Complete requests: 100000
Failed requests: 0
Non-2xx responses: 100000
Total transferred: 419800000 bytes
HTML transferred: 392900000 bytes
Requests per second: 11584.60 [#/sec] (mean)
Time per request: 8.632 [ms] (mean)
Time per request: 0.086 [ms] (mean, across all concurrent requests)
Transfer rate: 47492.34 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 1.5 1 35
Processing: 0 7 4.2 7 101
Waiting: 0 6 3.7 6 100
Total: 1 9 4.3 8 114
Percentage of the requests served within a certain time (ms)
50% 8
66% 9
75% 10
80% 11
90% 13
95% 15
98% 19
99% 25
100% 114 (longest request)
2.3、压测结果分析
bash
# 测试基本信息
This is ApacheBench, Version 2.3 <$Revision: 1903618 $> # ab工具版本信息
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient) # 开始对localhost进行压测
Completed 10000 requests # 每完成1万请求输出一次进度
[...省略中间进度...]
Finished 100000 requests # 总10万请求完成
# 服务器信息
Server Software: Apache/2.4.58 # 被测试的Web服务器软件及版本
Server Hostname: localhost # 测试目标主机名
Server Port: 80 # 测试目标端口
# 请求内容信息
Document Path: / # 测试的URL路径(根目录)
Document Length: 3929 bytes # 返回的HTML内容长度(约3.9KB)
# 压测参数
Concurrency Level: 100 # 并发连接数(-c参数)
Time taken for tests: 8.632 seconds # 总测试耗时(从开始到结束)
Complete requests: 100000 # 完成的请求总数(-n参数)
Failed requests: 0 # 失败的请求数(超时/连接错误)
Non-2xx responses: 100000 # 非HTTP 2xx状态码的响应数(需排查)
Total transferred: 419800000 bytes # 总传输数据量(含HTTP头)
HTML transferred: 392900000 bytes # 总HTML内容传输量(3929B × 10万)
# 性能核心指标
Requests per second: 11584.60 [#/sec] (mean) # 每秒请求数(QPS/RPS)
Time per request: 8.632 [ms] (mean) # 单个请求平均耗时(服务端视角)
Time per request: 0.086 [ms] (mean, across all concurrent requests) # 客户端视角单个请求耗时
Transfer rate: 47492.34 [Kbytes/sec] received # 数据传输速率(47.49MB/s)
# 连接时间分布(单位:毫秒)
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 1.5 1 35 # TCP连接建立时间(从SYN到ACK)
Processing: 0 7 4.2 7 101 # 服务器处理请求时间(从收到请求到返回)
Waiting: 0 6 3.7 6 100 # 请求排队等待时间(Processing的子集)
Total: 1 9 4.3 8 114 # 总耗时(Connect + Processing)
# 延迟百分位数(重要性能指标)
Percentage of the requests served within a certain time (ms)
50% 8 # 50%的请求在8ms内完成(中位数)
66% 9 # 66%的请求≤9ms
75% 10 # 75%的请求≤10ms
80% 11 # 80%的请求≤11ms
90% 13 # 90%的请求≤13ms
95% 15 # 95%的请求≤15ms
98% 19 # 98%的请求≤19ms
99% 25 # 99%的请求≤25ms(P99延迟)
100% 114 (longest request) # 最慢请求耗时114ms
3、针对Nginx进行压测
3.1、启动Nginx
bash
systemctl start httpd
3.2、压测
bash
[root@localhost ~]# # 使用ab测试(10万请求,并发100)
[root@localhost ~]# ab -n 100000 -c 100 http://localhost:80/
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software: nginx/1.24.0
Server Hostname: localhost
Server Port: 80
Document Path: /
Document Length: 3510 bytes
Concurrency Level: 100
Time taken for tests: 3.535 seconds
Complete requests: 100000
Failed requests: 0
Total transferred: 374400000 bytes
HTML transferred: 351000000 bytes
Requests per second: 28289.92 [#/sec] (mean)
Time per request: 3.535 [ms] (mean)
Time per request: 0.035 [ms] (mean, across all concurrent requests)
Transfer rate: 103435.02 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.7 1 8
Processing: 0 2 0.9 2 8
Waiting: 0 2 0.8 2 8
Total: 1 4 1.0 3 11
WARNING: The median and mean for the total time are not within a normal deviation
These results are probably not that reliable.
Percentage of the requests served within a certain time (ms)
50% 3
66% 4
75% 4
80% 4
90% 5
95% 6
98% 7
99% 7
100% 11 (longest request)
3.3、压测结果分析
bash
[root@localhost ~]# ab -n 100000 -c 100 http://localhost:80/ # 运行ApacheBench命令,参数说明:-n 100000(总请求数)-c 100(并发数)测试目标URL
This is ApacheBench, Version 2.3 <$Revision: 1903618 $> # ApacheBench版本信息
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ # 版权信息及原始开发者
Licensed to The Apache Software Foundation, http://www.apache.org/ # 授权信息及当前维护组织
Benchmarking localhost (be patient) # 开始测试目标服务器(localhost),提示需要耐心等待
Completed 10000 requests # 已完成10000个请求的进度提示
Completed 20000 requests # 已完成20000个请求的进度提示
Completed 30000 requests # 已完成30000个请求的进度提示
Completed 40000 requests # 已完成40000个请求的进度提示
Completed 50000 requests # 已完成50000个请求的进度提示
Completed 60000 requests # 已完成60000个请求的进度提示
Completed 70000 requests # 已完成70000个请求的进度提示
Completed 80000 requests # 已完成80000个请求的进度提示
Completed 90000 requests # 已完成90000个请求的进度提示
Completed 100000 requests # 所有100000个请求完成
Finished 100000 requests # 测试完全结束
Server Software: nginx/1.24.0 # 服务器使用的软件及版本(此处为Nginx 1.24.0)
Server Hostname: localhost # 服务器主机名(测试目标地址)
Server Port: 80 # 服务器监听的端口号(HTTP默认端口80)
Document Path: / # 请求的URL路径(根路径/)
Document Length: 3510 bytes # 测试页面的实际响应内容长度(单位字节)
Concurrency Level: 100 # 并发请求数设置(同时发起100个请求)
Time taken for tests: 3.535 seconds # 整个测试耗时总时间(从第一个请求开始到最后一个请求完成)
Complete requests: 100000 # 成功完成的请求数(无失败时等于总请求数)
Failed requests: 0 # 失败的请求数(0表示所有请求均成功)
Total transferred: 374400000 bytes # 所有请求接收的总字节数(包括HTTP头和内容)
HTML transferred: 351000000 bytes # 所有请求实际HTML内容的总字节数(不包括HTTP头)
Requests per second: 28289.92 [#/sec] (mean) # 每秒处理请求数(Requests per Second, RPS),平均值
Time per request: 3.535 [ms] (mean) # 每个请求的平均处理时间(单个请求从发送到接收完成的时间)
Time per request: 0.035 [ms] (mean, across all concurrent requests) # 并发场景下每个请求的平均时间(总耗时除以并发数,反映服务器吞吐能力)
Transfer rate: 103435.02 [Kbytes/sec] received # 每秒接收的数据量(单位KB/s,反映网络传输效率)
Connection Times (ms) # 连接和处理时间统计表的标题
min mean[+/-sd] median max # 列标题:最小值、平均值(±标准差)、中位数、最大值
Connect: 0 1 0.7 1 8 # 建立连接的时间统计(最小/平均/标准差/中位数/最大值)
Processing: 0 2 0.9 2 8 # 服务器处理请求的时间(不包含网络传输时间)
Waiting: 0 2 0.8 2 8 # 服务器等待处理的时间(通常等于Processing,表示后端处理延迟)
Total: 1 4 1.0 3 11 # 总耗时(Connect + Processing)
WARNING: The median and mean for the total time are not within a normal deviation # 警告提示
These results are probably not that reliable. # 结果可靠性提示(因数据波动大可能不可靠)
Percentage of the requests served within a certain time (ms) # 请求响应时间百分比分布表的标题
50% 3 # 50%的请求在3ms内完成
66% 4 # 66%的请求在4ms内完成(常用于系统可靠性评估)
75% 4 # 75%的请求在4ms内完成
80% 4 # 80%的请求在4ms内完成
90% 5 # 90%的请求在5ms内完成(关键性能指标)
95% 6 # 95%的请求在6ms内完成
98% 7 # 98%的请求在7ms内完成
99% 7 # 99%的请求在7ms内完成
100% 11 (longest request) # 最长单个请求耗时11ms