Python flask 與 GO WEB服務器性能對比

測試環境:python

  • 系統: CentOS 7.1
  • Mem: 8G
  • CPU: 虛擬機16核
  • Python版本: python3.6
  • Flask版本: 0.12.2
  • Golang版本: 1.6.3
1.首先寫一個Flask的web程序,只返回一個 Hello word!
from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello word!'

if __name__ == '__main__':
    app.run()
2.寫一個go語言的web程序,也返回一個 Hello word!
package main

import (
    f "fmt"
    "log"
    "net/http"
)

func sayhelloName(w http.ResponseWriter, r *http.Request) {
    f.Fprintln(w, "hello world!")
}
func main() {
    http.HandleFunc("/", sayhelloName)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe:", err)
    }
}
3.直接在控制檯啓動Flask, 用壓力測試工具模擬併發請求。這裏模擬100個併發100000個請求。
ab -n 100000 -c 100 "http://172.30.200.88:5000/"
Server Software:        Werkzeug/0.12.2
Server Hostname:        172.30.200.88
Server Port:            5000

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      100
Time taken for tests:   88.441 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      16500000 bytes
HTML transferred:       1100000 bytes
Requests per second:    1130.70 [#/sec] (mean)
Time per request:       88.441 [ms] (mean)
Time per request:       0.884 [ms] (mean, across all concurrent requests)
Transfer rate:          182.19 [Kbytes/sec] received
4.用Gunicorn以8個進程啓動flask,再以一樣的併發測試一遍
Server Software:        gunicorn/19.7.1
Server Hostname:        172.30.200.88
Server Port:            8080

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      100
Time taken for tests:   15.842 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      17100000 bytes
HTML transferred:       1100000 bytes
Requests per second:    6312.50 [#/sec] (mean)
Time per request:       15.842 [ms] (mean)
Time per request:       0.158 [ms] (mean, across all concurrent requests)
Transfer rate:          1054.14 [Kbytes/sec] received
5.以一樣的併發測試Go
Server Software:        
Server Hostname:        172.30.200.88
Server Port:            8080

Document Path:          /
Document Length:        13 bytes

Concurrency Level:      100
Time taken for tests:   12.460 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      13000000 bytes
HTML transferred:       1300000 bytes
Requests per second:    8025.80 [#/sec] (mean)
Time per request:       12.460 [ms] (mean)
Time per request:       0.125 [ms] (mean, across all concurrent requests)
Transfer rate:          1018.90 [Kbytes/sec] received

測試結果:
Flask 總耗時 88.441秒,平均每秒處理1130個請求
Gunicorn多進程時耗時 15.842, 平均每秒處理 6312個請求
Go 總耗時 12.46秒,每秒處理 8025個請求golang

相比之下 go跟gunicorn的處理速度有提高,可是並無像傳說中十倍的性能。在測試中還發現,在用flask的時候並不能支持大併發,當併發在200以上就很容易報錯了, 而Golang的併發到1000都能正常運行,這一點上差距比較大。

相關文章
相關標籤/搜索