Apache Bench-测试应用程序示例

在上一章中,我们了解了Apache Bench用于测试第三方网站的基本用法.在本节中,我们将使用此工具在我们自己的服务器上测试Web应用程序.为了尽可能保持教程自包含,我们选择安装python应用程序用于演示目的;您可以根据自己的专业水平选择任何其他语言,如PHP或Ruby.

安装Python

通常,Python默认安装在Linux服务器上.

安装Bottle框架并创建一个简单的应用程序

Bottle是一个用python编写的用于创建Web应用程序的微框架,而pip是一个python包经理.在终端中键入以下命令以安装Bottle :

 
 $ sudo apt-get install python-pip 
 $ sudo pip install bottle

现在让我们创建一个小瓶子应用程序.为此,创建一个目录并在其中移动 : 去;

 
 $ mkdir webapp 
 $ cd webapp

我们将在webapp目录中创建一个新的python脚本 app.py :

 
 $ vim app.py

现在,在app.py文件中写下以下代码 :

from bottle import Bottle, run

app = Bottle()

@app.route('/')
@app.route('/hello')
def hello():
   return "Hello World!"

run(app, host = 'localhost', port = 8080)

添加上述行后,保存并保存关闭文件.保存文件后,我们可以运行python脚本来启动应用程序 :

 
 $ python app.py

输出

Bottle v0.12.7 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

此输出显示我们的应用程序在主机 http://localhost 的本地计算机上运行并侦听端口 8080 .

让我们检查一下我们的应用是否正确响应了HTTP请求.由于此终端在不退出服务瓶申请的情况下无法接受任何输入,我们需要使用其他终端登录我们的VPS.使用其他终端登录VPS后,您可以通过在新终端中键入以下代码导航到您的应用程序.

 
 $ lynx http://localhost:8080/

Lynx是一个命令行浏览器,默认安装在Debian和Ubuntu等各种Linux发行版中.如果您看到以下输出,则表示您的应用正常运行.

输出

Lynx

如果你看到上面的输出,那就意味着我们的应用程序已经上线并准备好进行测试了.

使用开发Web服务器测试应用程序

请注意,ab中存在错误,无法在localhost上测试应用程序.因此,我们将把主机从localhost更改为app.py文件中的127.0.0.1.因此文件将更改为以下 :

from bottle import Bottle, run

app = Bottle()

@app.route('/')
@app.route('/hello')
def hello():
   return "Hello World!"

run(app, host = '127.0.0.1', port = 8080)

现在让我们通过输入来测试我们的应用运行lynx命令的同一终端上的以下命令 :

 
 $ ab -n 100 -c 10 http://127.0. 0.1:8080/hello

输出

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:        WSGIServer/0.1
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /hello
Document Length:        12 bytes

Concurrency Level:      10
Time taken for tests:   0.203 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      16500 bytes
HTML transferred:       1200 bytes
Requests per second:    493.78 [#/sec] (mean)
Time per request:       20.252 [ms] (mean)
Time per request:       2.025 [ms] (mean, across all concurrent requests)
Transfer rate:          79.56 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     1    6  28.2      2     202
Waiting:        1    6  28.2      2     202
Total:          1    6  28.2      2     202

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

第一个终端的输出为(100)时间)如下 :

 
 ... 
 127.0.0.1  -   -  [10/Jun/2017 04:30:26 ]"GET/hello HTTP/1.0"200 12 
 127.0.0.1  -   -  [10/Jun/2017 04:30:26]"GET/hello HTTP/1.0"200 12 
 127.0.0.1  -  -  [10/Jun/2017 04:30:26]"GET/hello HTTP/1.0"200 12 
 ...

你可以观察与初始测试相比,ab结果的各种值如何变化.

使用多线程Web服务器测试应用程序

在previ对ab的测试,我们使用了Bottle框架中捆绑的默认Web服务器.

现在我们将使用多线程更改单线程默认Web服务器.因此,让我们安装一个多线程Web服务器库,如 cherrypy gunicorn ,并告诉Bottle使用它.我们选择了gunicorn用于演示目的(你也可以选择其他一个) :

 
 $ sudo apt-get install gunicorn

并修改文件,即从默认Web服务器更改为gunicorn :

 
 ... 
 run(server ='gunicorn'...)
 ...

让我们测试一下第二个终端中的应用程序.

 
 $ ab -n 100 -c 10 http://127.0.0.1:8080/hello

输出

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:        gunicorn/19.0.0
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /hello
Document Length:        12 bytes

Concurrency Level:      10
Time taken for tests:   0.031 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      17200 bytes
HTML transferred:       1200 bytes
Requests per second:    3252.77 [#/sec] (mean)
Time per request:       3.074 [ms] (mean)
Time per request:       0.307 [ms] (mean, across all concurrent requests)
Transfer rate:          546.36 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.9      0       4
Processing:     1    2   0.7      3       4
Waiting:        0    2   0.8      2       3
Total:          2    3   0.6      3       5
WARNING: The median and mean for the initial connection time are not within a normal
        deviation These results are probably not that reliable.
WARNING: The median and mean for the processing time are not within a normal deviation
        These results are probably not that reliable.

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

观察每秒请求数从493增加到3252.这意味着gunicorn适合作为python应用程序的生产服务器.