如何进行 Web 服务器性能基准测试?

深入了解网站负载能力:性能测试工具解析

您是否了解您的网站的平均响应速度? 又是否清楚您的网站可以同时承受多少用户的访问?

对于任何Web应用来说,负载测试都至关重要,它可以帮助我们了解网站的承载能力。在选择Web服务器时,首要任务之一就是进行负载测试,以便找到最适合您需求的服务器。

基准测试可以辅助您做出以下决策:

  • 哪种Web服务器表现最佳?
  • 您的服务器需要处理多少请求?
  • 哪种配置能带来最佳性能?
  • 哪种技术栈表现更出色?
  • 当网站运行缓慢甚至崩溃时,会发生什么?

目前,市面上存在多种在线工具可以执行压力测试。然而,如果您寻求内部解决方案或希望对Web服务器性能进行基准测试,可以使用ApacheBench以及接下来将要介绍的其他工具。

我曾使用托管在Apache和Nginx上的Web服务器(来自DigitalOcean)进行测试。

ApacheBench

ApacheBench (ab) 是一个开源命令行工具,适用于各种Web服务器。 本文将介绍如何安装并使用这个小程序执行负载测试,并分析测试结果。

Apache 服务器测试

让我们使用yum命令安装ApacheBench。

yum install httpd-tools

如果您的系统中已经安装了httpd-tools,则可以跳过此步骤。

现在,我们来测试一下,看看Apache如何处理5000个并发请求,其中并发级别为500。

[[email protected] ~]# 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)
[[email protected] ~]#

测试结果显示,Apache服务器每秒能够处理373个请求,并且处理完所有请求共耗时13.389秒。

通过这个测试,您了解了默认配置下服务器可以处理的请求量。今后,在您更改任何配置后,可以再次运行测试,对比结果,从而选择最佳配置。

Nginx 服务器测试

接下来,我们使用相同的测试参数来测试Nginx,以便比较哪个服务器的性能更佳。

[[email protected] ~]# 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
100%     79 (longest request)
[[email protected] ~]#

结果令人震惊!

您看到了吗?

Nginx每秒处理了6593个请求!毫无疑问,Nginx是赢家。

通过比较这两台Web服务器的性能,您就可以明确地知道哪一个更适合您的Web应用程序。

上述测试是在CentOS 6.8, 64位系统上进行的。您可以尝试不同的操作系统和Web服务器版本组合,以获得最佳的测试结果。

如果您不喜欢使用ApacheBench,不用担心,还有很多其他工具可以执行HTTP负载测试。

Siege

Siege是一个支持UNIX的HTTP负载测试实用程序。它可以将多个URL放置在一个文本文件中进行负载测试。您可以使用yum命令安装Siege。

# yum install siege

接下来,让我们运行一个5秒的测试,并发请求数为500。

[[email protected] ~]# 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
[[email protected] ~]#

参数说明:

-q – 静默运行(不显示请求详情)

-t – 运行5秒

-c – 500个并发请求

从测试结果可以看出,可用性为100%,响应时间为0.04秒。您可以根据需要调整负载测试的参数。

Ali

Ali 是一款相对较新的负载测试工具,可以执行实时分析。它支持多种平台安装,包括Docker。

安装完成后,执行ali命令可以查看详细的使用说明。

[email protected]:~# 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 <[email protected]>
[email protected]:~#

如您所见,您可以通过Ali工具设置HTTP请求头、测试持续时间、速率限制、超时等参数。我曾使用该工具对techblik.com进行了快速测试,以下是输出结果。

测试报告是交互式的,并提供详细的延迟信息。

Gobench

Gobench 是一个用Go语言编写的简单负载测试工具,用于对Web服务器的性能进行基准测试。 它支持超过20,000个并发用户,这是ApacheBench所不具备的。

Apache JMeter

JMeter 是最流行的开源Web应用程序性能测试工具之一。JMeter是一个基于Java的应用程序,不仅可以用于测试Web服务器,还可以用于测试PHP、Java、ASP.net、SOAP、REST等。

JMeter具有友好的图形用户界面(GUI)。 最新3.0版本需要Java 7或更高版本才能启动应用程序。 如果您的目标是优化Web应用程序的性能,JMeter是您必须尝试的工具。

Wrk

Wrk 是另一个现代的性能测量工具,可以对您的Web服务器施加负载,并提供延迟、每秒请求数、每秒传输数等详细信息。

使用wrk,您可以指定使用多个线程运行负载测试。

举例来说,我们运行一个为期5分钟的测试,有500个并发用户和8个线程。

wrk –t8 –c500 -d300s http://localhost

Autocannon

受Wrk的启发,Autocannon 是用Node.js编写的。您可以通过API或独立实用程序以编程方式使用它。您只需要安装NodeJS作为前提条件。

您可以控制连接数、请求数、测试持续时间、工作线程数、超时时间、连接速率,以及其他大量的选项,以便对您的Web应用程序进行基准测试。

Curl-loader

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

Tsung 是一个多协议分布式压力测试工具,可以对HTTP、SOAP、PostgreSQL、LDAP、XAMP和MySQL服务器进行压力测试。 它支持HTTP/1.0、HTTP/1.1,并自动处理cookies。

使用Tsung可以生成测试报告。

总结

希望以上介绍的基准测试工具能够帮助您了解您的Web服务器性能,并决定哪个工具最适合您的项目。

接下来,不要忘记监控您网站的性能。