动态页面测试准备



本章我们将了解测试动态页面所需的准备工作。服务器端动态网页是指其构建由处理服务器端脚本的应用程序服务器控制的网页。Apache Bench 只能负载测试服务器端动态网页。

并发级别和请求总数

并发级别应低于请求总数。

$ ab -l -r -n 30 -c 80 -k -H "Accept-Encoding: gzip, deflate"  http://127.0.0.1:8000/

输出

ab: Cannot use concurrency level greater than total number of requests
Usage: ab [options] [http[s]://]hostname[:port]/path

标志的使用

在本节中,我们将描述 ab 命令中一些重要标志的使用。我们将交替使用术语“选项”和“标志”。

详细 -v

详细选项可用于分析和调试是否存在多个失败的请求。负载测试失败的一个常见迹象是测试非常快地完成,并且每秒请求值很好。但这将是一个错误的基准。为了确定成功或失败,可以使用-v 2选项,它会将每个响应的主体和标头转储到终端输出。以下命令描述了一个用例:

$ ab -n 1 -v 2 http://www.generic-example-URL.com/

输出

LOG: header received:
HTTP/1.0 200 OK
…
Content-Length: 2548687

当然,如果您正在测试可变响应或在发生任何错误时返回非 200 HTTP 代码,则应使用-l选项忽略长度检查。在后续章节中,我们将启动一个 web2py 应用程序时,我们将很快看到非 200 HTTP。

保持活动 -k

当客户端发送 HTTP 请求时,会与服务器建立连接,服务器发送响应,并在发送请求后关闭连接。此循环在每次请求时都会继续。但是,使用保持活动设置(也称为持久连接),客户端会保持底层 TCP 连接打开以促进多个请求和响应;这消除了否则存在的缓慢且代价高昂的连接初始化时间。

可变文档长度 -l

如果网页长度可变,则应使用选项-l。如果响应的长度不恒定,Apache Bench 不会报告错误。这对于动态页面很有用。

选项 -r 的使用

如何强制 ab 在收到错误时不退出?您应该使用选项-r。没有此选项,您的测试可能会在任何请求遇到套接字错误时中断。但是,使用此选项,错误将在“失败错误”标题中报告,但测试将持续到结束。

选项 -H 的使用

此选项用于添加任意标头行。参数通常采用有效标头行的形式,包含一个冒号分隔的字段-值对(即,“Accept-Encoding: zip/zop;8bit”)。

选项 -C 的使用

在下一节中,我们将详细了解如何将上述选项与使用 cookie 值的选项结合使用,即-C选项。-C 选项通常采用name = value对的形式。此字段可以重复。

使用 Apache Bench 的会话 Cookie

要了解如何在 Apache Bench 中使用 cookie,我们需要一个尝试设置 cookie 的网页。一个很好的例子是 web2py 应用程序,它是一个 Python Web 框架。

安装 web2py

我们将快速安装另一个 Python 应用程序 web2py。您可以在Web2py 框架概述中阅读有关如何使用它的更多信息。

Ubuntu 和 Debian 服务器通常默认安装 Python。因此,已经满足了一个成功运行 web2py 的要求。

但是,我们需要安装 unzip 包才能从我们将下载的 zip 文件中提取 web2py 的源文件:

$ sudo apt-get update
$ sudo apt-get install unzip

让我们从项目的网站获取 web2py 框架。我们将将其下载到我们的主文件夹:

$cd ~
$ wget http://www.web2py.com/examples/static/web2py_src.zip

现在,我们可以解压刚刚下载的文件并移入:

$ unzip web2py_src.zip
$ cd web2py

要运行 web2py,您无需安装它。进入 web2py 目录后,您可以通过键入以下命令来运行它:

$python web2py.py

如果一切顺利,您将看到以下输出,其中会要求您为管理 UI 选择密码:

web2py Web Framework
Created by Massimo Di Pierro, Copyright 2007-2017
Version 2.14.6-stable+timestamp.2016.05.10.00.21.47
Database drivers available: sqlite3, imaplib, pymysql, pg8000
WARNING:web2py:GUI not available because Tk library is not installed
choose a password:

please visit:
        http://127.0.0.1:8000/
use "kill -SIGTERM 23904" to shutdown the web2py server

但是,您需要注意的是,启动的 Web 界面只能在本地机器上访问。

从输出中,您可以了解到要停止 Web 服务器,您必须在即时终端中键入“CTRL-C”。另一方面,要在与同一 VPS 相关的另一个终端上停止 web2py 服务器,您可以插入命令 kill -SIGTERM <PID>,其中<PID>是 web2py 服务器的进程 ID,在本例中为 23904。

来自 web2py 的会话 Cookie

如果一个页面只能由已登录的用户访问,而不能直接从登录页面访问,那么您可以使用-C标志。此标志为 ab 命令定义一个 cookie。但是您必须从有效的会话中获取会话标识符 cookie 的值。如何获取?各种在线教程将指导您使用 Chrome(或 Mozilla)浏览器开发者工具。但在我们的测试用例中,由于应用程序只能在命令行上使用,我们将使用 lynx 浏览器来获取该值。

让我们首先获取会话的 cookie 值。打开另一个终端并键入以下命令:

$ lynx http://127.0.0.1:8000/

响应上述命令,lynx 将会询问您是否允许接受来自 web2py 服务器的 cookie,如下面的图像所示。

Session Cookie from web2py

在键入y接受 cookie 之前,记下 cookie 值。现在终端将类似于下面的图像——终端上的网站!

Website on the Terminal

获得 cookie 值后,我们现在将运行 ab 测试。为此,我们将必须打开第三个终端(参见下图):

Cookie Value

现在,让我们在第三个终端中使用 -C 标志:

$ ab -n 100 -c 10 -C session_name = 127.0.0.1-643dad04-3c34  http://127.0.0.1:8000/

输出

This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, https://apache.org/

Benchmarking 127.0.0.1 (be patient).....done


Server Software:        Rocket
Server Hostname:        127.0.0.1
Server Port:            8000

Document Path:          /
Document Length:        66 bytes

Concurrency Level:      10
Time taken for tests:   0.051 seconds
Complete requests:      100
Failed requests:        0
Non-2xx responses:      100
Total transferred:      27700 bytes
HTML transferred:       6600 bytes
Requests per second:    1968.12 [#/sec] (mean)
Time per request:       5.081 [ms] (mean)
Time per request:       0.508 [ms] (mean, across all concurrent requests)
Transfer rate:          532.39 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    2   0.9      2       4
Processing:     0    3   0.9      3       5
Waiting:        0    2   1.1      2       4
Total:          4    5   0.7      5       7

Percentage of the requests served within a certain time (ms)
  50%      5
  66%      5
  75%      5
  80%      6
  90%      6
  95%      6
  98%      7
  99%      7
 100%      7 (longest request)

从上面的输出中,我们注意到几点。首先,web2py 使用Rocket Web 服务器。我们还注意到,除了前面讨论的输出标题外,我们还获得了“非 2xx 响应”。一般来说,Http 协议使用响应代码响应请求,200 范围内的任何内容都表示“确定”,其余内容则对应于某些问题。例如,400 是资源相关的错误,例如 404 文件未找到。500 对应于服务器错误。在我们目前的例子中,除了使用 -C 选项时之外,没有任何错误。正如前面所述,它可以使用 -l 选项来抑制。

检查管理页面

在本节中,我们将了解如何检查管理页面。为了进行比较,让我们测试 web2py 应用程序的另一个 URL:

$ ab -n 100 -c 10 session_name = 127.0.0.1-643dad04-3c34  http://127.0.0.1:8000/admin

输出

This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, https://apache.org/

Benchmarking 127.0.0.1 (be patient).....done


Server Software:        Rocket
Server Hostname:        127.0.0.1
Server Port:            8000

Document Path:          /admin
Document Length:        8840 bytes

Concurrency Level:      10
Time taken for tests:   2.077 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      926700 bytes
HTML transferred:       884000 bytes
Requests per second:    48.14 [#/sec] (mean)
Time per request:       207.749 [ms] (mean)
Time per request:       20.775 [ms] (mean, across all concurrent requests)
Transfer rate:          435.61 [Kbytes/sec] received

Connection Times (ms)
          min  mean[+/-sd] median   max
Connect:        0    1   3.2      0      12
Processing:    62  204  52.2    199     400
Waiting:       61  203  52.0    199     400
Total:         62  205  54.3    199     411

Percentage of the requests served within a certain time (ms)
  50%    199
  66%    211
  75%    220
  80%    226
  90%    264
  95%    349
  98%    381
  99%    411
 100%    411 (longest request)
 

您应该特别注意http://127.0.0.1:8000/http://127.0.0.1:8000/admin的“连接时间”和“已服务的请求百分比…”部分中的相应统计数据。差异巨大。

使用时间限制选项

通常,时间限制选项是一个棘手的问题。让我们从ab 的手册中了解这一点,这相当具有解释性:

-t timelimit
Maximum number of seconds to spend for benchmarking. This implies a -n 50000 internally.
Use this to benchmark the server within a fixed total amount of time.
Per default there is no timelimit.

让我们使用此选项运行测试。在浏览输出后,我们将记下我们的观察结果:

$ ab -n 100 -c 10 -t 60   http://127.0.0.1:8000/

输出

This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, https://apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests


Server Software:        Rocket
Server Hostname:        127.0.0.1
Server Port:            8000

Document Path:          /
Document Length:        66 bytes

Concurrency Level:      10
Time taken for tests:   22.547 seconds
Complete requests:      50000
Failed requests:        0
Non-2xx responses:      50000
Total transferred:      13850000 bytes
HTML transferred:       3300000 bytes
Requests per second:    2217.61 [#/sec] (mean)
Time per request:       4.509 [ms] (mean)
Time per request:       0.451 [ms] (mean, across all concurrent requests)
Transfer rate:          599.88 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   0.8      2       8
Processing:     0    2   3.2      2     218
Waiting:        0    2   3.2      2     218
Total:          2    4   3.1      4     220

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      4
  80%      5
  90%      5
  95%      5
  98%      7
  99%      8
 100%    220 (longest request)

请注意,输出显示此选项会覆盖由-n选项指定的请求数,并持续到 50K 个请求。但是,由于请求处理得非常快,ab 一旦达到 50k 标记(在本例中为 22 秒内(参见标题测试所用时间))就会终止。

您可以测试相同的命令,将http://127.0.0.1:8000/替换为http://127.0.0.1:8000/admin(假设它是我们的 web2py 应用程序)或像 https://apache.org/ 这样的第三方网站,注意统计数据的差异。

执行负载测试前的清单

有一些检查将帮助您成功运行测试并准确测量性能。在执行负载测试之前,请考虑以下条件:

  • 确保没有加载额外的 Python 模块。

  • 为了避免 TCP/IP 端口耗尽,您通常应该在进行另一个 ab 测试之前等待 2-3 分钟。

  • 确保并发连接数低于 Apache 工作线程数。

  • 如果 Apache 或 Python 崩溃,您应该在执行另一个测试之前重新启动服务器。

广告