V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
1314258
V2EX  ›  Python

sanic 自家的 hello world 评测性能也不是很牛逼。

  •  
  •   1314258 · 2017-10-21 17:05:17 +08:00 · 5729 次点击
    这是一个创建于 2379 天前的主题,其中的信息可能已经有所发展或是发生改变。

    采用了的 https://github.com/channelcat/sanic/tree/master/tests/performance 里面的代码. 我就测试了 bottle 和 sanic. bottle 的按照他自己代码

    # Run with: gunicorn --workers=1 --worker-class=meinheld.gmeinheld.MeinheldWorker -b :8000 simple_server:app
    import bottle
    from bottle import route, run
    import ujson
    
    
    @route('/')
    def index():
        return ujson.dumps({'test': True})
    
    app = bottle.default_app()
    

    sanic 的代码如下

    import sys
    import os
    import inspect
    
    currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    sys.path.insert(0, currentdir + '/../../../')
    
    from sanic import Sanic
    from sanic.response import json
    
    app = Sanic("test")
    
    
    @app.route("/")
    async def test(request):
        return json({"test": True})
    
    if __name__ == '__main__':
        app.run(host="0.0.0.0", port=sys.argv[1])
    

    ab 都是 ab -n 10000 -c 10 http://192.168.2.110/
    结果如下 bottle

    HTML transferred:       130000 bytes
    Requests per second:    1677.17 [#/sec] (mean)
    Time per request:       5.962 [ms] (mean)
    Time per request:       0.596 [ms] (mean, across all concurrent requests)
    Transfer rate:          281.71 [Kbytes/sec] received
    

    sanic 如下

    HTML transferred:       130000 bytes
    Requests per second:    526.89 [#/sec] (mean)
    Time per request:       18.979 [ms] (mean)
    Time per request:       1.898 [ms] (mean, across all concurrent requests)
    Transfer rate:          53.00 [Kbytes/sec] received
    

    为什么呢?说好牛逼的 uvloop 和 httptools 呢?难道只有在数据库操作这些东西的时候,才会表现出异步的优势?

    22 条回复    2017-10-24 13:17:58 +08:00
    kslr
        1
    kslr  
       2017-10-21 17:09:00 +08:00
    谁家的业务是显示 hello world 的?
    1314258
        2
    1314258  
    OP
       2017-10-21 17:10:44 +08:00
    @kslr 没错。
    1314258
        3
    1314258  
    OP
       2017-10-21 17:13:23 +08:00
    @kslr 我记得前段时间比较过操作数据库的,sanic 加上连接池,优势才有点明显。也没见 sanic 自己吹的那样吊打。
    takanasi
        4
    takanasi  
       2017-10-21 17:13:58 +08:00
    currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    sys.path.insert(0, currentdir + '/../../../')

    这段的意义是什么?
    lrh3321
        5
    lrh3321  
       2017-10-21 21:14:58 +08:00
    没有耗时超操作,干嘛要搞成异步的,这个场景下使用 sanic 肯定不是最优的。
    zhengxiaowai
        6
    zhengxiaowai  
       2017-10-21 22:49:12 +08:00
    你可以在 bottle 中加上 sleep(1) 和 在 sanic 中加上 async.sleep(1) ,再测试一下
    mlyy
        7
    mlyy  
       2017-10-21 22:57:47 +08:00 via iPad
    这只是说明了 hello world 主要性能瓶颈在 cpu 计算,而不是 io 等待。
    boyxupers
        8
    boyxupers  
       2017-10-22 00:17:25 +08:00 via iPhone
    用 python 考虑啥性能…
    1314258
        9
    1314258  
    OP
       2017-10-22 00:38:30 +08:00
    @lrh3321
    @zhengxiaowai
    @mlyy
    这个 performance 的代码可不是我自己写出来的,是 sainc 官方自己的。 我只是奇怪为什么拿这个代码和别人对比,也展示不出自己的优势啊
    https://github.com/channelcat/sanic/tree/master/tests/performance
    boyxupers
        10
    boyxupers  
       2017-10-22 09:29:38 +08:00 via iPhone
    @zhengxiaowai gunicorn/gevent worker+bottle 试试?
    guyskk0x0
        11
    guyskk0x0  
       2017-10-22 10:08:10 +08:00 via Android
    有几个疑问:
    1. ab 的参数,如果把并发数调大,结果如何?
    2. gunicorn 用了 meinheld 这个 worker,它是 c 实现的,性能非常好,但可能有很多坑,网上相关的文章也很少。楼主有在生产环境用过吗?
    rogwan
        12
    rogwan  
       2017-10-22 12:29:52 +08:00
    web 框架的性能差别都没影响,瓶颈都在 sql 读写,用什么语言都影响不大
    keysona
        13
    keysona  
       2017-10-22 15:28:04 +08:00
    @1314258

    aiohttp

    Running 30s test @ http://127.0.0.1:8000
    30 threads and 30 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 3.95ms 1.16ms 15.69ms 64.21%
    Req/Sec 254.29 39.75 626.00 74.09%
    228077 requests in 30.04s, 33.71MB read
    Requests/sec: 7592.38
    Transfer/sec: 1.12MB

    bottle
    Running 30s test @ http://127.0.0.1:8000
    30 threads and 30 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 2.50ms 512.94us 11.59ms 94.98%
    Req/Sec 402.43 64.64 595.00 71.06%
    360870 requests in 30.03s, 60.91MB read
    Requests/sec: 12017.63
    Transfer/sec: 2.03MB

    sanic

    Running 30s test @ http://127.0.0.1:8000
    30 threads and 30 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 1.83ms 778.22us 14.05ms 81.87%
    Req/Sec 558.23 95.23 2.00k 75.12%
    500849 requests in 30.10s, 58.75MB read
    Requests/sec: 16642.26
    Transfer/sec: 1.95MB
    keysona
        14
    keysona  
       2017-10-22 15:29:13 +08:00
    @1314258

    官方的测试例子( sanic 的),默认打开 logging....

    关了它,性能就飞上来了。

    因为其它的例子貌似都没有开 log...
    keysona
        15
    keysona  
       2017-10-22 16:03:50 +08:00
    keysona
        16
    keysona  
       2017-10-22 16:05:17 +08:00
    我这里的测试,

    加了 uvloop 的 aiohttp,比没有加的快 1.5 倍...

    tornado 的测试惨不忍睹...

    sanic 全方位吊打???
    1314258
        17
    1314258  
    OP
       2017-10-22 16:40:46 +08:00
    @guyskk0x0 生成环境没用 meinheld,我只是按照 sanic 官方的代码来测试的。我待会做下数据库的,和尝试增加并发试一下。


    @keysona sanic 默认是 log_config=None 的。1.5 倍?怎么算的?
    keysona
        18
    keysona  
       2017-10-22 17:19:02 +08:00
    @1314258

    app.run(host="0.0.0.0", port=sys.argv[1], access_log=False)


    我是这样跑的,一开始我也是用 log_config,发现没有用??还是打印日志....

    你运行的时候没有跑日志?
    1314258
        19
    1314258  
    OP
       2017-10-22 20:17:32 +08:00   ❤️ 1
    @keysona app = Sanic(log_config=None)
    速度上来了。
    1314258
        20
    1314258  
    OP
       2017-10-22 20:40:49 +08:00   ❤️ 1
    @keysona 查看 sanic 的代码 633 行和 648 行,即使 log_config 在 run 设置了 False 或者 None
    最后他还是用回了 self.log_config。即是 Sanic __init__的时候 的 log_config=LOGGING

    好笨啊
    chenqh
        21
    chenqh  
       2017-10-24 13:17:03 +08:00
    @keysona tornado 多少?
    chenqh
        22
    chenqh  
       2017-10-24 13:17:58 +08:00
    没有 log 的东西和一条咸鱼有什么区别
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2657 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:05 · PVG 10:05 · LAX 19:05 · JFK 22:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.