pyserve
1.0.0
Python中實現的單個文件CGI服務器。
創建一個名為hello.py的新文件,其中包含以下內容:
#!/usr/bin/env python3
from pyserve import http
print ( f"Hi { http . GET [ 'name' ] } !" )即使您不使用它,也必須導入Pyserve。否則,您需要明確編寫腳本正確的HTTP標頭
使文件可執行:
chmod +x hello.py啟動服務器:
python3 pyserve.py打開http://localhost:8000/hello.py?name=Meir 。
您應該看到以下輸出:
Hi! ['Meir']
由於為每個請求創建了一個新過程,因此性能不是很好。
#!/usr/bin/env python3
import pyserve
print ( "Hello World!" ) ab -n 500 -c 50 http://127.0.0.1:8000/hello_world.py
This is ApacheBench, Version 2.3 <$Revision: 1901567 $>
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)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests
Server Software:
Server Hostname: 127.0.0.1
Server Port: 8000
Document Path: /hello_world.py
Document Length: 13 bytes
Concurrency Level: 50
Time taken for tests: 6.108 seconds
Complete requests: 500
Failed requests: 0
Total transferred: 38500 bytes
HTML transferred: 6500 bytes
Requests per second: 81.86 [#/sec] (mean)
Time per request: 610.768 [ms] (mean)
Time per request: 12.215 [ms] (mean, across all concurrent requests)
Transfer rate: 6.16 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.7 0 5
Processing: 88 591 150.9 569 993
Waiting: 87 570 146.2 548 990
Total: 89 591 151.1 569 993
Percentage of the requests served within a certain time (ms)
50% 569
66% 648
75% 700
80% 724
90% 777
95% 848
98% 959
99% 962
100% 993 (longest request)
每秒只有80個請求。讓我們將其與燒瓶進行比較:
from flask import Flask
app = Flask ( __name__ )
@ app . route ( "/" )
def ping ():
return "Hello World!" ab -n 500 -c 50 http://127.0.0.1:8000/
This is ApacheBench, Version 2.3 <$Revision: 1901567 $>
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)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests
Server Software: gunicorn
Server Hostname: 127.0.0.1
Server Port: 8000
Document Path: /
Document Length: 12 bytes
Concurrency Level: 50
Time taken for tests: 0.158 seconds
Complete requests: 500
Failed requests: 0
Total transferred: 82500 bytes
HTML transferred: 6000 bytes
Requests per second: 3157.24 [#/sec] (mean)
Time per request: 15.837 [ms] (mean)
Time per request: 0.317 [ms] (mean, across all concurrent requests)
Transfer rate: 508.74 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 2
Processing: 2 15 4.0 13 28
Waiting: 1 15 4.0 13 28
Total: 4 15 4.1 13 29
Percentage of the requests served within a certain time (ms)
50% 13
66% 14
75% 16
80% 18
90% 22
95% 25
98% 27
99% 28
100% 29 (longest request)
注意:我們使用gunicorn app:app運行服務器。
〜3150每秒請求(快速x40倍)。這是一個巨大的差異,如果我們增加工人人數,我們可以取得更好的結果。