先前寫過一篇文章: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 main
import (
"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的封裝度更好,請求在進入業務邏輯前需要進行大量的處理。
以上僅為個人觀點。
