Rust 给 Python Web 带来的性能提升

245 天前
 Baloneo

对比 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 的下一波春天?

2973 次点击
所在节点    Python
20 条回复
wuwukai007
245 天前
官方文档就是个简单的 demo ,很怀疑持久性,对比之前的 litestar 文档就很齐全
rrfeng
245 天前
不是,我看着数不是被 gin 秒杀了?
Baloneo
245 天前
@rrfeng 是的
xgdgsc
245 天前
TArysiyehua
245 天前
这么说吧,我用 python 的那一刻就没考虑性能的问题
so1n
245 天前
没加数据库调用的测试都是耍流氓...而这个例子的接口连 IO 处理都没...
vicalloy
245 天前
如果是 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
245 天前
@vicalloy 已经是 orjson 了
Oxonomy
245 天前
序列化带来的提升能说明什么呢🤔
fakeshadow
245 天前
哥们儿你那个附言我笑了,压测 404
Baloneo
245 天前
@fakeshadow 确实是错了 估计是 Robyn 框架并发的 bug
CodeCodeStudy
245 天前
类似 Swoole 用 C++做扩展来给 PHP 做性能提升吗?
Baloneo
245 天前
Baloneo
245 天前
@Oxonomy 或许可以将更多的模块用 rust python 绑定重写 用 python 调用
fakeshadow
245 天前
@Baloneo 如果你浏览 tfb 可以参考下 robyn 的分数,横向比较也并没有很快的感觉(我对 py 库了解不多)。https://www.techempower.com/benchmarks/#section=test&runid=074e8a70-d6fb-4f10-82f3-43e57c0965b5&test=plaintext&l=hra0hr-35r
Baloneo
245 天前
@fakeshadow 比 fastapi 还是快很多
huihuiHK
245 天前
@app.get("/json")。 Running 20s test @ http://127.0.0.1:8111/query
lanlanye
244 天前
一般来说,瓶颈都不会在这里……
SenLief
244 天前
我都有 python 了,那性能就不是我考虑的东西。
shinession
244 天前
json 序列化不是用 msgspec 吗? 试了比 orjson 快很多

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/971674

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX