2025年再来测试一下.NET各个版本和Golang&Rust的web性能,哪个更快?


 

今天无聊又翻出了一篇很久以前golang和.net测试的文章(原文),

很是好奇7、8年过去了,golang和.net 有啥变化吗?

于是我在电脑上又测了一遍。

我的电脑是win10系统,.net sdk都下了最新的版本,重新安装了一编,golang用的是go1.24.1。

添加了rust 的actix-web和may_minihttp。为啥是may_minihttp?因为在techempower第23轮测试中may_minihttp排第一。

techempower第23轮

 

好了,各位大佬来看看测试结果吧

 

.NET个版本测试结果:

 

gin和iris框架测试结果:

 

actix-web和may_minihttp框架测试结果:

 

  • 完成 5000000 个请求的时间 – 越短越好。
  • 请求次数/每秒 – 越大越好。
  • 等待时间 — 越短越好。
  • 内存使用 — 越小越好。
  • 吞吐量 — 越大越好。

 

.NET 代码:

NET5.0、6.0、7.0:
using Microsoft.AspNetCore.Mvc;
namespace _5.Controllers
{
    // ValuesController is the equivalent
    // `ValuesController` of the Iris 8.3 mvc application.

    [Route("api/[controller]")]
    public class ValuesController : ControllerBase
    {
        // Get handles "GET" requests to "api/values/{id}".
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }
        // Put handles "PUT" requests to "api/values/{id}".
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }
        // Delete handles "DELETE" requests to "api/values/{id}".
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}

 

NET8.0、9.0、10.0:
app.MapGet("/api/values/{id}", (int id) =>
{
    return "value";
})
.WithName("GetApi")
.WithOpenApi();

 

Gin 代码:

package main

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

func main() {
    gin.SetMode(gin.ReleaseMode)
    gin.DefaultWriter = ioutil.Discard
    r := gin.Default()
    r.GET("/api/values/:id", func(c *gin.Context) {
        c.String(200, "value")
    })
    r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

  

iris 代码:

package main

import "github.com/kataras/iris/v12"

func main() {
    app := iris.New()

    booksAPI := app.Party("/api")
    {
        booksAPI.Use(iris.Compression)
        booksAPI.Get("/values/{id}", Get)
    }

    app.Listen(":8080")
}


func Get(ctx iris.Context) {
    // id,_ := vc.Params.GetInt("id")
    ctx.WriteString("value")
}

may_minihttp 代码:

use std::io;
use may_minihttp::{HttpServer, HttpService, Request, Response};
use may::coroutine;

#[derive(Clone)]
struct HelloWorld;

impl HttpService for HelloWorld {
    fn call(&mut self, _req: Request, res: &mut Response) -> io::Result<()> {
        match _req.path() {
            "/api/values" => {
                res.body("Hello, world!");
            }
            _ => {
                res.status_code(404, "Not Found");
            }
        }
        
        Ok(())
    }
}

// 在 `main` 函数中启动服务器。
fn main() {
        // 配置 may 协程池
    may::config()
        .set_workers(8)           // 设置线程数
        .set_pool_capacity(500)    // 设置协程池容量
        .set_stack_size(0x1000);   // 设置协程栈大小
    let server = HttpServer(HelloWorld).start("0.0.0.0:8080").unwrap();
    println!("Server running at http://127.0.0.1:8080");
    server.join().unwrap();
}

  

actix-web代码:

use actix_web::{web, App, HttpRequest, HttpServer, Responder};

async fn greet(req: HttpRequest) -> impl Responder {
    let name = req.match_info().get("name").unwrap_or("World");
    format!("Hello {}!", &name)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(greet))
            .route("/api/values/{name}", web::get().to(greet))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

电脑配置:

  • CPU:7840H

  • 内存:32.0 GB

  • 网卡:AX210

  • 操作系统: Windows 10 22H2 专业版

参考地址:

https://linux.cn/article-8935-1-rel.html

https://hackernoon.com/go-vs-net-core-in-terms-of-http-performance-7535a61b67b8