1. 程式人生 > >python高性能web框架——Japronto

python高性能web框架——Japronto

url hub class ews scrip pat 0ms cep dem

  近期做了一個簡單的demo需求,搭建一個http server,支持簡單的qa查詢。庫中有10000個qa對,需要支持每秒10000次以上的查詢請求。

  需求比較簡單,主要難點就是10000+的RPS。首先使用python + uwsgi寫了個簡單的demo,壓測後發現,RPS只有幾千,達不到性能要求。後來部署了多個服務,使用nginx做負載均衡才勉強達到需求。

Japronto

  後來經過google 搜索,發現了Japronto,github地址https://github.com/squeaky-pl/japronto,性能非常強悍,可以看下作者提供的性能圖:

技術分享圖片

  為什麽可以有這麽高的性能,因為Japronto 做了大量優化,其中最主要的是HTTP pipelining,Japronto 用它來做執行並發請求的優化。大多數服務器把來自客戶端的pipelining和non-pipelining請求都一視同仁,用同樣的方法處理,並沒有做針對性的優化。

技術分享圖片

  其他細節可以參考 https://medium.freecodecamp.org/million-requests-per-second-with-python-95c137af319 和 https://github.com/squeaky-pl/japronto

測試

  采用docker的方式進行部署的,按照官網的例子

  1、拉取鏡像

  docker pull japronto/japronto

  2、編寫測試代碼

# examples/1_hello/hello.py
from japronto import Application


# Views handle logic, take request as a parameter and
# returns Response object back to the client def hello(request): return request.Response(text=Hello world!) # The Application instance is a fundamental concept. # It is a parent to all the resources and all the settings # can be tweaked here. app = Application() # The Router instance lets you register your handlers and execute
# them depending on the url path and methods app.router.add_route(/, hello) # Finally start our server and handle requests until termination is # requested. Enabling debug lets you see request logs and stack traces. app.run(debug=True)

   3、啟動docker 容器

  docker run -p 8080:8080 -v $(pwd)/hello.py:/hello.py japronto/japronto --script /hello.py

  

  使用wrk進行壓測,使用 單線程,100個連接,壓測30s。結果如下

wrk -c 100 -t 1 -d 30s http://192.168.86.10:8077/

Running 30s test @ http://192.168.86.10:8077/
  1 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.88ms  548.76us  17.70ms   88.46%
    Req/Sec    53.43k     2.40k   54.86k    96.33%
  1593994 requests in 30.02s, 139.85MB read
Requests/sec:  53104.58
Transfer/sec:      4.66MB

  

 壓測結果受服務器,運行方式等影響,雖然和給出的數據相差較大,但是性能也是非常強悍的。

不過比較遺憾的是,目前這個項目已經暫停更新了

python高性能web框架——Japronto