- Apache Bench 教程
- Apache Bench - 首頁
- 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 https://:8080/ Hit Ctrl-C to quit.
此輸出顯示我們的應用程式正在本地機器上的主機 **https://** 上執行,並在埠 **8080** 上監聽。
讓我們檢查一下我們的應用程式是否能夠正確響應 HTTP 請求。由於此終端在不停止 Bottle 應用程式服務的情況下無法接收任何輸入,我們需要使用另一個終端登入到我們的 VPS。使用另一個終端登入到 VPS 後,您可以透過在新的終端中輸入以下程式碼來導航到您的應用程式。
$ lynx https://: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 伺服器替換單執行緒預設 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 應用程式的生產伺服器。