您知道您的网站的平均响应时间吗?您知道您的网站可以同时处理多少用户吗?
负载测试对于 Web 应用程序了解网站的容量至关重要。选择 Web 服务器时,要做的第一件事就是运行负载测试,看看哪一个运行良好。
基准可以帮助您做出决定。
- 哪种网络服务器效果最好?
- 服务器数量 x 需要处理的请求数量
- 哪种配置可提供最佳结果
- 哪种技术栈表现更好?
- 如果您的网站速度变慢或停止工作。
有几种执行压力测试的在线工具。但是,如果您正在寻找内部解决方案,或者只是想对 Web 服务器的性能进行基准测试,则可以使用ApacheBench或使用下面列出的一些工具。
使用DigitalOcean上托管的 Apache 和 Nginx Web 服务器进行测试。

阿帕奇长凳
ApacheBench (ab) 是一个开源命令行程序,可以在任何 Web 服务器上运行。在这篇文章中,我们将向您展示如何安装这个小程序、运行负载测试并对结果进行基准测试。
阿帕奇
让我们使用 yum 命令安装 ApacheBench。
yum install httpd-tools如果你已经有httpd-tools,你可以忽略它。
现在让我们看看它在 500 个并发请求下的性能如何。
[root@lab ~]# ab -n 5000 -c 500 http://localhost:80/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software: Apache/2.2.15
Server Hostname: localhost
Server Port: 80
Document Path: /
Document Length: 4961 bytes
Concurrency Level: 500
Time taken for tests: 13.389 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Non-2xx responses: 5058
Total transferred: 26094222 bytes
HTML transferred: 25092738 bytes
Requests per second: 373.45 [#/sec] (mean)
Time per request: 1338.866 [ms] (mean)
Time per request: 2.678 [ms] (mean, across all concurrent requests)
Transfer rate: 1903.30 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 42 20.8 41 1000
Processing: 0 428 2116.5 65 13310
Waiting: 0 416 2117.7 55 13303
Total: 51 470 2121.0 102 13378
Percentage of the requests served within a certain time (ms)
50% 102
66% 117
75% 130
80% 132
90% 149
95% 255
98% 13377
99% 13378
100% 13378 (longest request)
[root@lab ~]#正如您所看到的,Apache每秒处理 373 个请求,总共花费了 13.389 秒来处理所有请求。
现在您知道默认配置可以处理许多此类请求,您可以再次运行测试并在更改配置时比较结果并选择最佳配置。
nginx
让我们运行一下针对 Apache 所做的测试,并比较哪一个性能更好。
[root@lab ~]# ab -n 5000 -c 500 http://localhost:80/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software: nginx/1.10.1
Server Hostname: localhost
Server Port: 80
Document Path: /
Document Length: 3698 bytes
Concurrency Level: 500
Time taken for tests: 0.758 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Total transferred: 19660000 bytes
HTML transferred: 18490000 bytes
Requests per second: 6593.48 [#/sec] (mean)
Time per request: 75.832 [ms] (mean)
Time per request: 0.152 [ms] (mean, across all concurrent requests)
Transfer rate: 25317.93 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 11.0 2 53
Processing: 5 19 8.2 17 53
Waiting: 0 18 8.2 16 47
Total: 10 25 17.4 18 79
Percentage of the requests served within a certain time (ms)
50% 18
66% 21
75% 21
80% 22
90% 69
95% 73
98% 75
99% 76
00% 79 (longest request)
[root@lab ~]#哦!
你看到了吗?
Nginx每秒处理 6593 个请求。这是一个赢家。
因此,只需比较两台 Web 服务器,您就会知道为您的 Web 应用程序选择哪一台。
以上测试是在CentOS 6.8,64位上完成的。您可以尝试操作系统和 Web 服务器版本的多种组合以获得最佳结果。
如果您出于任何原因不喜欢 ApacheBench,请不要担心。您还可以使用许多其他东西来执行 HTTP 加载。

围城
SIEGE是 UNIX 支持的 HTTP 负载测试实用程序。您可以在文本文件中包含多个 URL 以加载测试。您可以使用 yum 安装 siege。
# yum install siege 让我们以 500 个并发请求运行 5 秒的测试。
[root@lab ~]# siege -q -t 5S -c 500 http://localhost/
Lifting the server siege... done.
Transactions: 4323 hits
Availability: 100.00 %
Elapsed time: 4.60 secs
Data transferred: 15.25 MB
Response time: 0.04 secs
Transaction rate: 939.78 trans/sec
Throughput: 3.31 MB/sec
Concurrency: 37.97
Successful transactions: 4323
Failed transactions: 0
Longest transaction: 1.04
Shortest transaction: 0.00
[root@lab ~]#
对参数进行细分。
-q – 安静运行(不显示请求详细信息)。
-t – 运行 5 秒
-c – 500 个并发请求
如您所见,可用性为 100%,响应时间为 0.04 秒。您可以根据您的目标微调负载测试参数。

阿里
阿里是一个比较新的负载测试工具,可以进行实时分析。支持在多个平台上安装,包括Docker。
安装完成后,运行ali即可查看使用详情。
root@lab:~# ali
no target given
Usage:
ali [flags] <target URL>
Flags:
-b, --body string A request body to be sent.
-B, --body-file string The path to file whose content will be set as the http request body.
--debug Run in debug mode.
-d, --duration duration The amount of time to issue requests to the targets. Give 0s for an infinite attack. (default 10s)
-H, --header strings A request header to be sent. Can be used multiple times to send multiple headers.
-k, --keepalive Use persistent connections. (default true)
-M, --max-body int Max bytes to capture from response bodies. Give -1 for no limit. (default -1)
-m, --method string An HTTP request method for each request. (default "GET")
-r, --rate int The request rate per second to issue against the targets. Give 0 then it will send requests as fast as possible. (default 50)
-t, --timeout duration The timeout for each request. 0s means to disable timeouts. (default 30s)
-v, --version Print the current version.
Examples:
ali --duration=10m --rate=100 http://host.xz
Author:
Ryo Nakao <ryo@nakao.dev>
root@lab:~#正如您在上面看到的,您可以选择发送 HTTP 标头、测试持续时间、速率限制、超时等。我使用工具进行了快速测试,输出如下所示:
该报告是交互式的,并提供详细的延迟信息。

戈本奇
Gobench是一个用 Go 语言编写的简单负载测试实用程序,用于对 Web 服务器的性能进行基准测试。支持超过20,000个并发用户,ApacheBench不支持。

阿帕奇JMeter
JMeter是用于测量 Web 应用程序性能的最流行的开源工具之一。 JMeter 是一个基于 Java 的应用程序,不仅可以用作 Web 服务器,还可以用于 PHP 和 Java。 ASP.net、SOAP、REST 等
JMeter 有一个相当易于使用的 GUI,最新版本 3.0 需要 Java 7 或更高版本才能启动应用程序。如果您的目标是优化 Web 应用程序的性能,您应该尝试 JMeter。
工作
wrk是另一种现代性能测量工具,它会给您的 Web 服务器带来压力,并提供诸如延迟、每秒请求数、每秒传输数等详细信息。
wrk 允许您指定多个线程运行负载测试。
考虑一个在 8 个线程上运行 5 分钟、500 个并发用户的测试的示例。
wrk –t8 –c500 -d300s http://localhost自动炮
受到我工作的启发, autocannon 是用 Node.js 编写的。可以通过 API 或独立实用程序以编程方式使用。您所需要的只是安装 NodeJS 作为先决条件。
它允许您控制连接数、请求、持续时间、工作线程、超时、连接速度,并提供多种用于对 Web 应用程序进行基准测试的选项。
卷曲装载机
curl-loader用 C 语言编写,用于模拟应用程序加载并支持 SSL/TLS。在测试网页的同时,您还可以使用此开源工具加载到 FTP 服务器。
您可以创建在单个批量配置中结合 HTTP、HTTPS、FTP 和 FTPS 的测试计划。
httperf
httperf是一个专注于微观和宏观层面基准测试的高性能工具。支持HTTP/1.1和SSL协议。
如果您有预期的并发用户数,并且想要测试您的 Web 服务器是否可以处理大量请求,您可以使用以下命令:
httperf --server localhost --port 80 --num-conns 1000 --rate 100上述命令以每秒 100 个请求的速度测试 1000 个 HTTP 请求。
曾
Tsung是一个多协议分布式压力测试工具,可对 HTTP、SOAP、PostgreSQL、LDAP、XAMP 和 MySQL 服务器进行压力测试。它支持HTTP/1.0、HTTP/1.1并自动处理cookie。
可以使用 Tsung 生成报告。
结论
我们希望上面列出的基准测试工具能让您了解 Web 服务器的性能,并帮助您决定哪一个最适合您的项目。
接下来,不要忘记监控网站的性能。




![2021 年如何设置 Raspberry Pi Web 服务器 [指南]](https://i0.wp.com/pcmanabu.com/wp-content/uploads/2019/10/web-server-02-309x198.png?w=1200&resize=1200,0&ssl=1)

