找回密码
 立即注册
首页 业界区 安全 2025年再来测试一下.NET各个版本和Golang&Rust的web性能 ...

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

狞嗅 2025-6-1 20:54:22
 

今天无聊又翻出了一篇很久以前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轮:

1.png

 
好了,各位大佬来看看测试结果吧
 
.NET个版本测试结果:

2.png

 
gin和iris框架测试结果:

3.png

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

4.png

 

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

NET5.0、6.0、7.0:
  1. using Microsoft.AspNetCore.Mvc;
  2. namespace _5.Controllers
  3. {
  4.     // ValuesController is the equivalent
  5.     // `ValuesController` of the Iris 8.3 mvc application.
  6.     [Route("api/[controller]")]
  7.     public class ValuesController : ControllerBase
  8.     {
  9.         // Get handles "GET" requests to "api/values/{id}".
  10.         [HttpGet("{id}")]
  11.         public string Get(int id)
  12.         {
  13.             return "value";
  14.         }
  15.         // Put handles "PUT" requests to "api/values/{id}".
  16.         [HttpPut("{id}")]
  17.         public void Put(int id, [FromBody] string value)
  18.         {
  19.         }
  20.         // Delete handles "DELETE" requests to "api/values/{id}".
  21.         [HttpDelete("{id}")]
  22.         public void Delete(int id)
  23.         {
  24.         }
  25.     }
  26. }
复制代码
 
NET8.0、9.0、10.0:
  1. app.MapGet("/api/values/{id}", (int id) =>
  2. {
  3.     return "value";
  4. })
  5. .WithName("GetApi")
  6. .WithOpenApi();
复制代码
 
Gin 代码:
  1. package main
  2. import (
  3.   "github.com/gin-gonic/gin"
  4.   "io/ioutil"
  5. )
  6. func main() {
  7.     gin.SetMode(gin.ReleaseMode)
  8.     gin.DefaultWriter = ioutil.Discard
  9.     r := gin.Default()
  10.     r.GET("/api/values/:id", func(c *gin.Context) {
  11.         c.String(200, "value")
  12.     })
  13.     r.Run() // 监听并在 0.0.0.0:8080 上启动服务
  14. }
复制代码
  
iris 代码:
  1. package main
  2. import "github.com/kataras/iris/v12"
  3. func main() {
  4.     app := iris.New()
  5.     booksAPI := app.Party("/api")
  6.     {
  7.         booksAPI.Use(iris.Compression)
  8.         booksAPI.Get("/values/{id}", Get)
  9.     }
  10.     app.Listen(":8080")
  11. }
  12. func Get(ctx iris.Context) {
  13.     // id,_ := vc.Params.GetInt("id")
  14.     ctx.WriteString("value")
  15. }
复制代码
may_minihttp 代码:
  1. use std::io;
  2. use may_minihttp::{HttpServer, HttpService, Request, Response};
  3. use may::coroutine;
  4. #[derive(Clone)]
  5. struct HelloWorld;
  6. impl HttpService for HelloWorld {
  7.     fn call(&mut self, _req: Request, res: &mut Response) -> io::Result<()> {
  8.         match _req.path() {
  9.             "/api/values" => {
  10.                 res.body("Hello, world!");
  11.             }
  12.             _ => {
  13.                 res.status_code(404, "Not Found");
  14.             }
  15.         }
  16.         
  17.         Ok(())
  18.     }
  19. }
  20. // 在 `main` 函数中启动服务器。
  21. fn main() {
  22.         // 配置 may 协程池
  23.     may::config()
  24.         .set_workers(8)           // 设置线程数
  25.         .set_pool_capacity(500)    // 设置协程池容量
  26.         .set_stack_size(0x1000);   // 设置协程栈大小
  27.     let server = HttpServer(HelloWorld).start("0.0.0.0:8080").unwrap();
  28.     println!("Server running at http://127.0.0.1:8080");
  29.     server.join().unwrap();
  30. }
复制代码
  
actix-web代码:
  1. use actix_web::{web, App, HttpRequest, HttpServer, Responder};
  2. async fn greet(req: HttpRequest) -> impl Responder {
  3.     let name = req.match_info().get("name").unwrap_or("World");
  4.     format!("Hello {}!", &name)
  5. }
  6. #[actix_web::main]
  7. async fn main() -> std::io::Result<()> {
  8.     HttpServer::new(|| {
  9.         App::new()
  10.             .route("/", web::get().to(greet))
  11.             .route("/api/values/{name}", web::get().to(greet))
  12.     })
  13.     .bind("127.0.0.1:8080")?
  14.     .run()
  15.     .await
  16. }
复制代码
电脑配置:


  • 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

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册