FastAPI同步与异步性能测试

发布于 2025-02-14 00:08:02 字数 2052 浏览 0 评论 0原文

我编写了简单的FastApi服务器,我想在其中进行性能测试。这意味着我想测试异步与同步端点,但是我总是得到相同的结果。我不明白的概念或我做错了什么?

from fastapi import FastAPI
import time
import asyncio
app = FastAPI()


@app.get("/hello")
def say_hello():
    time.sleep(5)


@app.get("/hello-async")
async def say_hello():
    await asyncio.sleep(5)

我像这样运行服务器: Gunicorn Main:App - Workers = 4-劳动级uvicorn.workers.uvicornworker - bind 0.0.0.0.0:8000

并进行性能测试:

ab -n 80 -c 8 http://localhost:8000/hello-async
ab -n 80 -c 8 http://localhost:8000/hello

得到了这种结果:

This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        uvicorn
Server Hostname:        localhost
Server Port:            8000

Document Path:          /hello-async
Document Length:        4 bytes

Concurrency Level:      8
Time taken for tests:   55.016 seconds
Complete requests:      80
Failed requests:        0
Total transferred:      10240 bytes
HTML transferred:       320 bytes
Requests per second:    1.45 [#/sec] (mean)
Time per request:       5501.634 [ms] (mean)
Time per request:       687.704 [ms] (mean, across all concurrent requests)
Transfer rate:          0.18 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:  5000 5001   0.6   5001    5003
Waiting:     5000 5001   0.6   5001    5003
Total:       5000 5001   0.6   5001    5003

Percentage of the requests served within a certain time (ms)
  50%   5001
  66%   5002
  75%   5002
  80%   5002
  90%   5002
  95%   5002
  98%   5003
  99%   5003
 100%   5003 (longest request)

我想模拟I/O操作,如果我调用API或数据库,我想知道异步方法的速度更快。现在发生了什么,我得到了同一时间进行测试。

如果我要进行CPU密集操作,我会明白,我会得到相同的结果。我做错了什么?

同样,在同步方法中,Web服务器仅限于产生的数字线程,但是Async方法的理论限制是什么?另外,我如何在同步方法和异步方法中测试限制线程的数量,事件循环的极限是什么?

I wrote simple fastapi server in which I want to do performance testing. Which means that I would like to test async vs. sync endpoint, but I got always the same results. Which concept I don't understand or what I am doing wrong?

from fastapi import FastAPI
import time
import asyncio
app = FastAPI()


@app.get("/hello")
def say_hello():
    time.sleep(5)


@app.get("/hello-async")
async def say_hello():
    await asyncio.sleep(5)

I run server like this:
gunicorn main:app --workers=4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000

And do performance testing:

ab -n 80 -c 8 http://localhost:8000/hello-async
ab -n 80 -c 8 http://localhost:8000/hello

And got this kind of results:

This is ApacheBench, Version 2.3 <$Revision: 1879490 
gt;
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        uvicorn
Server Hostname:        localhost
Server Port:            8000

Document Path:          /hello-async
Document Length:        4 bytes

Concurrency Level:      8
Time taken for tests:   55.016 seconds
Complete requests:      80
Failed requests:        0
Total transferred:      10240 bytes
HTML transferred:       320 bytes
Requests per second:    1.45 [#/sec] (mean)
Time per request:       5501.634 [ms] (mean)
Time per request:       687.704 [ms] (mean, across all concurrent requests)
Transfer rate:          0.18 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:  5000 5001   0.6   5001    5003
Waiting:     5000 5001   0.6   5001    5003
Total:       5000 5001   0.6   5001    5003

Percentage of the requests served within a certain time (ms)
  50%   5001
  66%   5002
  75%   5002
  80%   5002
  90%   5002
  95%   5002
  98%   5003
  99%   5003
 100%   5003 (longest request)

I wanted to simulate I/O operation, if I call API or database I want to se how much faster is async approach. What happened now I got the same time taken for tests.

If I would do CPU intensive operation I would understand that I will got the same result. What I am doing wrong?

Also in syncing approach the web server is limited to number threads which are spawned, but then what is theoretical limit of async approach? Also how can I test limits number of spawned threads in syncing approach and in async approach what is the limit of event loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文