准备工作

Go(基于Gin)与 Net Core 准备一最简单的接口,为避免影响,不返回任何数据。

Go :test.go

package test

import (
	"github.com/gin-gonic/gin"
)

func Index(c *gin.Context) {
	c.Done()
}

Go:router.go

func InitRouter() *gin.Engine {
	router := gin.New()
	router.GET("/favicon.ico", func(c *gin.Context) {
	})

	testRouter := router.Group("test")
	{
		testRouter.GET("index", test.Index)
	}

	return router
}

Go:main.go

package main

import (
	"findo.api/conf"
	"findo.api/router"
	"findo.api/services/redis"
)

func main() {
	router := router.InitRouter()
	router.Run()
}

测试Go应用

Net Core

MainController.cs,/main/index不做任何处理

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace Findo.Controllers {

    [Route ("Main")]
    public class MainController {

        private IHttpContextAccessor _accessor;
        public MainController (IHttpContextAccessor accessor) {
            _accessor = accessor;
        }

        [AllowAnonymous]
        [HttpPost]
        [HttpGet]
        [Route ("index")]
        public void index () {

        }
    }
}

测试:

布属情况查看:

[root@k8s-0001 wrk]# kubectl get pods -n develop -o wide
NAME                                        READY   STATUS    RESTARTS   AGE     IP              NODE       NOMINATED NODE   READINESS GATES
findo-api-84bd4f9bdc-97qpj                  1/1     Running   1          116m    192.168.1.117   k8s-0004   <none>           <none>
findo-api-84bd4f9bdc-x2llt                  1/1     Running   1          116m    192.168.1.85    k8s-0003   <none>           <none>
go-findo-api-8598ff7d86-2c9mw               1/1     Running   2          137m    192.168.1.117   k8s-0004   <none>           <none>
go-findo-api-8598ff7d86-77m95               1/1     Running   1          146m    192.168.1.85    k8s-0003   <none>           <none>
go-findo-api-8598ff7d86-hswhx               1/1     Running   0          137m    192.168.1.100   k8s-0002   <none>           <none>

测试比较:

1、镜像大小比较

fengyideMacBook-Pro:Downloads fengyi$ docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
fengyily/netcore.findo.api             v2                  70d6599dcdb0        2 hours ago         264MB
fengyily/go.findo.api                  auto                d9cb3d8ac5ac        2 hours ago         21.8MB

由于go应用是基于 alpine制作,大小只有 21M,.net core应用 264M

2、压力测试:使用wrk压测。

先安装wrk

git clone https://github.com/wg/wrk.git  
cd wrk  
make  

第一次测试:内网、1CPU 1GB内存

# 以下为 GO 应用的测试结果
内网测试,云服务器,入门型,1 CPU 1 GB内存
Running 1m test @ http://192.168.1.100:8080/test/index
  12 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    24.11ms    5.04ms  88.87ms   82.68%
    Req/Sec     3.46k   289.38     4.22k    72.51%
  2480092 requests in 1.00m, 177.39MB read
Requests/sec:  41307.53
Transfer/sec:      2.95MB

#以下为 .net Core应用的测试结果
[root@k8s-0001 wrk]# ./wrk -t 12 -c 1000 -d 60s  http://192.168.1.85:6660/main/index
Running 1m test @ http://192.168.1.85:6660/main/index
  12 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    89.63ms   35.21ms   1.98s    81.24%
    Req/Sec     0.93k   305.73     4.47k    72.13%
  663779 requests in 1.00m, 58.24MB read
  Socket errors: connect 0, read 0, write 0, timeout 71
Requests/sec:  11053.71
Transfer/sec:      0.97MB

GO:Requests/sec:  41307.53
.Net Core:Requests/sec:  11053.71

第二次测试:内网、2CPU 4GB内存(我采用的是云服务器,通过更改服务器规格实现)

# Go 内网测试,云服务器,入门型,2vCPUs | 4GB
[root@k8s-0001 wrk]#  ./wrk -t 12 -c 1000 -d 60s  http://192.168.1.117:8080/test/index
Running 1m test @ http://192.168.1.117:8080/test/index
  12 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    13.89ms    3.61ms  66.14ms   82.41%
    Req/Sec     6.01k   240.48    11.71k    85.08%
  4309218 requests in 1.00m, 308.22MB read
Requests/sec:  71716.09
Transfer/sec:      5.13MB

