自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go vs Rust:文件上傳性能比較

開發(fā) 后端
在本文中,主要測試并比較了Go—Gin和Rust—Actix之間的多部分文件上傳性能。

在本文中,主要測試并比較了Go—Gin和Rust—Actix之間的多部分文件上傳性能。

一、設(shè)置

所有測試都在配備16G內(nèi)存的 MacBook Pro M1 上執(zhí)行。

軟件版本為:

  • Go v1.20.5
  • Rust v1.70.0

測試工具是一個基于 libcurl 并使用標準線程的自定義工具,能夠發(fā)送多部分請求。

資產(chǎn)目錄中有 100,000 個文件。每個文件的大小都是確切的 100K。這些文件數(shù)量在測試工作線程之間進行分配。同一個文件不會一遍又一遍地上傳。工作線程會循環(huán)處理分配給它們的文件。一旦它們處理完所有分配的文件,它們就會回到第一個文件重新開始。

每個請求攜帶兩個文件作為多部分請求體。請求的頭部和體部大致如下:

// -- Headers

{
  "content-length": "205150",
  "content-type": "multipart/form-data; boundary=------------------------3f6a15690b315b91",
}

// -- Body

--------------------------3f6a15690b315b91
Content-Disposition: form-data; name="files"; filename="45469"
Content-Type: application/octet-stream

<<File suppressed>>
--------------------------3f6a15690b315b91
Content-Disposition: form-data; name="files"; filename="42102"
Content-Type: application/octet-stream

<<file suppressed>>
--------------------------3f6a15690b315b91--

二、代碼

1.Go

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/jaevor/go-nanoid"
)

func main() {
    dst := "/Users/mayankc/Work/source/perfComparisons/uploads/"
    canonicID, err := nanoid.Standard(21)
    if err != nil {
        panic(err)
    }

    router := gin.New()
    router.POST("/upload", func(c *gin.Context) {
        form, _ := c.MultipartForm()
        files := form.File["files"]

        for _, file := range files {
            c.SaveUploadedFile(file, dst+canonicID())
        }
        c.Writer.WriteHeader(201)
    })
    router.Run(":3000")
}

2.Rust

use actix_multipart::{
    form::{
        tempfile::{TempFile, TempFileConfig},
        MultipartForm,
    }
};
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer, Responder};
use nanoid::nanoid;

const BASE_DIR: &str = "/Users/mayankc/Work/source/perfComparisons/uploads/";

#[derive(Debug, MultipartForm)]
struct UploadForm {
    #[multipart(rename = "files")]
    files: Vec<TempFile>,
}

async fn save_files(
    MultipartForm(form): MultipartForm<UploadForm>,
) -> Result<impl Responder, Error> {
    for f in form.files {
        let path = format!("{}{}", BASE_DIR, nanoid!());
        f.file.persist(path).unwrap();
    }

    Ok(HttpResponse::Ok())
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .wrap(middleware::Logger::default())
            .app_data(TempFileConfig::default().directory(BASE_DIR))
            .service(
                web::resource("/upload")
                    .route(web::post().to(save_files)),
            )
    })
    .bind(("127.0.0.1", 3000))?
    .run()
    .await
}

Rust代碼已在release mode下編譯。

三、結(jié)果

對10個、50個和100個并發(fā)連接執(zhí)行測試。每個測試總共執(zhí)行10萬個請求。以下是結(jié)果:

四、結(jié)論

從結(jié)果中使用以下公式生成了一個評分表。對于每個測量,獲取獲勝的幅度。如果獲勝幅度為:

  • < 5%,不給予任何分數(shù)
  • 在 5% 和 20% 之間,給予獲勝者 1 分
  • 在 20% 和 50% 之間,給予獲勝者 2 分
  • > 50%,給予獲勝者 3 分

責(zé)任編輯:趙寧寧 來源: 技術(shù)的游戲
相關(guān)推薦

2024-06-24 07:00:00

C++RustGo

2019-05-24 08:48:33

JSONJacksonJSONP

2022-12-15 08:54:28

JAVA性能JDK

2011-04-15 10:26:38

JavaMVC

2019-04-02 15:07:51

API NginxZuul

2009-05-25 08:39:08

iPhone蘋果移動OS

2020-07-07 07:00:00

RustGo語言編程語言

2011-05-18 14:52:04

XML

2023-12-11 08:39:14

Go語言字符串拼

2013-12-16 10:20:48

MySQL數(shù)據(jù)庫

2023-11-20 10:34:09

語言

2009-12-04 19:28:25

FreeBSD 8.0Ubuntu 9.10性能比較

2010-12-27 16:01:45

jQuery選擇器

2012-12-03 10:26:51

Scala

2023-09-24 22:34:41

SpringBootRust

2009-07-01 18:12:18

JSP的優(yōu)勢性能比較

2017-12-14 10:16:01

CaddySSLDockerNginx

2010-03-10 16:35:23

Python編程語言

2011-07-06 14:18:40

Percona SerMySQL

2013-04-03 10:04:36

MySQL 5.6
點贊
收藏

51CTO技術(shù)棧公眾號