cd ApiPerformanceTest

最小 GET API

使用帶有路由的最小 API 創建簡單的 GET 方法/factorial

app.MapGet("/factorial", (int n) =>  
{
long Factorial(int number)
{
return number <= 1 ? 1 : number * Factorial(number - 1);
}

long result = Factorial(n);
return result;
})
.WithName("GetFactorial")
.WithOpenApi();

并使用以下命令運行 API

dotnet run

API 將可以訪問,其中檢查 for defined in 和 是我們將為其計算階乘的數字。http://localhost:{portNo}/Factorial/{n}portNo lauchSettings.jsonn

其次,讓我們在 Python 中創建一個類似的 GET 方法

使用以下命令安裝 FastAPI 和 Uvicorn

pip install fastapi uvicorn

創建包含以下內容的文件。main.py

from fastapi import FastAPI  

app = FastAPI()

def factorial(n: int) -> int:
return 1 if n <= 1 else n * factorial(n - 1)

@app.get("/factorial/{n}")
def get_factorial(n: int):
result = factorial(n)
return {"factorial": result}

用于運行 FastAPI 服務器。uvicorn

uvicorn main:app --reload

API 將在 where is 我們將為其計算階乘的數字進行訪問。http://127.0.0.1:8000/factorial/{n}n

性能比較

ApacheBench (ab) 通常用于 Web 服務器、API 和應用程序的性能基準測試。

安裝

sudo apt-get install apache2-utils

用法

確保兩個應用程序服務器都可以在定義的端口號處訪問,并且應該已經啟動并運行,沒有任何錯誤。

ab -n 1000 -c 10 http://localhost:5030/factorial?n=10  
ab -n 1000 -c 10 http://127.0.0.1:8000/factorial/10

.Net 結果

通過在本地調用在 5030 上運行的 .Net API 從 Apache Bench 返回的性能結果。

cmd > ab -n 1000 -c 10 http://localhost:5030/factorial?n=10
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
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)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software: Kestrel
Server Hostname: localhost
Server Port: 5030

Document Path: /factorial?n=10
Document Length: 7 bytes

Concurrency Level: 10
Time taken for tests: 0.184 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 146000 bytes
HTML transferred: 7000 bytes
Requests per second: 5439.01 [#/sec] (mean)
Time per request: 1.839 [ms] (mean)
Time per request: 0.184 [ms] (mean, across all concurrent requests)
Transfer rate: 775.48 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 5
Processing: 0 2 0.9 2 9
Waiting: 0 1 0.9 1 8
Total: 0 2 0.9 2 9

Percentage of the requests served within a certain time (ms)
50% 2
66% 2
75% 2
80% 2
90% 2
95% 3
98% 5
99% 6
100% 9 (longest request)

Python 結果

通過在本地調用在 8000 上運行的 Python API 從 Apache Bench 返回的性能結果。

cmd > ab -n 1000 -c 10 http://127.0.0.1:8000/factorial/10
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software: uvicorn
Server Hostname: 127.0.0.1
Server Port: 8000

Document Path: /factorial/10
Document Length: 21 bytes

Concurrency Level: 10
Time taken for tests: 0.766 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 165000 bytes
HTML transferred: 21000 bytes
Requests per second: 1305.69 [#/sec] (mean)
Time per request: 7.659 [ms] (mean)
Time per request: 0.766 [ms] (mean, across all concurrent requests)
Transfer rate: 210.39 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 2
Processing: 3 7 3.6 7 41
Waiting: 2 6 3.5 6 41
Total: 3 8 3.6 7 41

Percentage of the requests served within a certain time (ms)
50% 7
66% 8
75% 8
80% 8
90% 9
95% 9
98% 11
99% 38
100% 41 (longest request)

比較

C#(帶有 Kestrel 的 .NET Core)

Python(FastAPI 與 Uvicorn)

主要觀察

每秒請求數

每個請求的時間

傳輸速率

連接時間

一致性

簡單負載測試

在高負載下測量吞吐量和響應時間,同時提高并發性。

// For .Net  
ab -n 10000 -c 100 http://127.0.0.1:5219/factorial?n=10

// For Python
ab -n 10000 -c 100 http://127.0.0.1:8000/factorial?n=10

下表比較了 .NET (Kestrel) 和 Python (Uvicorn) 之間的基準測試結果

延遲測試

評估單個請求的響應時間以評估延遲。

// For .Net  
ab -n 1000 -c 1 http://127.0.0.1:5219/factorial?n=10

// For Python
ab -n 1000 -c 1 http://127.0.0.1:8000/factorial?n=10

下表比較了 .NET (Kestrel) 和 Python (Uvicorn) 之間的基準測試結果

故障率測試

檢查在中等負載下有多少個請求失敗。

// For .Net  
ab -n 10000 -c 10 http://127.0.0.1:5219/factorial?n=10

// For Python
ab -n 10000 -c 10 http://127.0.0.1:8000/factorial?n=10

下表比較了 .NET (Kestrel) 和 Python (Uvicorn) 之間的延遲測試結果,對 10000 個請求使用并發級別 10

長連接測試

使用持久連接時測量性能。

// For .Net  
ab -n 10000 -c 10 -H "Connection: Keep-Alive" http://127.0.0.1:5219/factorial?n=10

// For Python
ab -n 10000 -c 10 -H "Connection: Keep-Alive" http://127.0.0.1:8000/factorial?n=10

以下是 Kestrel 和 Uvicorn 服務器在用于 10000 個并發級別為 10 的請求時的比較Keep-Alive

壓力測試

評估極端條件下的服務器限制和行為。

// For .Net  
ab -n 50000 -c 200 http://127.0.0.1:5219/factorial?n=10

// For Python
ab -n 50000 -c 200 http://127.0.0.1:8000/factorial?n=10

這是 Kestrel (.NET) 和 Uvicorn (FastAPI) 之間的性能比較,它們的負載要高得多,為 50,000 個請求和 200 個并發連接

在本次基準測試中**,帶有 Kestrel 的 C#** 的性能明顯優于帶有 FastAPI 和 Uvicorn 的 Python

對于高性能應用程序,尤其是在處理大量并發請求時,C#?在此方案中提供更好的性能。

文章轉自微信公眾號@DotNet NB

上一篇:

使用ASP.NET Core構建RESTful API的技術指南

下一篇:

使用gin搭建api后臺系統之MySQL初步CURD
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費