- Apache 工作台教程
- Apache 工作台 - 主页
- Apache Bench - 概述
- Apache Bench - 环境设置
- 测试我们的示例应用程序
- 同时测试多个 URL
- 测试动态页面的准备工作
- 动态页面的顺序测试用例
- 输出比较
- Apache Bench 有用资源
- Apache Bench - 快速指南
- Apache Bench - 有用的资源
- Apache Bench - 讨论
测试我们的示例应用程序
在上一章中,我们了解了Apache Bench测试第三方网站的基本使用方法。在本节中,我们将使用此工具在我们自己的服务器上测试 Web 应用程序。为了尽可能保持教程的独立性,我们选择安装一个 python 应用程序用于演示目的;您可以根据您的专业水平选择任何其他语言,例如 PHP 或 Ruby。
安装Python
一般Linux服务器上都会默认安装Python。
安装 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 中有一个错误,它无法在本地主机上测试应用程序。因此,我们将在 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 应用程序的生产服务器。