先前写过一篇文章: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的封装度更好,请求在进入业务逻辑前需要进行大量的处理。

以上仅为个人观点。

 

posted on 2016-06-05 20:45  无名书生  阅读(4719)  评论(6编辑  收藏  举报