ทดสอบแอปพลิเคชันตัวอย่างของเรา
ในบทที่แล้วเราได้ทำความเข้าใจเกี่ยวกับการใช้งานพื้นฐานของ Apache Bench เพื่อทดสอบเว็บไซต์ของบุคคลที่สาม ในส่วนนี้เราจะใช้เครื่องมือนี้เพื่อทดสอบเว็บแอปพลิเคชันบนเซิร์ฟเวอร์ของเราเอง เพื่อให้บทช่วยสอนมีอยู่ในตัวเราได้เลือกที่จะติดตั้งแอปพลิเคชัน python เพื่อจุดประสงค์ในการสาธิต คุณสามารถเลือกภาษาอื่น ๆ เช่น PHP หรือ Ruby ขึ้นอยู่กับระดับความเชี่ยวชาญของคุณ
การติดตั้ง Python
โดยทั่วไป Python ถูกติดตั้งโดยค่าเริ่มต้นบนเซิร์ฟเวอร์ Linux
การติดตั้ง Bottle Framework และการสร้างแอปพลิเคชันอย่างง่าย
Bottle เป็นไมโครเฟรมเวิร์กที่เขียนด้วย python สำหรับสร้างเว็บแอปพลิเคชันและ pip เป็นตัวจัดการแพ็คเกจหลาม พิมพ์คำสั่งต่อไปนี้ในเทอร์มินัลเพื่อติดตั้ง Bottle -
$ sudo apt-get install python-pip
$ sudo pip install bottleให้เราสร้างแอปพลิเคชั่นขวดเล็ก ๆ สร้างไดเร็กทอรีและย้ายเข้าไปข้างใน -
$ mkdir webapp
$ cd webappเราจะสร้างสคริปต์ python ใหม่ app.pyภายในไดเรกทอรี webapp -
$ 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.pyOutput
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 หากคุณเห็นผลลัพธ์ต่อไปนี้แสดงว่าแอปของคุณทำงานได้ดี
Output
 
                หากคุณเห็นผลลัพธ์ข้างต้นแสดงว่าแอปพลิเคชันของเราพร้อมใช้งานและพร้อมสำหรับการทดสอบ
การทดสอบแอปพลิเคชันกับเว็บเซิร์ฟเวอร์พัฒนาการ
โปรดทราบว่ามีข้อผิดพลาดใน ab และไม่สามารถทดสอบแอปพลิเคชันบน localhost ได้ ดังนั้นเราจะเปลี่ยนโฮสต์จาก localhost เป็น 127.0.0.1 ในไฟล์ app.py ดังนั้นไฟล์จะเปลี่ยนเป็นดังต่อไปนี้ -
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/helloOutput
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 มีการเปลี่ยนแปลงอย่างไรเมื่อเทียบกับการทดสอบเบื้องต้น
การทดสอบแอปพลิเคชันกับเว็บเซิร์ฟเวอร์แบบมัลติเธรด
ในการทดสอบ ab ก่อนหน้านี้เราได้ใช้เว็บเซิร์ฟเวอร์เริ่มต้นที่รวมอยู่ในกรอบงาน Bottle
ตอนนี้เราจะเปลี่ยนเว็บเซิร์ฟเวอร์เริ่มต้นแบบเธรดเดียวด้วยมัลติเธรด ดังนั้นให้เราติดตั้งไลบรารีเว็บเซิร์ฟเวอร์แบบมัลติเธรดเช่นcherrypy หรือ gunicornและบอกให้ Bottle ใช้ เราได้เลือก gunicorn เพื่อการสาธิตที่นี่ (คุณสามารถเลือกอันอื่นได้เช่นกัน) -
$  sudo apt-get install gunicornและแก้ไขไฟล์นั่นคือการเปลี่ยนจากเว็บเซิร์ฟเวอร์เริ่มต้นเป็น gunicorn -
...
run(server = 'gunicorn'...)
...ให้เราทดสอบแอปในเทอร์มินัลที่สอง
$ ab -n 100 -c 10  http://127.0.0.1:8080/helloOutput
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