這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
先前寫過一篇文章:http://www.cnblogs.com/gengzhe/p/5557789.html,也是asp.net core和golang web的對比,熱心的園友提出了幾點問題,如下:
1、需要加入sleep來類比實際業務,這樣才能考驗協程調度能力。
2、golang擅長的是多核環境。
於是今天修正了一下再次進行測試
CPU:E1230-v2
記憶體:16GB
作業系統:centos 7 (3核心2GB記憶體)
下面是代碼:
go
package mainimport ("fmt""net/http""time")func main() {fmt.Println("This is webserver base!")//第一個參數為用戶端發起http請求時的介面名,第二個參數是一個func,負責處理這個請求。http.HandleFunc("/login", loginTask)//伺服器要監聽的主機地址和連接埠號碼err := http.ListenAndServe("192.168.199.236:8081", nil)if err != nil {fmt.Println("ListenAndServe error: ", err.Error())}}func loginTask(w http.ResponseWriter, req *http.Request) {//擷取用戶端通過GET/POST方式傳遞的參數time.Sleep(time.Millisecond*300)req.ParseForm()fmt.Fprint(w, "Hello Word!")}
C#
public class MyHandlerMiddleware { // Must have constructor with this signature, otherwise exception at run time public MyHandlerMiddleware(RequestDelegate next) { // This is an HTTP Handler, so no need to store next } public async Task Invoke(HttpContext context) { await Task.Delay(1*300); await context.Response.WriteAsync("Hello World!"); } // ... } public class Startup { public void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.MapWhen(context => { return context.Request.Path.ToString().EndsWith("jjj.go"); }, ap => { ap.UseMiddleware<MyHandlerMiddleware>(); }); } }
測試結果(1000使用者並發)
go
C#
測試結果確實是golang稍微好一點。
個人總結的差距有2點
1、go是靜態編譯,運行速度按理應該快一些。
2、.NET MVC的封裝度更好,請求在進入商務邏輯前需要進行大量的處理。
以上僅為個人觀點。