Python的SQL性能測試

前言

測試異步與同步下Python對於PostgreSQL數據庫操做的性能html

經過測試同步以及異步下對於數據庫的增長和查找操做,以進行性能評估。更直觀的以及量化地感覺同步以及異步下的性能差距。python

環境初始化

代碼地址git

須要安裝pipenv,詳細內容可參考github

pip3.6 install pipenv
git clone https://github.com/GuangTianLi/python-sql-performance.git
cd python-sql-performance
pipenv sync
pipenv shell

SQL操做性能評估

python postgresql_speed_test.py

  DBAPI:  psycopg2
         11004 function calls in 2.235 seconds
  DBAPI:  asyncpg
         471973 function calls in 2.436 seconds
  DBAPI:  uvloop
         206945 function calls in 0.794 seconds
  DBAPI:  psycopg2, total seconds 2.558364
  DBAPI:  asyncpg, total seconds 2.309759
  DBAPI:  uvloop, total seconds 2.032303

結論

從結果上看,對於數據庫操做自己,異步對於其性能自己只能算是錦上添花。而異步操做自己則也須要添加對事件循環的處理,等因而變相的增長了運行時間,而若是每一個數據庫操做自己所須要的時間小於事件循環處理的時間,則其總時間就是增長的。sql

因此異步架構在用於單純的數據庫操做時,並不能取得很是良好的性能優化,數據庫操做自己的優化仍是依賴於操做自己以及數據庫結構的優化。shell

WebServer性能評估

flask

python flask_server_speed_test.py
wrk -d 60 -c 100 -t 12 --timeout 8 http://127.0.0.1:8080/db
Running 1m test @ http://127.0.0.1:8080/db
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   331.47ms  221.85ms   2.01s    89.71%
    Req/Sec    30.95     17.90    80.00     63.85%
  18597 requests in 1.00m, 3.10MB read
Requests/sec:    309.41
Transfer/sec:     52.88KB

sanic

python sanic_server_speed_test.py
wrk -d 60 -c 100 -t 12 --timeout 8 http://127.0.0.1:8080/db
Running 1m test @ http://127.0.0.1:8080/db
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   162.95ms   99.56ms   1.89s    87.88%
    Req/Sec    52.26     23.73   148.00     61.57%
  36702 requests in 1.00m, 4.83MB read
Requests/sec:    610.64
Transfer/sec:     82.29KB

結論

從中等量級的壓測的結果上看,對於異步架構的網絡服務器,在性能上有了很大的提高。數據庫

相關文章
相關標籤/搜索