自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

論HTTP性能,Go與.NET Core一爭雌雄

開發(fā) 后端
我們會比較它們相同的東西,比如應(yīng)用程序、預(yù)期響應(yīng)及運行時的穩(wěn)定性,所以我們不會把像對 JSON 或者 XML 的編碼、解碼這些煩多的事情加入比較游戲中來,僅僅只會使用簡單的文本消息。為了公平起見,我們會分別使用 Go 和 .NET Core 的 MVC 架構(gòu)模式。

 [[205765]]

朋友們,你們好!

近來,我聽到了大量的關(guān)于新出的 .NET Core 和其性能的討論,尤其在 Web 服務(wù)方面的討論更甚。

因為是新出的,我不想立馬就比較兩個不同的東西,所以我耐心等待,想等發(fā)布更穩(wěn)定的版本后再進行。

本周一(8 月 14 日),微軟發(fā)布 .NET Core 2.0 版本,因此,我準備開始。您們認為呢?

如前面所提的,我們會比較它們相同的東西,比如應(yīng)用程序、預(yù)期響應(yīng)及運行時的穩(wěn)定性,所以我們不會把像對 JSON 或者 XML 的編碼、解碼這些煩多的事情加入比較游戲中來,僅僅只會使用簡單的文本消息。為了公平起見,我們會分別使用 Go 和 .NET Core 的 MVC 架構(gòu)模式。

參賽選手

Go (或稱 Golang): 是一種快速增長的開源編程語言,旨在構(gòu)建出簡單、快捷和穩(wěn)定可靠的應(yīng)用軟件。

用于支持 Go 語言的 MVC web 框架并不多,還好我們找到了 Iris ,可勝任此工作。

Iris: 支持 Go 語言的快速、簡單和高效的微型 Web 框架。它為您的下一代網(wǎng)站、API 或分布式應(yīng)用程序奠定了精美的表現(xiàn)方式和易于使用的基礎(chǔ)。

C#: 是一種通用的、面向?qū)ο蟮木幊陶Z言。其開發(fā)團隊由 Anders Hejlsberg 領(lǐng)導(dǎo)。

.NET Core: 跨平臺,可以在極少時間內(nèi)開發(fā)出高性能的應(yīng)用程序。

可從 https://golang.org/dl 下載 Go ,從 https://www.microsoft.com/net/core 下載 .NET Core。

在下載和安裝好這些軟件后,還需要為 Go 安裝 Iris。安裝很簡單,僅僅只需要打開終端,然后執(zhí)行如下語句:

  1. go get -u github.com/kataras/iris 

基準

硬件

  • 處理器: Intel(R) Core(TM) i7–4710HQ CPU @ 2.50GHz 2.50GHz
  • 內(nèi)存: 8.00 GB

軟件

  • 操作系統(tǒng): 微軟 Windows [10.0.15063 版本], 電源計劃設(shè)置為“高性能”
  • HTTP 基準工具: https://github.com/codesenberg/bombardier, 使用***的 1.1 版本。
  • .NET Core: https://www.microsoft.com/net/core, 使用***的 2.0 版本。
  • Iris: https://github.com/kataras/iris, 使用基于 Go 1.8.3 構(gòu)建的*** 8.3 版本。

兩個應(yīng)用程序都通過請求路徑 “api/values/{id}” 返回文本“值”。

.NET Core MVC


 

 

Logo 由 Pablo Iglesias 設(shè)計。

可以使用 dotnet new webapi 命令創(chuàng)建項目,其 webapi 模板會為您生成代碼,代碼包含 GET 請求方法的 返回“值”。

