Rust보다 더 빨리 가나요?

Jan 07 2023
Rust에 매우 실망했기 때문에 누군가 제 코드를 수정해 주세요. 여기서도 메신저를 쏘지 마세요(나의 잘못된 코드가 문제라면 저를 쏘셔도 좋습니다).

Rust에 매우 실망했기 때문에 누군가 제 코드를 수정해 주세요. 여기서도 메신저를 쏘지 마세요(나의 잘못된 코드가 문제라면 저를 쏘셔도 좋습니다).

Go와 Rust 사이의 거의 모든 성능 비교에서 Rust가 더 나은 성능을 보인다는 것을 알고 있지만 항상 그렇습니까? 그렇지 않은 것 같고, 사실 제가 공유할 결과는 Rust의 성능이 매우 좋지 않으며 모든 과대광고에도 불구하고 매우 실망스럽습니다.

Go — 실행 결과go build && ./go_test

wrk -t 25 -c 100 -d 60s --timeout 8s http://127.0.0.1:8080/test_top25
Running 1m test @ http://127.0.0.1:8080/test_top25
  25 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     2.37s   399.13ms   4.75s    81.81%
    Req/Sec     2.76      3.37    30.00     86.68%
  2474 requests in 1.00m, 378.22MB read
Requests/sec:     41.18
Transfer/sec:      6.30MB

wrk -t 25 -c 100 -d 60s --timeout 8s http://127.0.0.1:8000/test_top25
Running 1m test @ http://127.0.0.1:8000/test_top25
  25 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     3.30s   560.20ms   6.08s    76.31%
    Req/Sec     2.75      3.88    30.00     84.61%
  1769 requests in 1.00m, 272.14MB read
Requests/sec:     29.43
Transfer/sec:      4.53MB

내가 찾은 모든 성능 테스트는 일종의 가짜 데이터를 사용한 것입니다. 실제 응용 프로그램 설정에서 Rust 성능이 모든 사람이 생각하는 것만큼 좋지 않습니까? 크레이트/라이브러리를 사용하고 있습니까? 확실하지 않으니 한번 살펴보도록 합시다...

use actix_web::{web, App, HttpResponse, HttpServer};
use sqlx::postgres::PgPoolOptions;
use sqlx::types::JsonValue;

#[derive(sqlx::FromRow, serde::Serialize)]
struct Snapshot {
    Resource: serde_json::Value
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let pool = PgPoolOptions::new()
        .max_connections(25)
        .min_connections(25)
        .connect("<postgres_url>").await
        .expect("Failed to create pool.");

    HttpServer::new(move || {
        App::new()
            .wrap(actix_web::middleware::Compress::default())
            .app_data(web::Data::new(pool.clone()))
            .route("/test_top25", web::get().to(|pool: web::Data<sqlx::PgPool>| async move {
                let rows = sqlx::query_as::<_, Snapshot>("SELECT <query> LIMIT 25")
                    .fetch_all(&**pool).await.unwrap();
                HttpResponse::Ok().json(serde_json::json!({ "message": rows }))
            }))
    })
    .client_request_timeout(std::time::Duration::from_secs(60))
    .bind(("0.0.0.0", 8000))?
    .run().await
}

package main

import (
 "context"
 "fmt"
 "net/http"

 "github.com/gin-gonic/gin"
 "github.com/jackc/pgx/v5"
 "github.com/jackc/pgx/v5/pgxpool"
)

func setupRouter() *gin.Engine {
 r := gin.Default()

 psqlInfo := "<postgres_url>"

 config, err := pgxpool.ParseConfig(psqlInfo)
 config.MaxConns = 25
 config.MinConns = 25

 pool, err := pgxpool.NewWithConfig(context.Background(), config)

 r.GET("/test_top25", func(c *gin.Context) {
  res, err := pool.Query(context.Background(), "SELECT <query> LIMIT 25")

  rows, err := pgx.CollectRows(res, pgx.RowTo[any])

  c.JSON(http.StatusOK, gin.H{
   "message": rows,
  })
 })

 return r
}

func main() {
 r := setupRouter()
 r.Run(":8080")
}

누군가 빨리 내 Rust 코드를 수정하세요. 왜냐하면 다른 모든 사람에 따르면 이런 일이 일어나서는 안 되기 때문입니다. 나는 Rust에 대해 적어도 꽤 괜찮다고 생각했고 지금까지 내 인생에서 Go를 한 줄도 작성하지 않았기 때문에 이제 막 배우기 시작한 언어에서 더 나은 성능을 얻을 수 있었다는 사실은 실망.

참고로 저는 이 테스트를 몇 가지 다른 매개변수로 시도했습니다. 다른 스레드 수, 다른 연결 수, 다른 시간 범위 등이 있지만 Rust는 약 15개의 테스트 중 한 세트에서만 Go를 이겼습니다.