测试我们的示例应用程序
测试我们的示例应用程序
在上一章中,我们了解了 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
现在让我们创建一个小的 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 请求。由于此终端在不退出 Bottle 应用程序的情况下无法接受任何输入,因此我们需要使用另一个终端登录我们的 VPS。使用另一个终端登录 VPS 后,您可以通过在新终端中键入以下代码来导航到您的应用程序。
$ lynx http://localhost:8080/
Lynx 是一个命令行浏览器,通常默认安装在各种 Linux 发行版中,例如 Debian 和 Ubuntu。如果您看到以下输出,则表示您的应用运行良好。
输出

如果您看到以上输出,则意味着我们的应用程序已上线并准备好进行测试。
使用开发性 Web 服务器测试应用程序
请注意,ab 中存在一个错误,无法在 localhost 上测试应用程序。因此,我们将在 app.py 文件中将主机从 localhost 更改为 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 服务器测试应用程序
在之前的 ab 测试中,我们使用了 Bottle 框架中捆绑的默认 Web 服务器。
现在我们将使用多线程服务器更改单线程默认 Web 服务器。因此,让我们安装一个像cherrypy或gunicorn这样的多线程Web 服务器库,并告诉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 应用程序的生产服务器。