源代碼:

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.IO; 
  4. using System.Linq; 
  5. using System.Threading.Tasks; 
  6. using Microsoft.AspNetCore; 
  7. using Microsoft.AspNetCore.Hosting; 
  8. using Microsoft.Extensions.Configuration; 
  9. using Microsoft.Extensions.Logging; 
  10. namespace netcore_mvc 
  11.     public class Program 
  12.     { 
  13.         public static void Main(string[] args) 
  14.         { 
  15.             BuildWebHost(args).Run(); 
  16.         } 
  17.         public static IWebHost BuildWebHost(string[] args) => 
  18.             WebHost.CreateDefaultBuilder(args) 
  19.                 .UseStartup<Startup>() 
  20.                 .Build(); 
  21.     } 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Threading.Tasks; 
  5. using Microsoft.AspNetCore.Builder; 
  6. using Microsoft.AspNetCore.Hosting; 
  7. using Microsoft.Extensions.Configuration; 
  8. using Microsoft.Extensions.DependencyInjection; 
  9. using Microsoft.Extensions.Logging; 
  10. using Microsoft.Extensions.Options; 
  11. namespace netcore_mvc 
  12.     public class Startup 
  13.     { 
  14.         public Startup(IConfiguration configuration) 
  15.         { 
  16.             Configuration = configuration; 
  17.         } 
  18.         public IConfiguration Configuration { get; } 
  19.         // This method gets called by the runtime. Use this method to add services to the container. 
  20.         public void ConfigureServices(IServiceCollection services) 
  21.         { 
  22.             services.AddMvcCore(); 
  23.         } 
  24.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
  25.         public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
  26.         { 
  27.             app.UseMvc(); 
  28.         } 
  29.     } 
  30. } 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Threading.Tasks; 
  5. using Microsoft.AspNetCore.Mvc; 
  6. namespace netcore_mvc.Controllers 
  7.     // ValuesController is the equivalent 
  8.     // `ValuesController` of the Iris 8.3 mvc application. 
  9.     [Route("api/[controller]")] 
  10.     public class ValuesController : Controller 
  11.     { 
  12.         // Get handles "GET" requests to "api/values/{id}"
  13.         [HttpGet("{id}")] 
  14.         public string Get(int id) 
  15.         { 
  16.             return "value"
  17.         } 
  18.         // Put handles "PUT" requests to "api/values/{id}"
  19.         [HttpPut("{id}")] 
  20.         public void Put(int id, [FromBody]string value) 
  21.         { 
  22.         } 
  23.         // Delete handles "DELETE" requests to "api/values/{id}"
  24.         [HttpDelete("{id}")] 
  25.         public void Delete(int id) 
  26.         { 
  27.         } 
  28.     } 

運行 .NET Core web 服務(wù)項目:

  1. $ cd netcore-mvc 
  2. $ dotnet run -c Release 
  3. Hosting environment: Production 
  4. Content root path: C:\mygopath\src\github.com\kataras\iris\_benchmarks\netcore-mvc 
  5. Now listening on: http://localhost:5000 
  6. Application started. Press Ctrl+C to shut down. 

運行和定位 HTTP 基準工具:

  1. $ bombardier -c 125 -n 5000000 http://localhost:5000/api/values/5 
  2. Bombarding http://localhost:5000/api/values/5 with 5000000 requests using 125 connections 
  3.  5000000 / 5000000 [=====================================================] 100.00% 2m3s 
  4. Done! 
  5. Statistics        Avg      Stdev        Max 
  6.   Reqs/sec     40226.03    8724.30     161919 
  7.   Latency        3.09ms     1.40ms   169.12ms 
  8.   HTTP codes: 
  9.     1xx - 0, 2xx - 5000000, 3xx - 0, 4xx - 0, 5xx - 0 
  10.     others - 0 
  11.   Throughput:     8.91MB/s 

Iris MVC


 

 

Logo 由 Santosh Anand 設(shè)計。

