.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 性能到底如何。git
2018年8月1日更新:據熱心網友測試,徹底關閉 ASP.NET Core 的日誌功能能夠成噸提高性能,詳情見評論區github
跑分僅供參考golang
測試項目地址:https://github.com/zeekozhu/iriswindows
原文地址:https://hackernoon.com/go-vs-net-core-in-terms-of-http-performance-7535a61b67b8api
本次測試分三輪進行。第一輪測試中,服務器只返回簡單的純文本。第二輪測試中,服務器須要響應渲染出來的 HTML 模板。第三輪測試中,服務器都使用低層 API 來處理請求,並響應簡單的純文本。bash
爲了不特定格式(如,JSON
,XML
)序列化以及編碼帶來的影響,因此此次測試中僅測試了對簡單文本的響應速度與穩定性。爲了公平起見,Go 與 .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
$ 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 請求
的時間 - 越小越好。.NET Core MVC 程序,使用了 2 分 16 秒完成了測試,平均每秒可以處理:36682.65 個請求,平均的響應延遲在 3.40ms 左右,最大延遲爲 254.50ms。mvc
Iris MVC 程序,使用了 1 分 11 秒完成了測試,平均每秒可以處理:70416.19 個請求,平均的響應延遲在 1.77ms 左右,最大延遲爲 31.50ms。框架
接下來就是 MVC 服務端模板渲染性能測試,在這個測試中,服務端程序一共須要處理 1000000 個請求,並響應經過視圖引擎渲染出來的 HTML。工具
$ 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
$ 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 請求
的時間 - 越小越好。.NET Core MVC 程序,使用了 1 分 18 秒完成了測試,平均每秒可以處理:13043.07 個請求,平均的響應延遲在 9.74ms 左右,最大延遲爲 464.00ms。
Iris MVC 程序,使用了 37 秒完成了測試,平均每秒可以處理:33129.39 個請求,平均的響應延遲在 4.64ms 左右,最大延遲爲 34.00ms。
在本次測試中,服務端程序須要處理 1000000 個請求,並返回一個簡單的純文本響應。.NET Core 再也不使用 ASP.NET Core MVC,而是直接經過 Kestrel 處理請求,Iris 也會使用底層的 Handlers 來處理請求。
$ 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
$ 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
1000000 請求
的時間 - 越小越好。.NET Core MVC 程序,使用了 17 秒完成了測試,平均每秒可以處理:55937.72 個請求,平均的響應延遲在 2.23ms 左右,最大延遲爲 87.00ms。
Iris MVC 程序,使用了 12 秒完成了測試,平均每秒可以處理:80559.83 個請求,平均的響應延遲在 1.55ms 左右,最大延遲爲 34.50ms。
儘管微軟在發行說明中提到了 .NET Core 2.1 有較大幅度的性能提高,可是在個人測試結果中與去年 Iris 做者運行的測試結果相差並非很大。若是你認爲本文的測試代碼有問題,歡迎在評論區指出。