Skip to content

Spring Boot 3 和 Python FastAPI 壓力測試比較

Published: at 下午06:50

因為最近公司在 python 開發中 在做新的技術框架選型

看了 flask 和 fastApi

最後因為效能選擇了 fastApi

但如果是這樣 old school 的 java spring boot 呢? 效能不是更好嗎?

如果終究都要改寫成 java 那為什麼不一開始就寫 java ?

離題了

於是就也有把兩個框架 拿來壓力測試 做比較

使用的壓力測試工具是

ab (Apache Bench)

ubuntu

sudo apt-get install apache2-utils

windwos

scoop install ab

在兩個框架都 寫了回字串的 RESTful API

    @GetMapping("test")
    public ResponseEntity<String> test(){
        return new ResponseEntity<>("springboot" , HttpStatus.OK);
    }
@app.get("/")
async def root():
    return {"message": "Welcome to FastAPI!"}

不含任何的 計算處理或是查詢資料庫

然後個別進行測試

測試指令為

ab -n 100000 -c 100 http://localhost:11611/

-n 100000: 表示總共發送 100,000 次請求。

-c 100: 表示同時並發處理 100 個請求(也就是每次發送 100 個請求,同時等待響應)

python fastApi 測試結果

Server Software:        uvicorn
Server Hostname:        localhost
Server Port:            11611

Document Path:          /
Document Length:        33 bytes

Concurrency Level:      100
Time taken for tests:   132.483 seconds
Complete requests:      100000
Failed requests:        0
Total transferred:      17700000 bytes
HTML transferred:       3300000 bytes
Requests per second:    754.82 [#/sec] (mean)
Time per request:       132.483 [ms] (mean)
Time per request:       1.325 [ms] (mean, across all concurrent requests)
Transfer rate:          130.47 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0      14
Processing:     0  132  82.8    115     750
Waiting:        0  112  78.2     93     744
Total:          0  132  82.8    115     750

Percentage of the requests served within a certain time (ms)
  50%    115
  66%    151
  75%    174
  80%    189
  90%    239
  95%    290
  98%    354
  99%    410
 100%    750 (longest request)

springboot 3 測試結果

Server Software:
Server Hostname:        localhost
Server Port:            8187

Document Path:          /test
Document Length:        4 bytes

Concurrency Level:      100
Time taken for tests:   14.293 seconds
Complete requests:      100000
Failed requests:        0
Total transferred:      13600000 bytes
HTML transferred:       400000 bytes
Requests per second:    6996.55 [#/sec] (mean)
Time per request:       14.293 [ms] (mean)
Time per request:       0.143 [ms] (mean, across all concurrent requests)
Transfer rate:          929.23 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0      16
Processing:     0   14   4.6     14      36
Waiting:        0   10   4.9     10      34
Total:          0   14   4.6     14      36

Percentage of the requests served within a certain time (ms)
  50%     14
  66%     15
  75%     16
  80%     17
  90%     19
  95%     22
  98%     26
  99%     28
 100%     36 (longest request)

可能的瑕疵

  1. 都在local
  2. 可能 python 回傳的字 比較多 但應該不會影響結果太多

分析

  1. 每秒處理請求數(RPS)

    • FastAPI:754.82 請求/秒
    • Spring Boot 3:6996.55 請求/秒
      分析:Spring Boot Spring Boot 比 FastAPI 快 9 倍
  2. 平均請求處理時間(Time per request)

    • FastAPI:132.483 毫秒(總平均),1.325 毫秒(單併發平均)
    • Spring Boot 3:14.293 毫秒(總平均),0.143 毫秒(單併發平均)
      分析:Spring Boot 比 FastAPI 快了將近 10 倍
  3. 總耗時

    • FastAPI:花了 132.483 秒(約 2 分 12 秒)
    • Spring Boot 3:花 14.293 秒
      分析:Spring Boot 用不到 FastAPI 十分之一的時間就搞定了同樣的工作量
  4. 延遲:

好 java 不用嗎?

但 java 寫起來確實比 python 麻煩許多