源代碼:

  1. package main 
  2. import ( 
  3.     "github.com/kataras/iris" 
  4.     "github.com/kataras/iris/_benchmarks/iris-mvc/controllers" 
  5. func main() { 
  6.     app := iris.New() 
  7.     app.Controller("/api/values/{id}", new(controllers.ValuesController)) 
  8.     app.Run(iris.Addr(":5000"), iris.WithoutVersionChecker) 
  1. package controllers 
  2. import "github.com/kataras/iris/mvc" 
  3. // ValuesController is the equivalent 
  4. // `ValuesController` of the .net core 2.0 mvc application. 
  5. type ValuesController struct { 
  6.     mvc.Controller 
  7. // Get handles "GET" requests to "api/values/{id}"
  8. func (vc *ValuesController) Get() { 
  9.     // id,_ := vc.Params.GetInt("id"
  10.     vc.Ctx.WriteString("value"
  11. // Put handles "PUT" requests to "api/values/{id}"
  12. func (vc *ValuesController) Put() {} 
  13. // Delete handles "DELETE" requests to "api/values/{id}"
  14. func (vc *ValuesController) Delete() {} 

運行 Go web 服務(wù)項目:

  1. $ cd iris-mvc 
  2. $ go run main.go 
  3. Now listening on: http://localhost:5000 
  4. Application started. Press CTRL+C to shut down. 

運行和定位 HTTP 基準工具:

  1. $ bombardier -c 125 -n 5000000 http://localhost:5000/api/values/5 
  2. Bombarding http://localhost:5000/api/values/5 with 5000000 requests using 125 connections 
  3.  5000000 / 5000000 [======================================================] 100.00% 47s 
  4. Done! 
  5. Statistics        Avg      Stdev        Max 
  6.   Reqs/sec    105643.81    7687.79     122564 
  7.   Latency        1.18ms   366.55us    22.01ms 
  8.   HTTP codes: 
  9.     1xx - 0, 2xx - 5000000, 3xx - 0, 4xx - 0, 5xx - 0 
  10.     others - 0 
  11.   Throughput:    19.65MB/s 

想通過圖片來理解的人,我也把我的屏幕截屏出來了!

請點擊這兒可以看到這些屏幕快照。

總結(jié)

  • 完成 5000000 個請求的時間 - 越短越好。
  • 請求次數(shù)/每秒 - 越大越好。
  • 等待時間 — 越短越好。
  • 吞吐量 — 越大越好。
  • 內(nèi)存使用 — 越小越好。
  • LOC (代碼行數(shù)) — 越少越好。

.NET Core MVC 應(yīng)用程序,使用 86 行代碼,運行 2 分鐘 8 秒,每秒接納 39311.56 個請求,平均 3.19ms 等待,***時到 229.73ms,內(nèi)存使用大約為 126MB(不包括 dotnet 框架)。

Iris MVC 應(yīng)用程序,使用 27 行代碼,運行 47 秒,每秒接納 105643.71 個請求,平均 1.18ms 等待,***時到 22.01ms,內(nèi)存使用大約為 12MB。

還有另外一個模板的基準,滾動到底部。

2017 年 8 月 20 號更新

Josh Clark 和 Scott Hanselman在此 tweet 評論上指出,.NET Core Startup.cs 文件中 services.AddMvc(); 這行可以替換為 services.AddMvcCore();。我聽從他們的意見,修改代碼,重新運行基準,該文章的 .NET Core 應(yīng)用程序的基準輸出已經(jīng)修改。

@topdawgevh @shanselman 他們也在使用 AddMvc() 而不是 AddMvcCore() ...,難道都不包含中間件?

 —  @clarkis117

@clarkis117 @topdawgevh Cool @MakisMaropoulos @benaadams @davidfowl 我們來看看。認真學(xué)習(xí)下怎么使用更簡單的性能默認值。

 —  @shanselman

@shanselman @clarkis117 @topdawgevh @benaadams @davidfowl @shanselman @benaadams @davidfowl 謝謝您們的反饋意見。我已經(jīng)修改,更新了結(jié)果,沒什么不同。對其它的建議,我非常歡迎。

 —  @MakisMaropoulos

它有點稍微的不同但相差不大(從 8.61MB/s 到 8.91MB/s)

想要了解跟 services.AddMvc() 標準比較結(jié)果的,可以點擊這兒。

想再多了解點兒嗎?

我們再制定一個基準,產(chǎn)生 1000000 次請求,這次會通過視圖引擎由模板生成 HTML 頁面。

.NET Core MVC 使用的模板

  1. using System; 
  2. namespace netcore_mvc_templates.Models 
  3.     public class ErrorViewModel 
  4.     { 
  5.         public string Title { get; set; } 
  6.         public int Code { get; set; } 
  7.     } 
  1.  using System; 
  2. using System.Collections.Generic; 
  3. using System.Diagnostics; 
  4. using System.Linq; 
  5. using System.Threading.Tasks; 
  6. using Microsoft.AspNetCore.Mvc; 
  7. using netcore_mvc_templates.Models; 
  8. namespace netcore_mvc_templates.Controllers 
  9.     public class HomeController : Controller 
  10.     { 
  11.         public IActionResult Index() 
  12.         { 
  13.             return View(); 
  14.         } 
  15.         public IActionResult About() 
  16.         { 
  17.             ViewData["Message"] = "Your application description page."
  18.             return View(); 
  19.         } 
  20.         public IActionResult Contact() 
  21.         { 
  22.             ViewData["Message"] = "Your contact page."
  23.             return View(); 
  24.         } 
  25.         public IActionResult Error() 
  26.         { 
  27.             return View(new ErrorViewModel { Title = "Error", Code = 500}); 
  28.         } 
  29.     } 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.IO; 
  4. using System.Linq; 
  5. using System.Threading.Tasks; 
  6. using Microsoft.AspNetCore; 
  7. using Microsoft.AspNetCore.Hosting; 
  8. using Microsoft.Extensions.Configuration; 
  9. using Microsoft.Extensions.Logging; 
  10. namespace netcore_mvc_templates 
  11.     public class Program 
  12.     { 
  13.         public static void Main(string[] args) 
  14.         { 
  15.             BuildWebHost(args).Run(); 
  16.         } 
  17.         public static IWebHost BuildWebHost(string[] args) => 
  18.             WebHost.CreateDefaultBuilder(args) 
  19.                 .UseStartup<Startup>() 
  20.                 .Build(); 
  21.     } 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Threading.Tasks; 
  5. using Microsoft.AspNetCore.Builder; 
  6. using Microsoft.AspNetCore.Hosting; 
  7. using Microsoft.Extensions.Configuration; 
  8. using Microsoft.Extensions.DependencyInjection; 
  9. namespace netcore_mvc_templates 
  10.     public class Startup 
  11.     { 
  12.         public Startup(IConfiguration configuration) 
  13.         { 
  14.             Configuration = configuration; 
  15.         } 
  16.         public IConfiguration Configuration { get; } 
  17.         // This method gets called by the runtime. Use this method to add services to the container. 
  18.         public void ConfigureServices(IServiceCollection services) 
  19.         { 
  20.             /*  An unhandled exception was thrown by the application. 
  21.                 System.InvalidOperationException: No service for type 
  22.                 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered. 
  23.                 Solution: Use AddMvc() instead of AddMvcCore() in Startup.cs and it will work
  24.             */ 
  25.             // services.AddMvcCore(); 
  26.             services.AddMvc(); 
  27.         } 
  28.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
  29.         public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
  30.         { 
  31.             app.UseStaticFiles(); 
  32.             app.UseMvc(routes => 
  33.             { 
  34.                 routes.MapRoute( 
  35.                     name"default"
  36.                     template: "{controller=Home}/{action=Index}/{id?}"); 
  37.             }); 
  38.         } 
  39.     } 
  1. /* 
  2. wwwroot/css 
  3. wwwroot/images 
  4. wwwroot/js 
  5. wwwroot/lib 
  6. wwwroot/favicon.ico 
  7. Views/Shared/_Layout.cshtml 
  8. Views/Shared/Error.cshtml 
  9. Views/Home/About.cshtml 
  10. Views/Home/Contact.cshtml 
  11. Views/Home/Index.cshtml 
  12. These files are quite long to be shown in this article but you can view them at:  
  13. https://github.com/kataras/iris/tree/master/_benchmarks/netcore-mvc-templates 

運行 .NET Core 服務(wù)項目:

  1. $ cd netcore-mvc-templates 
  2. $ dotnet run -c Release 
  3. Hosting environment: Production 
  4. Content root path: C:\mygopath\src\github.com\kataras\iris\_benchmarks\netcore-mvc-templates 
  5. Now listening on: http://localhost:5000 
  6. Application started. Press Ctrl+C to shut down. 

運行 HTTP 基準工具:

  1. Bombarding http://localhost:5000 with 1000000 requests using 125 connections 
  2.  1000000 / 1000000 [====================================================] 100.00% 1m20s 
  3. Done! 
  4. Statistics Avg Stdev Max 
  5.  Reqs/sec 11738.60 7741.36 125887 
  6.  Latency 10.10ms 22.10ms 1.97s 
  7.  HTTP codes: 
  8.  1xx — 0, 2xx — 1000000, 3xx — 0, 4xx — 0, 5xx — 0 
  9.  others — 0 
  10.  Throughput: 89.03MB/s 

Iris MVC 使用的模板

  1. package controllers 
  2. import "github.com/kataras/iris/mvc" 
  3. type AboutController struct{ mvc.Controller } 
  4. func (c *AboutController) Get() { 
  5.     c.Data["Title"] = "About" 
  6.     c.Data["Message"] = "Your application description page." 
  7.     c.Tmpl = "about.html" 
  1. package controllers 
  2. import "github.com/kataras/iris/mvc" 
  3. type ContactController struct{ mvc.Controller } 
  4. func (c *ContactController) Get() { 
  5.     c.Data["Title"] = "Contact" 
  6.     c.Data["Message"] = "Your contact page." 
  7.     c.Tmpl = "contact.html" 
  1. package models 
  2. // HTTPError a silly structure to keep our error page data. 
  3. type HTTPError struct { 
  4.     Title string 
  5.     Code  int 
  1. package controllers 
  2. import "github.com/kataras/iris/mvc" 
  3. type IndexController struct{ mvc.Controller } 
  4. func (c *IndexController) Get() { 
  5.     c.Data["Title"] = "Home Page" 
  6.     c.Tmpl = "index.html" 
  1. package main 
  2. import ( 
  3.     "github.com/kataras/iris/_benchmarks/iris-mvc-templates/controllers" 
  4.     "github.com/kataras/iris" 
  5.     "github.com/kataras/iris/context" 
  6. const ( 
  7.     // templatesDir is the exactly the same path that .NET Core is using for its templates, 
  8.     // in order to reduce the size in the repository. 
  9.     // Change the "C\\mygopath" to your own GOPATH. 
  10.     templatesDir = "C:\\mygopath\\src\\github.com\\kataras\\iris\\_benchmarks\\netcore-mvc-templates\\wwwroot" 
  11. func main() { 
  12.     app := iris.New() 
  13.     app.Configure(configure) 
  14.     app.Controller("/", new(controllers.IndexController)) 
  15.     app.Controller("/about", new(controllers.AboutController)) 
  16.     app.Controller("/contact", new(controllers.ContactController)) 
  17.     app.Run(iris.Addr(":5000"), iris.WithoutVersionChecker) 
  18. func configure(app *iris.Application) { 
  19.     app.RegisterView(iris.HTML("./views"".html").Layout("shared/layout.html")) 
  20.     app.StaticWeb("/public", templatesDir) 
  21.     app.OnAnyErrorCode(onError) 
  22. type err struct { 
  23.     Title string 
  24.     Code  int 
  25. func onError(ctx context.Context) { 
  26.     ctx.ViewData("", err{"Error", ctx.GetStatusCode()}) 
  27.     ctx.View("shared/error.html"
  1. /* 
  2. ../netcore-mvc-templates/wwwroot/css 
  3. ../netcore-mvc-templates/wwwroot/images 
  4. ../netcore-mvc-templates/wwwroot/js 
  5. ../netcore-mvc-templates/wwwroot/lib 
  6. ../netcore-mvc-templates/wwwroot/favicon.ico 
  7. views/shared/layout.html 
  8. views/shared/error.html 
  9. views/about.html 
  10. views/contact.html 
  11. views/index.html 
  12. These files are quite long to be shown in this article but you can view them at:  
  13. https://github.com/kataras/iris/tree/master/_benchmarks/iris-mvc-templates 
  14. */ 

運行 Go 服務(wù)項目:

  1. $ cd iris-mvc-templates 
  2. $ go run main.go 
  3. Now listening on: http://localhost:5000 
  4. Application started. Press CTRL+C to shut down. 

運行 HTTP 基準工具:

  1. Bombarding http://localhost:5000 with 1000000 requests using 125 connections 
  2.  1000000 / 1000000 [======================================================] 100.00% 37s 
  3. Done! 
  4. Statistics Avg Stdev Max 
  5.  Reqs/sec 26656.76 1944.73 31188 
  6.  Latency 4.69ms 1.20ms 22.52ms 
  7.  HTTP codes: 
  8.  1xx — 0, 2xx — 1000000, 3xx — 0, 4xx — 0, 5xx — 0 
  9.  others — 0 
  10.  Throughput: 192.51MB/s 

總結(jié)

  • 完成 1000000 個請求的時間 - 越短越好。
  • 請求次數(shù)/每秒 - 越大越好。
  • 等待時間 — 越短越好。
  • 內(nèi)存使用 — 越小越好。
  • 吞吐量 — 越大越好。

.NET Core MVC 模板應(yīng)用程序,運行 1 分鐘 20 秒,每秒接納 11738.60 個請求,同時每秒生成 89.03M 頁面,平均 10.10ms 等待,***時到 1.97s,內(nèi)存使用大約為 193MB(不包括 dotnet 框架)。

Iris MVC 模板應(yīng)用程序,運行 37 秒,每秒接納 26656.76 個請求,同時每秒生成 192.51M 頁面,平均 1.18ms 等待,***時到 22.52ms,內(nèi)存使用大約為 17MB。

接下來呢?

這里有上面所示的源代碼,請下載下來,在您本地以同樣的基準運行,然后把運行結(jié)果在這兒給大家分享。

想添加 Go 或 C# .net core WEB 服務(wù)框架到列表的朋友請向這個倉庫的 _benchmarks 目錄推送 PR。

我也需要親自感謝下 dev.to 團隊,感謝把我的這篇文章分享到他們的 Twitter 賬戶。

感謝大家真心反饋,玩得開心!

更新 : 2017 年 8 月 21 ,周一

很多人聯(lián)系我,希望看到一個基于 .NET Core 的較低級別 Kestrel 的基準測試文章。

因此我完成了,請點擊下面的鏈接來了解 Kestrel 和 Iris 之間的性能差異,它還包含一個會話存儲管理基準! 

責(zé)任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2015-09-15 10:40:26

HTTP2 WEB 性能優(yōu)化

2024-06-11 09:00:00

異步編程代碼

2024-09-10 08:13:16

Asp項目輕量級

2021-11-14 07:34:57

.NETEventCounte性能

2024-12-05 08:14:41

2024-06-27 10:48:48

2025-02-12 08:50:22

2013-10-08 17:36:06

亞馬遜微軟云計算

2023-08-14 08:34:14

GolangHttp

2025-03-06 02:00:00

.NETGrafana工具

2012-07-18 15:00:38

OpenStackCloudStack開源

2021-04-12 07:03:10

輕量級模塊化框架

2010-05-12 11:08:00

2015-09-15 10:54:54

HTTP2 WEB 性能優(yōu)化

2015-09-15 10:46:29

2020-10-28 15:17:08

Go服務(wù)超時net

2023-01-09 08:14:08

GoHttpServer

2023-09-26 09:42:00

2025-01-10 00:32:48

2025-04-18 08:45:26

點贊
收藏

51CTO技術(shù)棧公眾號