# Net Core 内网测试,云服务器,入门型,2vCPUs | 4GB
[root@k8s-0001 wrk]#  ./wrk -t 12 -c 1000 -d 60s  http://192.168.1.117:6660/main/index
Running 1m test @ http://192.168.1.117:6660/main/index
  12 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    51.47ms   64.88ms   1.46s    94.90%
    Req/Sec     1.91k   446.08     4.37k    73.41%
  1364717 requests in 1.00m, 119.74MB read
Requests/sec:  22707.53
Transfer/sec:      1.99MB

结果:
Go:Requests/sec:  71716.09
Net Core:Requests/sec:  22707.53

第三次测试:内网测试,云服务器,入门型,4 CPU 4GB 内存

# Go 内网测试,云服务器,入门型,4 CPU 4GB 内存
[root@k8s-0001 wrk]#  ./wrk -t 12 -c 1000 -d 60s  http://192.168.1.85:8080/test/index
Running 1m test @ http://192.168.1.85:8080/test/index
  12 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    30.23ms   52.63ms 420.53ms   86.08%
    Req/Sec     8.96k     4.62k   30.90k    66.28%
  5914190 requests in 1.00m, 423.02MB read
Requests/sec:  98407.30
Transfer/sec:      7.04MB

# Net Core 内网测试,云服务器,入门型,4 CPU 4GB 内存
[root@k8s-0001 wrk]#  ./wrk -t 12 -c 1000 -d 60s  http://192.168.1.85:6660/main/index
Running 1m test @ http://192.168.1.85:6660/main/index
  12 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    27.26ms   34.34ms 862.66ms   93.93%
    Req/Sec     3.66k   645.86     7.12k    87.61%
  2613362 requests in 1.00m, 229.29MB read
Requests/sec:  43530.40
Transfer/sec:      3.82MB

结果:
GO:Requests/sec:  98407.30
Net Core:Requests/sec:  43530.40

采用.Net core 3.1 重新制作镜像,Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 6660

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src

# COPY ["Findo.csproj", "./"]
COPY . .
RUN dotnet restore "./Findo.csproj"

WORKDIR "/src/."
# RUN dotnet build "Findo.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Findo.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ADD dbinit.ini .
COPY appsettings.json.bak appsettings.json
ENTRYPOINT ["dotnet", "Findo.dll"]

fengyideMacBook-Pro:Findo fengyi$ docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
fengyily/netcore.findo.api             v3                  6cbac6da8f2e        23 minutes ago      217MB
fengyily/netcore.findo.api             v2                  70d6599dcdb0        2 days ago          264MB
fengyily/netcore.findo.api             v1                  4843c5a36ffe        2 days ago          264MB
mcr.microsoft.com/dotnet/core/sdk      3.1                 0a4c7c13f9d6        2 weeks ago         689MB
mcr.microsoft.com/dotnet/core/sdk      2.1                 be2b589b3992        2 weeks ago         1.74GB
mcr.microsoft.com/dotnet/core/aspnet   3.1                 e28362768eed        2 weeks ago         207MB
mcr.microsoft.com/dotnet/core/aspnet   2.1                 d27433e73f8b        2 weeks ago         253MB
debian                                 latest              a8797652cfd9        2 weeks ago         114MB
golang                                 alpine              87eefb76f0a8        2 weeks ago         359MB
alpine                                 latest              e7d92cdc71fe        4 weeks ago         5.59MB
golang                                 latest              6586e3d10e96        2 weeks ago         803MB

3.1的镜像比 2.1的镜像要少不少,打出来的镜像也小 50M左右,接下来测试对比下

#Net Core 3.1 内网测试,云服务器,入门型,1 CPU 1 GB内存
 ./wrk -t 12 -c 1000 -d 60s  http://192.168.1.100:6660/main/index
Running 1m test @ http://192.168.1.100:6660/main/index
  12 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    64.81ms   35.99ms 813.41ms   92.09%
    Req/Sec     1.33k   375.18     2.47k    71.48%
  886475 requests in 1.00m, 77.78MB read
  Socket errors: connect 0, read 0, write 0, timeout 996
Requests/sec:  14758.00
Transfer/sec:      1.29MB

总结:

对比 1CPU 1GB内存

GO:Requests/sec: 41307.53

Net Core 2.1: Requests/sec:  11053.71

Net Core 3.1: Requests/sec:  14758.00

Go性能确实优秀,几乎是.Net Core 的4倍,net core 3.1性能比2.1也有30%左右的提升,但还是远不如GO的性能。

待有时间,再把Java、PHP、Python加入到测试中比较。