
2024年在線市場平臺的11大最佳支付解決方案
cd ApiPerformanceTest
使用帶有路由的最小 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
使用以下命令安裝 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
通過在本地調(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)
通過在本地調(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)
在高負(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
2024年在線市場平臺的11大最佳支付解決方案
完整指南:如何在應(yīng)用程序中集成和使用ChatGPT API
選擇AI API的指南:ChatGPT、Gemini或Claude,哪一個(gè)最適合你?
用ASP.NET Core 2.1 建立規(guī)范的 REST API — 緩存和并發(fā)
企業(yè)工商數(shù)據(jù)API用哪種?
2024年創(chuàng)建社交媒體帖子的最佳圖像工具API
2024年小型企業(yè)的7個(gè)最佳短信應(yīng)用API
用gin寫簡單的crud后端API接口
最新LangChain+GLM4開發(fā)AI應(yīng)用程序系列(一):快速入門篇