Testare la nostra applicazione di esempio

Nel capitolo precedente, abbiamo compreso l'uso di base di Apache Bench per testare un sito Web di terze parti. In questa sezione, utilizzeremo questo strumento per testare un'applicazione web sul nostro server. Per mantenere il tutorial autonomo per quanto possibile, abbiamo scelto di installare un'applicazione python a scopo dimostrativo; puoi scegliere qualsiasi altra lingua come PHP o Ruby a seconda del tuo livello di esperienza.

Installazione di Python

Generalmente, Python è installato per impostazione predefinita sui server Linux.

Installazione di Bottle Framework e creazione di un'applicazione semplice

Bottle è un micro-framework scritto in python per la creazione di applicazioni web e pip è un gestore di pacchetti python. Digita il seguente comando nel terminale per installare Bottle -

$ sudo apt-get install python-pip
$ sudo pip install bottle

Creiamo ora una piccola applicazione Bottle. Per questo, crea una directory e spostati al suo interno -

$ mkdir webapp
$ cd webapp

Creeremo un nuovo script python, app.py, all'interno della directory webapp -

$ vim app.py

Ora scrivi il seguente codice nel file 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)

Dopo aver aggiunto le righe precedenti, salva e chiudi il file. Dopo aver salvato il file, possiamo eseguire lo script python per avviare l'applicazione -

$ python app.py

Output

Bottle v0.12.7 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

Questo output mostra che la nostra applicazione è in esecuzione sulla macchina locale dell'host http://localhost e ascolto sul porto 8080.

Controlliamo se la nostra app risponde correttamente alle richieste HTTP. Poiché questo terminale non può ricevere alcun input senza chiudere il servizio dell'applicazione Bottle, è necessario accedere al nostro VPS con un altro terminale. Dopo aver effettuato l'accesso al VPS con un altro terminale, è possibile accedere alla propria applicazione digitando il seguente codice nel nuovo terminale.

$ lynx http://localhost:8080/

Lynx è un browser a riga di comando e di solito è installato di default in varie distribuzioni Linux come Debian e Ubuntu. Se vedi il seguente output, significa che la tua app funziona correttamente.

Output

Se vedi l'output sopra, significa che la nostra applicazione è attiva e pronta per il test.

Test dell'applicazione con Developmental Web Server

Notare che c'è un bug in ab e non è possibile testare l'applicazione sul localhost. Quindi cambieremo l'host da localhost a 127.0.0.1 nel file app.py. Quindi il file cambierà nel seguente:

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)

Proviamo ora la nostra app digitando il seguente comando sullo stesso terminale su cui è stato eseguito il comando lynx -

$ ab -n 100 -c 10  http://127.0.0.1:8080/hello

Output

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)

Mentre l'uscita sul primo terminale sarà (100 volte) come segue:

...
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   
...

È possibile osservare come sono cambiati i vari valori del risultato ab rispetto al test iniziale.

Test dell'applicazione con un server Web multi-thread

Nei precedenti test di ab, abbiamo utilizzato il server web predefinito in bundle nel framework Bottle.

Ora cambieremo il server web predefinito a thread singolo con uno multi-thread. Pertanto, installiamo una libreria di server web multi-thread comecherrypy o gunicorne dì a Bottle di usarlo. Abbiamo scelto gunicorn per lo scopo dimostrativo qui (puoi sceglierne anche un altro) -

$  sudo apt-get install gunicorn

E modifica il file, ovvero cambia dal server web predefinito a gunicorn -

...
run(server = 'gunicorn'...)
...

Proviamo l'app nel secondo terminale.

$ ab -n 100 -c 10  http://127.0.0.1:8080/hello

Output

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)

Osserva come le richieste al secondo sono aumentate da 493 a 3252. Significa che gunicorn è adatto come server di produzione per app python.