Go 與 NetCore (C#)性能測試對比

準備工作

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 () {

        }
    }
}

測試:

布屬到k8s:點擊查看《應用布屬到k8s》

布屬情況查看:

[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加入到測試中比較。

發佈了31 篇原創文章 · 獲贊 20 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章