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

结论

从中等量级的压测的结果上看,对于异步架构的网络服务器,在性能上有了很大的提高。数据库

相关文章
相关标签/搜索