.NET Core 2.1 正式發布之際,微軟團隊在博客的中提到了 .NET Core 2.1 中的性能提升。這讓我想起了去年 Go 語言 Iris MVC 框架作者做的 Go 與 .NET Core 2.0 之間的性能對比,當時的 Iris 在各個方面的表現都基本超出 .NET Core 至少 1 倍,測試結果在社區中引發了不小的討論,事后,微軟團隊的成員與 Iris 的作者進行了親切友好的會談,並就 Web 框架性能調優相關方面展開了深入的討論。現在 .NET Core 2.1 版本正式發布,讓我們來看看現在的 .NET Core 性能到底如何。
2018年8月1日更新:據熱心網友測試,完全關閉 ASP.NET Core 的日志功能可以成噸提升性能,詳情見評論區
跑分僅供參考
測試項目地址:https://github.com/zeekozhu/iris
原文地址:https://hackernoon.com/go-vs-net-core-in-terms-of-http-performance-7535a61b67b8
硬件配置
- 處理器: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz 2.50GHz
- 內存容量: 16.00 GB
軟件版本
- OS: Microsoft Windows [版本號 10 1803 17134.48], 電源計划:“最佳性能”
- HTTP 跑分工具: https://github.com/codesenberg/bombardier, 最新版本 1.1
- .NET Core SDK: https://www.microsoft.com/net/core, 最新版本 2.1.300
- Iris: https://github.com/kataras/iris, 最新版本 10.6.5 使用 go1.10.2-windows/amd64 編譯
本次測試分三輪進行。第一輪測試中,服務器只返回簡單的純文本。第二輪測試中,服務器需要響應渲染出來的 HTML 模板。第三輪測試中,服務器都使用低層 API 來處理請求,並響應簡單的純文本。
響應簡單文本內容
為了避免特定格式(如,JSON
,XML
)序列化以及編碼帶來的影響,所以這次測試中僅測試了對簡單文本的響應速度與穩定性。為了公平起見,Go 與 .NET Core 都使用了日常開發中最常用的 MVC 模式。
.NET Core MVC
$ cd netcore-mvc
$ dotnet run -c Release
Hosting environment: Production
Content root path: C:\mygopath\src\github.com\kataras\iris\_benchmarks\netcore-mvc
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.
Application is shutting down...
$ bombardier -c 125 -n 5000000 http://localhost:5000/api/values/5
Bombarding http://localhost:5000/api/values/5 with 5000000 request(s) using 125 connection(s)
5000000 / 5000000 [================================================================================] 100.00% 2m16s
Done!
Statistics Avg Stdev Max
Reqs/sec 36682.65 7344.86 125924.45
Latency 3.40ms 449.42us 254.50ms
HTTP codes:
1xx - 0, 2xx - 5000000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 8.09MB/s
Iris MVC
$ cd iris-mvc
$ go run main.go
Now listening on: http://localhost:5000
Application started. Press CTRL+C to shut down.
$ bombardier -c 125 -n 5000000 http://localhost:5000/api/values/5
Bombarding http://localhost:5000/api/values/5 with 5000000 request(s) using 125 connection(s)
5000000 / 5000000 [================================================================================] 100.00% 1m11s
Done!
Statistics Avg Stdev Max
Reqs/sec 70416.19 10167.84 89850.59
Latency 1.77ms 74.69us 31.50ms
HTTP codes:
1xx - 0, 2xx - 5000000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 13.09MB/s
簡單文本性能測試結果
- 完成
5000000 請求
的時間 - 越小越好。 - Reqs/sec - 越大越好。
- Latency - 越小越好。
- Throughput - 越大越好。
.NET Core MVC 程序,使用了 2 分 16 秒完成了測試,平均每秒能夠處理:36682.65 個請求,平均的響應延遲在 3.40ms 左右,最大延遲為 254.50ms。
Iris MVC 程序,使用了 1 分 11 秒完成了測試,平均每秒能夠處理:70416.19 個請求,平均的響應延遲在 1.77ms 左右,最大延遲為 31.50ms。
MVC + Templates
接下來就是 MVC 服務端模板渲染性能測試,在這個測試中,服務端程序一共需要處理 1000000 個請求,並響應通過視圖引擎渲染出來的 HTML。
.NET Core MVC with Templates
$ cd netcore-mvc-templates
$ dotnet run -c Release
Hosting environment: Production
Content root path: C:\mygopath\src\github.com\kataras\iris\_benchmarks\netcore-mvc-templates
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.
Application is shutting down...
$ bombardier -c 125 -n 1000000 http://localhost:5000
Bombarding http://localhost:5000 with 1000000 request(s) using 125 connection(s)
1000000 / 1000000 [=================================================================================] 100.00% 1m18s
Done!
Statistics Avg Stdev Max
Reqs/sec 13043.07 4754.11 120734.38
Latency 9.74ms 2.07ms 464.00ms
HTTP codes:
1xx - 0, 2xx - 1000000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 92.25MB/s
Iris MVC with Templates
$ cd iris-mvc-templates
$ go run main.go
Now listening on: http://localhost:5000
Application started. Press CTRL+C to shut down.
$ bombardier -c 125 -n 1000000 http://localhost:5000
Bombarding http://localhost:5000 with 1000000 request(s) using 125 connection(s)
1000000 / 1000000 [==================================================================================] 100.00% 37s
Done!
Statistics Avg Stdev Max
Reqs/sec 26927.88 4174.31 33129.39
Latency 4.64ms 206.76us 34.00ms
HTTP codes:
1xx - 0, 2xx - 1000000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 194.24MB/s
視圖性能測試結果
- 完成
1000000 請求
的時間 - 越小越好。 - Reqs/sec - 越大越好。
- Latency - 越小越好。
- Throughput - 越大越好。
.NET Core MVC 程序,使用了 1 分 18 秒完成了測試,平均每秒能夠處理:13043.07 個請求,平均的響應延遲在 9.74ms 左右,最大延遲為 464.00ms。
Iris MVC 程序,使用了 37 秒完成了測試,平均每秒能夠處理:33129.39 個請求,平均的響應延遲在 4.64ms 左右,最大延遲為 34.00ms。
低層 API 簡單文本性能測試
在本次測試中,服務端程序需要處理 1000000 個請求,並返回一個簡單的純文本響應。.NET Core 不再使用 ASP.NET Core MVC,而是直接通過 Kestrel 處理請求,Iris 也會使用底層的 Handlers 來處理請求。
.NET Core (Kestrel)
$ cd netcore
$ dotnet run -c Release
Hosting environment: Production
Content root path: C:\mygopath\src\github.com\kataras\iris\_benchmarks\netcore
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.
$ bombardier -c 125 -n 1000000 http://localhost:5000/api/values/5
Bombarding http://localhost:5000/api/values/5 with 1000000 request(s) using 125 connection(s)
1000000 / 1000000 [==================================================================================] 100.00% 17s
Done!
Statistics Avg Stdev Max
Reqs/sec 55937.72 4492.32 66770.94
Latency 2.23ms 328.65us 87.00ms
HTTP codes:
1xx - 0, 2xx - 1000000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 10.13MB/s
Iris
$ cd iris
$ go run main.go
Now listening on: http://localhost:5000
Application started. Press CTRL+C to shut down.
$ bombardier -c 125 -n 1000000 http://localhost:5000/api/values/5
Bombarding http://localhost:5000/api/values/5 with 1000000 request(s) using 125 connection(s)
1000000 / 1000000 [==================================================================================] 100.00% 12s
Done!
Statistics Avg Stdev Max
Reqs/sec 80559.83 6029.77 92301.38
Latency 1.55ms 185.02us 34.50ms
HTTP codes:
1xx - 0, 2xx - 1000000, 3xx - 0, 4xx - 0, 5xx - 0
others - 0
Throughput: 14.98MB/s
低層 API 簡單文本性能測試結果
- 完成
1000000 請求
的時間 - 越小越好。 - Reqs/sec - 越大越好。
- Latency - 越小越好。
- Throughput - 越大越好。
.NET Core MVC 程序,使用了 17 秒完成了測試,平均每秒能夠處理:55937.72 個請求,平均的響應延遲在 2.23ms 左右,最大延遲為 87.00ms。
Iris MVC 程序,使用了 12 秒完成了測試,平均每秒能夠處理:80559.83 個請求,平均的響應延遲在 1.55ms 左右,最大延遲為 34.50ms。
總結
盡管微軟在發行說明中提到了 .NET Core 2.1 有較大幅度的性能提升,但是在我的測試結果中與去年 Iris 作者運行的測試結果相差並不是很大。如果你認為本文的測試代碼有問題,歡迎在評論區指出。