📅  最后修改于: 2020-12-07 04:23:16             🧑  作者: Mango
在上一章中,我们了解了Apache Bench用于测试第三方网站的基本用法。在本节中,我们将使用此工具在我们自己的服务器上测试Web应用程序。为了使本教程尽可能地保持独立,我们选择安装一个Python应用程序来进行演示。您可以根据自己的专业水平选择其他任何语言,例如PHP或Ruby。
通常, Python默认安装在Linux服务器上。
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)中。如果您看到以下输出,则表明您的应用运行正常。
输出
如果您看到上面的输出,则表明我们的应用程序正在运行并且可以进行测试。
请注意,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
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结果的各种值如何变化。
在先前的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
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应用程序的生产服务器。