📜  测试动态页面的准备

📅  最后修改于: 2020-12-07 04:25:01             🧑  作者: Mango


在本章中,我们将了解测试动态页面所需的准备。服务器端动态网页是一种网页,其结构由处理服务器端脚本的应用程序服务器控制。 apache工作台只能对服务器端动态网页进行负载测试。

并发级别和请求总数

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

$ 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

verbose选项可用于分析和调试是否存在多个失败的请求。负载测试失败的一个常见指示是,测试完成非常快,并且每秒提供一个很好的请求数量值。但这将是错误的基准。要确定成功或失败,可以使用-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选项通常采用名称=值对的形式。该字段可以重复。

在Apache Bench中使用会话Cookie

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

安装web2py

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

默认情况下,通常在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 ,其中是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测试。为此,我们将不得不打开第三个终端(请参见下图)-

Cookie值

现在,让我们在第三个终端中使用-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 
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范围内的任何内容都表示“可以”,其余的则对应于一些问题。例如,400s是与资源相关的错误,例如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 
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 
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请求。但是,由于处理请求的速度非常快,所以在达到50k标记后,ab就立即终止-在本例中为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崩溃,则应在执行另一项测试之前重新引导服务器。