cd ApiPerformanceTest

最小 GET API

使用帶有路由的最小 API 創(chuàng)建簡單的 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();

并使用以下命令運(yùn)行 API

dotnet run

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

其次,讓我們在 Python 中創(chuàng)建一個(gè)類似的 GET 方法

使用以下命令安裝 FastAPI 和 Uvicorn

pip install fastapi uvicorn

創(chuàng)建包含以下內(nèi)容的文件。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}

用于運(yùn)行 FastAPI 服務(wù)器。uvicorn

uvicorn main:app --reload

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

性能比較

ApacheBench (ab) 通常用于 Web 服務(wù)器、API 和應(yīng)用程序的性能基準(zhǔn)測試。

安裝

sudo apt-get install apache2-utils

用法

確保兩個(gè)應(yīng)用程序服務(wù)器都可以在定義的端口號處訪問,并且應(yīng)該已經(jīng)啟動(dòng)并運(yùn)行,沒有任何錯(cuò)誤。

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 結(jié)果

通過在本地調(diào)用在 5030 上運(yùn)行的 .Net API 從 Apache Bench 返回的性能結(jié)果。

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 結(jié)果

通過在本地調(diào)用在 8000 上運(yùn)行的 Python API 從 Apache Bench 返回的性能結(jié)果。

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)

主要觀察

每秒請求數(shù)

每個(gè)請求的時(shí)間

傳輸速率

連接時(shí)間

一致性

簡單負(fù)載測試

在高負(fù)載下測量吞吐量和響應(yīng)時(shí)間,同時(shí)提高并發(fā)性。

// 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) 之間的基準(zhǔn)測試結(jié)果

延遲測試

評估單個(gè)請求的響應(yīng)時(shí)間以評估延遲。

// 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) 之間的基準(zhǔn)測試結(jié)果

故障率測試

檢查在中等負(fù)載下有多少個(gè)請求失敗。

// 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) 之間的延遲測試結(jié)果,對 10000 個(gè)請求使用并發(fā)級別 10

長連接測試

使用持久連接時(shí)測量性能。

// 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 服務(wù)器在用于 10000 個(gè)并發(fā)級別為 10 的請求時(shí)的比較Keep-Alive

壓力測試

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

// 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) 之間的性能比較,它們的負(fù)載要高得多,為 50,000 個(gè)請求和 200 個(gè)并發(fā)連接

在本次基準(zhǔn)測試中**,帶有 Kestrel 的 C#** 的性能明顯優(yōu)于帶有 FastAPI 和 Uvicorn 的 Python

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

文章轉(zhuǎn)自微信公眾號@DotNet NB

上一篇:

使用ASP.NET Core構(gòu)建RESTful API的技術(shù)指南

下一篇:

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

我們有何不同?

API服務(wù)商零注冊

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門場景實(shí)測,選對API

#AI文本生成大模型API

對比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

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

10個(gè)渠道
一鍵對比試用API 限時(shí)免費(fèi)