net core与golang web

Asp.net core与golang web简单对比测试

最近因为工作需要接触了go语言,又恰好asp.net core发布RC2,就想简单做个对比测试。

 

下面是测试环境:

 

CPU:E3-1230 v2

 

内存:16G

 

电脑有点不给力

 

操作系统:Centos7.0(虚拟机单核2G内存)

 

asp.net core rc2

 

golang v1.7beta1

 

下面是各自的代码:

 

go

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main
 
import (
    "fmt"
    "net/http"
)
 
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方式传递的参数
    req.ParseForm()
    fmt.Fprint(w, "Hello World!")
}

 

 C#

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 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>();
        });
    }
}

 

 都是简单路由和简单返回字符串

 

 

下面是测试结果

 

go

 

 

 

asp.net core

 

 

 

 

从测试结果看,asp.net core更好一些,包括响应时间和并发数。按理说go应该比.net core快才对。希望各位大神再多做对比测试来反驳我,我测试N次后都是这个结果

 

 

不过在windows环境下,golang的并发能到6000左右,而.net core依然在4600多,不过响应速度.net core依然比golang快一些,这个我有点费解。按理说windows应该是.net的天下才对,哈哈

 

 

最近反复对asp.net core进行测试,发现真的非常给力,欢迎大家多多尝试

 

 

修正测试:http://www.cnblogs.com/gengzhe/p/5561718.html

原文地址:https://www.cnblogs.com/Leo_wl/p/5561895.html