准备测试动态页面

准备测试动态页面


在本章中,我们将了解测试动态页面所需的准备工作。服务器端动态网页是由处理服务器端脚本的应用服务器控制其构造的网页。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 请求时,与服务器建立连接,服务器发送响应,并在发送请求后关闭连接。每个请求都会继续这个循环。但是,通过keep-alive设置(也称为持久连接),客户端保持一个底层TCP连接打开,以方便多个请求和响应;这消除了否则会出现的缓慢且昂贵的连接初始化时间。

可变文件长度 -l

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

使用选项 -r

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

使用选项 -H

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

使用选项 -C

在下面的部分中,我们将详细学习如何结合使用cookie值的选项,即-C选项来使用上述选项。-C 选项通常采用名称 = 值的形式该字段可以重复。

在 Apache Bench 中使用会话 Cookie

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

安装 web2py

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

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

但是,我们需要安装解压缩包以从我们将要下载的 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,如下图所示。

来自 web2py 的会话 Cookie

在输入y以接受 cookie之前记下 cookie 值现在终端将类似于下图 – 终端上的网站!

终端网站

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

饼干价值

现在,让我们在第三个终端中使用 -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, http://www.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协议使用响应码来响应请求,200s范围内的任何事情都意味着’ok’,其余的对应一些问题。例如,400 是与资源相关的错误,例如 404 File Not Found。500s 对应于服务器错误。在我们的例子中,除了使用 -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, http://www.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, http://www.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://www.apache)的相同命令.org/,注意统计数据的差异。

执行负载测试前的检查清单

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

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

  • 为避免 TCP/IP 端口耗尽,您通常应等待 2-3 分钟,然后再进行另一项 ab 测试。

  • 确保并发连接数低于 Apache Worker Threads。

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

觉得文章有用?

点个广告表达一下你的爱意吧 !😁