准备测试动态页面

在本章中,我们将了解测试动态页面所需的准备工作.服务器端动态网页是一种网页,其构造由处理服务器端脚本的应用服务器控制. 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.

Keep-alive -k

当客户端发送HTTP请求,连接到服务器,服务器发送响应,连接在发送请求后关闭.每个请求都会继续此循环.但是,使用keep-alive设置(也称为持久连接),客户端维护一个底层TCP连接,以便于多个请求和响应;这消除了原本会出现的缓慢且昂贵的连接初始化时间.

可变文档长度-l

如果网页长度可变,那么你应该使用选项 -l .如果响应的长度不恒定,Apache Bench不会报告错误.这对于动态页面非常有用.

使用选项-r

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

使用选项-H

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

使用选项-C

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

在Apache Bench中使用Session 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服务器ver,您必须在即时终端中输入"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测试.为此,我们将不得不打开第三个终端(见下图) :

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, 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 网络服务器.我们还注意到,除了先前在输出中讨论的标题之外,我们还获得了"非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 <$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 .存在巨大差异.

使用Timelimit选项

通常,Timelimit选项很棘手.让我们从 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/admin 替换 http://127.0.0.1:8000/的相同命令(假设它是我们的web2py应用程序)或第三方网站,如https://www.apache.org/,请注意统计数据的差异.

执行负载测试前的检查表

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

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

  • 为避免TCP/IP端口耗尽,通常需要等2-3分钟才能进行另一次ab测试.

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

  • 如果是Apache,您应该在执行其他测试之前重新启动服务器或者python崩溃.