V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
Baloneo
V2EX  ›  Python

Rust 给 Python Web 带来的性能提升

  •  
  •   Baloneo · Sep 7, 2023 · 4468 views
    This topic created in 975 days ago, the information mentioned may be changed or developed.

    对比 FastAPI Robyn Gin json 序列化返回性能 大概的性能对比如下

    wrk -t10 -c 10 -d 20s http://127.0.0.1:xxxx/json
    

    FastAPI (async)

    Running 20s test @ http://127.0.0.1:8080/json
      10 threads and 10 connections
      Thread Stats   Avg      Stdev     Max   +/- Stdev
        Latency   835.68us  798.76us  29.00ms   94.21%
        Req/Sec     1.32k   513.33     2.51k    49.45%
      262867 requests in 20.10s, 38.10MB read
    Requests/sec:  13078.90
    Transfer/sec:      1.90MB
    
    

    Robyn (sync)

    Running 20s test @ http://127.0.0.1:8111/json
      10 threads and 10 connections
      Thread Stats   Avg      Stdev     Max   +/- Stdev
        Latency   358.51us  690.23us  24.51ms   96.09%
        Req/Sec     3.91k     1.70k    9.80k    76.79%
      781679 requests in 20.10s, 75.29MB read
    Requests/sec:  38889.89
    Transfer/sec:      3.75MB
    

    Robyn (async)

    Running 20s test @ http://127.0.0.1:8111/json
      10 threads and 10 connections
      Thread Stats   Avg      Stdev     Max   +/- Stdev
        Latency   517.35us  795.62us  32.63ms   95.72%
        Req/Sec     2.42k     0.87k    6.79k    71.78%
      481576 requests in 20.10s, 46.39MB read
    Requests/sec:  23959.48
    Transfer/sec:      2.31MB
    

    Gin

    Running 20s test @ http://127.0.0.1:8888/json
      10 threads and 10 connections
      Thread Stats   Avg      Stdev     Max   +/- Stdev
        Latency   293.92us  688.32us  27.60ms   94.13%
        Req/Sec     6.87k     2.66k   13.32k    58.32%
      1368505 requests in 20.10s, 195.77MB read
    Requests/sec:  68090.01
    Transfer/sec:      9.74MB
    

    robyn 基准参考 https://github.com/sansyrox/robyn-comparison-benchmarks

    Rust+Python Python 的下一波春天?

    Supplement 1  ·  Sep 7, 2023
    重新修改了返回 json 的方式
    ```
    from robyn import Robyn, jsonify

    app = Robyn(__file__)

    @app.get("/json")
    async def h(request):
    return jsonify({
    "message": "Hello, world!"
    })

    app.start(port=8111)

    ```
    测得
    ```
    Running 20s test @ http://127.0.0.1:8111/query
    10 threads and 10 connections
    Thread Stats Avg Stdev Max +/- Stdev
    Latency 562.74us 1.79ms 53.50ms 93.07%
    Req/Sec 7.45k 3.76k 16.32k 59.89%
    1484168 requests in 20.10s, 159.94MB read
    Non-2xx or 3xx responses: 1484168
    Requests/sec: 73841.00
    Transfer/sec: 7.96MB

    ```
    20 replies    2023-09-08 07:56:28 +08:00
    wuwukai007
        1
    wuwukai007  
       Sep 7, 2023
    官方文档就是个简单的 demo ,很怀疑持久性,对比之前的 litestar 文档就很齐全
    rrfeng
        2
    rrfeng  
       Sep 7, 2023 via Android
    不是,我看着数不是被 gin 秒杀了?
    Baloneo
        3
    Baloneo  
    OP
       Sep 7, 2023
    @rrfeng 是的
    xgdgsc
        4
    xgdgsc  
       Sep 7, 2023 via Android
    TArysiyehua
        5
    TArysiyehua  
       Sep 7, 2023
    这么说吧,我用 python 的那一刻就没考虑性能的问题
    so1n
        6
    so1n  
       Sep 7, 2023
    没加数据库调用的测试都是耍流氓...而这个例子的接口连 IO 处理都没...
    vicalloy
        7
    vicalloy  
       Sep 7, 2023
    如果是 Json 序列化性能,可以做 FastAPI 里使用 orjson 进行加速。
    https://fastapi.tiangolo.com/advanced/custom-response/#orjsonresponse
    如果不想改动现有代码,可以简单的做个 monkey patch 。

    import orjson

    json.dumps = lambda obj, *args, **kwargs: orjson.dumps(
    obj, option=orjson.OPT_NON_STR_KEYS
    ).decode("utf-8")
    json.loads = lambda obj, *args, **kwargs: orjson.loads(obj)
    Baloneo
        8
    Baloneo  
    OP
       Sep 7, 2023
    @vicalloy 已经是 orjson 了
    Oxonomy
        9
    Oxonomy  
       Sep 7, 2023 via iPhone
    序列化带来的提升能说明什么呢🤔
    fakeshadow
        10
    fakeshadow  
       Sep 7, 2023
    哥们儿你那个附言我笑了,压测 404
    Baloneo
        11
    Baloneo  
    OP
       Sep 7, 2023
    @fakeshadow 确实是错了 估计是 Robyn 框架并发的 bug
    CodeCodeStudy
        12
    CodeCodeStudy  
       Sep 7, 2023
    类似 Swoole 用 C++做扩展来给 PHP 做性能提升吗?
    Baloneo
        14
    Baloneo  
    OP
       Sep 7, 2023
    @Oxonomy 或许可以将更多的模块用 rust python 绑定重写 用 python 调用
    fakeshadow
        15
    fakeshadow  
       Sep 7, 2023
    @Baloneo 如果你浏览 tfb 可以参考下 robyn 的分数,横向比较也并没有很快的感觉(我对 py 库了解不多)。https://www.techempower.com/benchmarks/#section=test&runid=074e8a70-d6fb-4f10-82f3-43e57c0965b5&test=plaintext&l=hra0hr-35r
    Baloneo
        16
    Baloneo  
    OP
       Sep 7, 2023
    @fakeshadow 比 fastapi 还是快很多
    huihuiHK
        17
    huihuiHK  
       Sep 7, 2023
    @app.get("/json")。 Running 20s test @ http://127.0.0.1:8111/query
    lanlanye
        18
    lanlanye  
       Sep 7, 2023
    一般来说,瓶颈都不会在这里……
    SenLief
        19
    SenLief  
       Sep 7, 2023
    我都有 python 了,那性能就不是我考虑的东西。
    shinession
        20
    shinession  
       Sep 8, 2023
    json 序列化不是用 msgspec 吗? 试了比 orjson 快很多
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2671 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 807ms · UTC 15:50 · PVG 23:50 · LAX 08:50 · JFK 11:50
    ♥ Do have faith in what you're doing.