Apache-bench-testing-our-sample-application
サンプルアプリケーションのテスト
前の章では、サードパーティのWebサイトをテストするためのApacheベンチの基本的な使用法を理解しました。 このセクションでは、このツールを使用して、独自のサーバーでWebアプリケーションをテストします。 チュートリアルを可能な限り自己完結型に保つために、デモ用にPythonアプリケーションをインストールすることを選択しました。専門レベルに応じて、PHPやRubyなどの他の言語を選択できます。
Pythonをインストールする
通常、PythonはデフォルトでLinuxサーバーにインストールされます。
Bottle Frameworkのインストールと簡単なアプリケーションの作成
Bottleは、Webアプリケーションを作成するためにpythonで書かれたマイクロフレームワークであり、pipはpythonパッケージマネージャーです。 ターミナルに次のコマンドを入力して、ボトルをインストールします-
$ 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リクエストに適切に応答しているかどうかを確認しましょう。 このターミナルは、Bottleアプリケーションのサービスを終了しないと入力を受け取れないため、別のターミナルでVPSにログインする必要があります。 別の端末でVPSにログインした後、新しい端末で次のコードを入力して、アプリケーションに移動できます。
$ lynx http://localhost:8080/
Lynxはコマンドラインブラウザーであり、通常はDebianやUbuntuなどのさまざまなLinuxディストリビューションにデフォルトでインストールされます。 次の出力が表示される場合、アプリが正常に機能していることを意味します。
出力
上記の出力が表示された場合、それはアプリケーションが稼働しており、テストの準備ができていることを意味します。
開発用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を選択しました(他のgunicornも選択できます)-
$ sudo apt-get install gunicorn
そして、ファイルを変更します。つまり、デフォルトのWebサーバーからgunicornに変更します-
...
run(server = 'gunicorn'...)
...
2番目のターミナルでアプリをテストしてみましょう。
$ 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)
1秒あたりのリクエスト数が493から3252にどのように増加したかを確認します。 これは、gunicornがPythonアプリの実稼働サーバーとして適していることを意味します。