{"id":361482,"date":"2024-05-21T01:14:35","date_gmt":"2024-05-21T01:14:35","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=361482"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=361482","title":{"rendered":"<span>Go (fiber) vs Rust (axum) JWT&amp;DB<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430 <a href=\"http:\/\/medium.com\" rel=\"noopener noreferrer nofollow\">medium.com<\/a> \u0435\u0441\u0442\u044c \u0440\u044f\u0434 \u0441\u0442\u0430\u0442\u0435\u0439 \u0441\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u044b\u0445 web-\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445. \u041e\u0434\u043d\u0430 \u0438\u0437 \u043d\u0438\u0445 <a href=\"https:\/\/medium.com\/deno-the-complete-reference\/go-vs-rust-performance-comparison-for-jwt-verify-and-mysql-query-61aeccac05e8\" rel=\"noopener noreferrer nofollow\">Go vs Rust: Performance comparison for JWT verify and MySQL query<\/a> \u0438 \u0441\u0443\u0434\u044f \u043f\u043e \u043d\u0435\u0439, Go \u043d\u0430 42% \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0447\u0435\u043c Rust. \u042f \u0440\u0435\u0448\u0438\u043b \u043f\u0435\u0440\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0438 \u0437\u0430\u043e\u0434\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c Gin \u043d\u0430 Fiber, Axis \u043d\u0430 Axum \u0438 MySQL \u043d\u0430 PostgreSQL.<\/p>\n<p>Web-\u0441\u0435\u0440\u0432\u0438\u0441 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439 \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443 JWT, \u0438\u0441\u043a\u0430\u0442\u044c \u0432 \u0411\u0414 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 \u0434\u0430\u043d\u043d\u044b\u043c email \u0438\u0437 JWT \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u0432\u0438\u0434\u0435 json. \u0422\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u0434\u043e\u0431\u043d\u0430\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0432\u0441\u0435\u043c\u0435\u0441\u0442\u043d\u043e, \u0442\u043e \u0442\u0435\u0441\u0442 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439.<\/p>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u0433\u043e\u0442\u043e\u0432\u0438\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0411\u0414. \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 PostgreSQL \u0438 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0435\u0435 \u0431\u0443\u0434\u0435\u043c \u0432 Docker \u0447\u0435\u0440\u0435\u0437 compose. \u0412 \u043f\u0430\u043f\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0448\u0430 \u0411\u0414, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b init.sql. \u0412 \u043d\u0435\u043c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u0443\u044e \u0431\u0430\u0437\u0443 \u0438 \u0432 \u043d\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 users:<\/p>\n<pre><code class=\"sql\">CREATE DATABASE testbench;  \\connect testbench;  CREATE TABLE users(     email VARCHAR(255) NOT NULL PRIMARY KEY,     first VARCHAR(255),     last VARCHAR(255),     country VARCHAR(255),     city VARCHAR(255),     age int );<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0442\u0430\u043c \u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0430\u043f\u043a\u0443 db \u0438 \u0444\u0430\u0439\u043b docker-compose.yaml \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"yaml\">services:   postgres:     image: postgres:alpine     environment:       - POSTGRES_PASSWORD=123456     volumes:       - .\/db:\/var\/lib\/postgresql\/data       # \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0438\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0431\u0430\u0437\u044b       - .\/init.sql:\/docker-entrypoint-initdb.d\/init.sql     ports:       - \"5432:5432\"<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440:<\/p>\n<pre><code class=\"bash\">$ docker-compose up<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u0430\u0437\u0443 100 000 \u0437\u0430\u043f\u0438\u0441\u044f\u043c\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u0435\u043d \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/www.getsynth.com\/\" rel=\"noopener noreferrer nofollow\">Synth<\/a>. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b \u0441 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u043c\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 (\u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e email \u0443 \u043d\u0430\u0441 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0439 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u044e\u0447):<\/p>\n<pre><code class=\"json\">{     \"type\": \"array\",     \"length\": {         \"type\": \"number\",         \"constant\": 1     },     \"content\": {         \"type\": \"object\",         \"email\": {             \"type\": \"unique\",             \"content\": {                 \"type\": \"string\",                 \"faker\": {                     \"generator\": \"free_email\"                 }             }         },         \"first\": {             \"type\": \"string\",             \"faker\": {                 \"generator\": \"first_name\"             }         },         \"last\": {             \"type\": \"string\",             \"faker\": {                 \"generator\": \"last_name\"             }         },         \"city\": {             \"type\": \"string\",             \"faker\": {                 \"generator\": \"city_name\"             }         },         \"country\": {             \"type\": \"string\",             \"faker\": {                 \"generator\": \"country_name\"             }         },         \"age\": {             \"type\": \"number\",             \"subtype\": \"i32\",             \"range\": {                 \"low\": 18,                 \"high\": 55,                 \"step\": 1             }         }     } }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"bash\">$ synth generate .\/ --to postgres:\/\/postgres:123456@localhost:5432\/testbench --size 100000<\/code><\/pre>\n<p>\u0411\u0414 \u0433\u043e\u0442\u043e\u0432\u0430, \u043f\u0438\u0448\u0435\u043c \u0441\u0430\u043c\u0438 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0438\u0441\u044b. <\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430 Go:<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"go\">package main  import ( \"bufio\" \"database\/sql\" \"log\" \"os\" \"strings\" \"time\"  \"github.com\/gofiber\/fiber\/v2\" \"github.com\/golang-jwt\/jwt\/v5\" _ \"github.com\/lib\/pq\" )  type MyCustomClaims struct { Email string `json:\"email\"` jwt.RegisteredClaims }  type User struct { Email  string First  string Last   string City   string Country string Age    int }  var jwtSecret = \"mysuperPUPERsecret100500security\"  func getToken(c *fiber.Ctx) string { hdr := c.Get(\"Authorization\") if hdr == \"\" { return \"\" }  token := strings.Split(hdr, \"Bearer \")[1] return token }  func main() { app := fiber.New() db, err := sql.Open(\"postgres\", \"user=postgres password=123456 dbname=testbench sslmode=disable\")  if err != nil { return }  defer db.Close()  db.SetMaxOpenConns(10) db.SetMaxIdleConns(10)  app.Get(\"\/\", func(c *fiber.Ctx) error { tokenString := getToken(c) if tokenString == \"\" { return c.SendStatus(fiber.StatusUnauthorized) } token, err := jwt.ParseWithClaims(tokenString, &amp;MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) { return []byte(jwtSecret), nil })  if err != nil { log.Println(err) return c.SendStatus(fiber.StatusUnauthorized) }  claims := token.Claims.(*MyCustomClaims)  query := \"SELECT * FROM users WHERE email=$1\" row := db.QueryRow(query, claims.Email)  var user User = User{} err2 := row.Scan(&amp;user.Email, &amp;user.First, &amp;user.Last, &amp;user.Country, &amp;user.City, &amp;user.Age) if err2 == sql.ErrNoRows { return c.SendStatus(fiber.StatusNotFound) } if err2 != nil { log.Println(err2) return c.SendStatus(fiber.StatusInternalServerError) }  return c.JSON(user) })  \/\/\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0440\u0443\u0447\u043a\u0430 app.Get(\"\/randomtoken\", func(c *fiber.Ctx) error { file, err := os.Create(\"tokens.txt\") if err != nil { log.Println(err) return c.SendStatus(fiber.StatusInternalServerError) }  writer := bufio.NewWriter(file)  rows, err := db.Query(\"SELECT * FROM USERS OFFSET floor(random() * 100000) LIMIT 10\") if err != nil { return c.SendStatus(fiber.StatusInternalServerError) }  for rows.Next() { var user User err = rows.Scan(&amp;user.Email, &amp;user.First, &amp;user.Last, &amp;user.Country, &amp;user.City, &amp;user.Age) if err != nil { log.Println(err) return c.SendStatus(fiber.StatusInternalServerError) }  claims := MyCustomClaims{ user.Email, jwt.RegisteredClaims{ ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)), }, }  token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) ss, err := token.SignedString([]byte(jwtSecret))  if err != nil { log.Println(err) return c.SendStatus(fiber.StatusInternalServerError) }  _, err = writer.WriteString(ss + \"\\n\") if err != nil { file.Close() log.Println(err) return c.SendStatus(fiber.StatusInternalServerError) }  }  writer.Flush() file.Close()  return c.SendFile(file.Name()) })  log.Fatal(app.Listen(\":3000\")) } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430 Rust<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"rust\">use axum::{     extract::State,     http::{header::AUTHORIZATION, HeaderMap, StatusCode},     response::IntoResponse,     routing::get,     Json, Router, }; use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; use serde::{Deserialize, Serialize}; use sqlx::{postgres::PgPoolOptions, Pool, Postgres};  #[derive(Debug, Serialize, Deserialize)] struct Claims {     email: String,     exp: usize, }  #[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] pub struct User {     pub email: String,     pub first: Option&lt;String>,     pub last: Option&lt;String>,     pub city: Option&lt;String>,     pub country: Option&lt;String>,     pub age: Option&lt;i32>, }  type ConnectionPool = Pool&lt;Postgres>;  async fn root(headers: HeaderMap, State(pool): State&lt;ConnectionPool>) -> impl IntoResponse {     let jwt_secret = \"mysuperPUPERsecret100500security\";     let validation = Validation::new(Algorithm::HS256);      let auth_header = headers.get(AUTHORIZATION).expect(\"no authorization header\");     let mut auth_hdr: &amp;str = auth_header.to_str().unwrap();     auth_hdr = &amp;auth_hdr.strip_prefix(\"Bearer \").unwrap();      let token = match decode::&lt;Claims>(         &amp;auth_hdr,         &amp;DecodingKey::from_secret(jwt_secret.as_ref()),         &amp;validation,     ) {         Ok(c) => c,         Err(e) => {             println!(\"Application error: {e}\");             return (StatusCode::INTERNAL_SERVER_ERROR, \"invalid token\").into_response();         }     };      let email = token.claims.email;     let query_result: Result&lt;User, sqlx::Error> =         sqlx::query_as(r#\"SELECT *  FROM USERS WHERE email=$1\"#)             .bind(email)             .fetch_one(&amp;pool)             .await;      match query_result {         Ok(user) => {             return (StatusCode::ACCEPTED, Json(user)).into_response();         }         Err(sqlx::Error::RowNotFound) => {             return (StatusCode::NOT_FOUND, \"user not found\").into_response();         }         Err(_e) => {             println!(\"error: {}\", _e.to_string());             return (StatusCode::INTERNAL_SERVER_ERROR, \"error\").into_response();         }     }; }  #[tokio::main] async fn main() -> Result&lt;(), Box&lt;dyn std::error::Error>> {     let database_url = String::from(\"postgres:\/\/postgres:123456@localhost\/testbench\");     let pool = PgPoolOptions::new()         .max_connections(10)         .connect(&amp;database_url)         .await         .expect(\"can't connect to database\");      println!(\"DB connect success\");      let app = Router::new()         .route(\"\/\", get(root))         .with_state(pool);      let listener = tokio::net::TcpListener::bind(\"127.0.0.1:3000\")         .await         .unwrap();     axum::serve(listener, app).await?;     Ok(()) }  \/\/ NOTE ---- \/\/ Rust code has been built in release mode for all performance tests <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0414\u043b\u044f Go \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u0444\u0430\u0439\u043b \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439: <em>go build, <\/em>\u0434\u043b\u044f Rust: <em>cargo build &#8212;release<\/em><\/p>\n<p><strong>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435:<\/strong><\/p>\n<p>\u041f\u041a: Windows 11 22H2, Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz, 32 \u0413\u0431.<\/p>\n<p>Go 1.21.4<\/p>\n<p>Rust 1.74.0<\/p>\n<p>\u0414\u0435\u043b\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u043c 100\u041a \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043f\u0440\u0438 10,50 \u0438 100 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/github.com\/rogerwelin\/cassowary\" rel=\"noopener noreferrer nofollow\">Cassowary<\/a>. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c 10 &#171;\u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445&#187; \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0432 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0438\u0441 \u043d\u0430 Go: <a href=\"http:\/\/127.0.0.1:3000\/randomtoken\" rel=\"noopener noreferrer nofollow\">http:\/\/127.0.0.1:3000\/randomtoken<\/a><\/p>\n<p>\u0412\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u043b\u044e\u0431\u043e\u0439 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b (\u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u0432 \u0441\u0432\u043e\u0439 \u0442\u043e\u043a\u0435\u043d):<\/p>\n<pre><code class=\"bash\">$ cassowary.exe run -u http:\/\/127.0.0.1:3000 -c 10 -n 100000 -H \"Authorization:Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImFsZWphbmRyaW5fZG9sb3JlbXF1ZUBnbWFpbC5jb20iLCJleHAiOjE3MDEzMDYyMTl9.5mT3KVV9Q69yd5gx-z97LVr6tgNA1yVJxpeJEXSq6U0\"<\/code><\/pre>\n<p>\u0414\u0435\u043b\u0430\u0435\u043c \u043f\u043e 3 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0438 \u0431\u0435\u0440\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b Go:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2cb\/c5c\/240\/2cbc5c240af118cc306bc3dbd33c9c15.png\" alt=\"Go 10\" title=\"Go 10\" width=\"761\" height=\"277\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2cb\/c5c\/240\/2cbc5c240af118cc306bc3dbd33c9c15.png\"\/><\/p>\n<div><figcaption>Go 10<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ae8\/77e\/855\/ae877e855d8df82b21ceb51681c1bda7.png\" alt=\"Go 50\" title=\"Go 50\" width=\"768\" height=\"271\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ae8\/77e\/855\/ae877e855d8df82b21ceb51681c1bda7.png\"\/><\/p>\n<div><figcaption>Go 50<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/cbc\/f77\/7d8\/cbcf777d8c9abe5e98da740bf8eb5f4e.png\" alt=\"Go 100\" title=\"Go 100\" width=\"773\" height=\"267\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cbc\/f77\/7d8\/cbcf777d8c9abe5e98da740bf8eb5f4e.png\"\/><\/p>\n<div><figcaption>Go 100<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b Rust:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a7f\/46f\/b96\/a7f46fb968768b63b8f828e7d37672fb.png\" alt=\"Rust 10\" title=\"Rust 10\" width=\"760\" height=\"274\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a7f\/46f\/b96\/a7f46fb968768b63b8f828e7d37672fb.png\"\/><\/p>\n<div><figcaption>Rust 10<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/48e\/dfc\/fd0\/48edfcfd037b902af9a754b12fcec264.png\" alt=\"Rust 50\" title=\"Rust 50\" width=\"770\" height=\"264\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/48e\/dfc\/fd0\/48edfcfd037b902af9a754b12fcec264.png\"\/><\/p>\n<div><figcaption>Rust 50<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/eec\/9fe\/451\/eec9fe451c4037031a2c64b38eb30ae2.png\" alt=\"Rust 100\" title=\"Rust 100\" width=\"770\" height=\"265\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/eec\/9fe\/451\/eec9fe451c4037031a2c64b38eb30ae2.png\"\/><\/p>\n<div><figcaption>Rust 100<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 10,50 \u0438 100 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0432 Go \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f, \u0430 \u0432 Rust \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435\u0442. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 \u0432 \u043f\u0443\u043b\u0435 \u0411\u0414 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043e 10-\u0442\u044c\u044e. \u0422\u0430\u043a \u0431\u044b\u043b\u043e \u0432 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u0438 \u044f \u043d\u0435 \u0441\u0442\u0430\u043b \u044d\u0442\u043e \u043c\u0435\u043d\u044f\u0442\u044c (\u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 \u0432 \u043f\u0443\u043b\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0432 \u0440\u0430\u0437\u044b \u043c\u0435\u043d\u044c\u0448\u0435 \u0447\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432).<\/p>\n<h3>\u0418\u0442\u043e\u0433<\/h3>\n<p>Go \u0431\u044b\u0441\u0442\u0440\u0435\u0435 Rust \u043d\u0430 35% \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438.<\/p>\n<p>\u0412 Rust \u044f \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u043c\u0435\u043d\u044f\u0442\u044c sqlx \u043d\u0430 tokio-postgres, \u0443\u0431\u0438\u0440\u0430\u043b \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u043a\u0443 JWT, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0442\u043e\u0442 \u0436\u0435. <\/p>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 <a href=\"https:\/\/github.com\/ITmind\/RustGoBench\" rel=\"noopener noreferrer nofollow\">Github<\/a> \u0434\u043b\u044f \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043d\u0430 \u0441\u0432\u043e\u0438\u0445 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u0445.<\/p>\n<p><strong>UPD<\/strong><\/p>\n<p>\u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u043d\u0438\u0437\u043a\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 Rust \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 sqlx. \u0417\u0430\u043c\u0435\u043d\u0430 sqlx \u043d\u0430 diesel \u0441\u0440\u0430\u0432\u043d\u044f\u043b\u0430 Go \u0438 Rust. (\u0432 Go \u0442\u043e\u0436\u0435 \u043f\u043e\u043c\u0435\u043d\u044f\u043b pq \u043d\u0430 pgx). \u0410 \u0432\u043e\u0442 \u043f\u0430\u043c\u044f\u0442\u0438 Go \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0432 3 \u0440\u0430\u0437\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u0447\u0435\u043c Rust.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e8f\/f89\/0de\/e8ff890de1174314af92517863485a81.png\" alt=\"Go 50\" title=\"Go 50\" width=\"769\" height=\"272\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e8f\/f89\/0de\/e8ff890de1174314af92517863485a81.png\"\/><\/p>\n<div><figcaption>Go 50<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a45\/e97\/cd8\/a45e97cd8bca6dd69ff596ffb32024a1.png\" alt=\"Rust 50\" title=\"Rust 50\" width=\"762\" height=\"271\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a45\/e97\/cd8\/a45e97cd8bca6dd69ff596ffb32024a1.png\"\/><\/p>\n<div><figcaption>Rust 50<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0412\u044b\u0432\u043e\u0434<\/strong><\/p>\n<p>\u0412 \u043e\u0431\u043e\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0435, \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0434\u043b\u044f Rust \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0438\u0442\u0435\u043b\u044c\u043d\u0435\u0435 ntex, \u0434\u043b\u044f Go \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c gnet (\u043f\u0440\u0430\u0432\u0434\u0430 \u044d\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043c\u0435\u043d\u0430 \u043f\u0430\u043a\u0435\u0442\u0443 net, \u0430 \u043d\u0435 \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0439 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a). \u041d\u0430 \u043e\u0431\u043e\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u043d\u0443\u044e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e, \u043f\u0438\u0441\u0430\u0442\u044c unsafe \u043a\u043e\u0434. \u041d\u0430 Go \u043b\u0435\u0433\u0447\u0435 \u043f\u0438\u0441\u0430\u0442\u044c, \u043e\u0448\u0438\u0431\u043a\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u043f\u043e\u043d\u044f\u0442\u043d\u0435\u0435. Rust \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u0438\u0439, \u043d\u043e \u0447\u0430\u0441\u0442\u043e \u043d\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0438 \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0432\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 generics, \u0437\u0430\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p><strong>\u0411\u043e\u043d\u0443\u0441<\/strong><\/p>\n<p>\u0421\u0434\u0435\u043b\u0430\u043b \u0435\u0449\u0435 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 CPU bounds \u0437\u0430\u0434\u0430\u0447\u0438: \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u043b\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0445\u044d\u0448\u0430 md5 \u0441 \u0447\u0435\u0442\u044b\u0440\u044c\u043c\u044f \u043d\u0443\u043b\u044f\u043c\u0438 \u0432 \u043a\u043e\u043d\u0446\u0435. \u041a\u043e\u0434 \u0434\u043b\u044f Go \u0432\u0437\u044f\u043b \u0438\u0437 <a href=\"https:\/\/habr.com\/ru\/companies\/timeweb\/articles\/770912\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 \u0425\u0430\u0431\u0440\u0435<\/a> (\u043e\u0434\u043d\u043e\u043f\u043e\u0442\u043e\u043a) \u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b, \u0441\u0434\u0435\u043b\u0430\u043b \u043f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438 \u0432 Rust. Rust \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043d\u0430 65% \u0447\u0435\u043c Go<\/p>\n<details class=\"spoiler\">\n<summary>Go<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"go\">package main  import ( \"crypto\/md5\" \"fmt\" \"time\" )  func getHashSync(in string) (string, int) { var salt int for { if h, ok := getHashIf(in, salt); ok { return h, salt } salt++ } }  func getHashIf(in string, salt int) (string, bool) { if x := md5.Sum([]byte(fmt.Sprintf(\"%d.%s\", salt, in))); isHashAcceptable(x) { return fmt.Sprintf(\"%x\", x), true } return \"\", false }  func isHashAcceptable(x [16]byte) bool { return x[15] == 0 &amp;&amp; x[14] == 0 &amp;&amp; x[13] == 0 &amp;&amp; x[12] == 0 }  const phrase = \"\u042d\u0442\u043e \u043f\u0440\u043e\u0432\u0440\u0435\u043a\u0430\"  func main() { fmt.Println(\"Start...\") defer func(t time.Time) { fmt.Printf(\"done with %v\\n\", time.Since(t)) }(time.Now())  var hash, salt = getHashSync(phrase) fmt.Printf(\"%s salt=%d\\n\", hash, salt) } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>Rust<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"rust\">use md5; use std::time::Instant;  fn get_hash_sync(input: &amp;str) -> (String, u32) {     let mut salt: u32 = 0;     loop {         if let Some(hash) = get_hash_if(input, salt) {             return (hash, salt);         }         salt += 1;     } }  fn get_hash_if(input: &amp;str, salt: u32) -> Option&lt;String> {     let digest: md5::Digest = md5::compute(format!(\"{}.{}\", salt, input).as_bytes());      if is_hash_acceptable(digest) {         return Some(format!(\"{:x}\", digest));     }     None }  fn is_hash_acceptable(bytes: md5::Digest) -> bool {     bytes[15] == 0 &amp;&amp; bytes[14] == 0 &amp;&amp; bytes[13] == 0 &amp;&amp; bytes[12] == 0 }  const PHRASE: &amp;str = \"\u042d\u0442\u043e \u043f\u0440\u043e\u0432\u0440\u0435\u043a\u0430\";  fn main() {     println!(\"Start...\");     let start_time = Instant::now();      let (hash, salt) = get_hash_sync(PHRASE);     println!(\"{} salt={}\", hash, salt);      let elapsed: std::time::Duration = start_time.elapsed();     let minutes = elapsed.as_secs() \/ 60;     let seconds = elapsed.as_secs() % 60;     println!(\"done with {:02}m{:02}s\", minutes, seconds);      let mut input = String::new();     let _ = std::io::stdin().read_line(&amp;mut input); } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/777072\/\"> https:\/\/habr.com\/ru\/articles\/777072\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430 <a href=\"http:\/\/medium.com\" rel=\"noopener noreferrer nofollow\">medium.com<\/a> \u0435\u0441\u0442\u044c \u0440\u044f\u0434 \u0441\u0442\u0430\u0442\u0435\u0439 \u0441\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u044b\u0445 web-\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445. \u041e\u0434\u043d\u0430 \u0438\u0437 \u043d\u0438\u0445 <a href=\"https:\/\/medium.com\/deno-the-complete-reference\/go-vs-rust-performance-comparison-for-jwt-verify-and-mysql-query-61aeccac05e8\" rel=\"noopener noreferrer nofollow\">Go vs Rust: Performance comparison for JWT verify and MySQL query<\/a> \u0438 \u0441\u0443\u0434\u044f \u043f\u043e \u043d\u0435\u0439, Go \u043d\u0430 42% \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0447\u0435\u043c Rust. \u042f \u0440\u0435\u0448\u0438\u043b \u043f\u0435\u0440\u0435\u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0438 \u0437\u0430\u043e\u0434\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c Gin \u043d\u0430 Fiber, Axis \u043d\u0430 Axum \u0438 MySQL \u043d\u0430 PostgreSQL.<\/p>\n<p>Web-\u0441\u0435\u0440\u0432\u0438\u0441 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439 \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443 JWT, \u0438\u0441\u043a\u0430\u0442\u044c \u0432 \u0411\u0414 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 \u0434\u0430\u043d\u043d\u044b\u043c email \u0438\u0437 JWT \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u0432\u0438\u0434\u0435 json. \u0422\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u0434\u043e\u0431\u043d\u0430\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0432\u0441\u0435\u043c\u0435\u0441\u0442\u043d\u043e, \u0442\u043e \u0442\u0435\u0441\u0442 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439.<\/p>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u0433\u043e\u0442\u043e\u0432\u0438\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0411\u0414. \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 PostgreSQL \u0438 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0435\u0435 \u0431\u0443\u0434\u0435\u043c \u0432 Docker \u0447\u0435\u0440\u0435\u0437 compose. \u0412 \u043f\u0430\u043f\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0448\u0430 \u0411\u0414, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b init.sql. \u0412 \u043d\u0435\u043c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u0443\u044e \u0431\u0430\u0437\u0443 \u0438 \u0432 \u043d\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 users:<\/p>\n<pre><code class=\"sql\">CREATE DATABASE testbench;  \\connect testbench;  CREATE TABLE users(     email VARCHAR(255) NOT NULL PRIMARY KEY,     first VARCHAR(255),     last VARCHAR(255),     country VARCHAR(255),     city VARCHAR(255),     age int );<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0442\u0430\u043c \u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0430\u043f\u043a\u0443 db \u0438 \u0444\u0430\u0439\u043b docker-compose.yaml \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"yaml\">services:   postgres:     image: postgres:alpine     environment:       - POSTGRES_PASSWORD=123456     volumes:       - .\/db:\/var\/lib\/postgresql\/data       # \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0438\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0431\u0430\u0437\u044b       - .\/init.sql:\/docker-entrypoint-initdb.d\/init.sql     ports:       - \"5432:5432\"<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440:<\/p>\n<pre><code class=\"bash\">$ docker-compose up<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u0430\u0437\u0443 100 000 \u0437\u0430\u043f\u0438\u0441\u044f\u043c\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u0435\u043d \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/www.getsynth.com\/\" rel=\"noopener noreferrer nofollow\">Synth<\/a>. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b \u0441 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u043c\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 (\u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e email \u0443 \u043d\u0430\u0441 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0439 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u044e\u0447):<\/p>\n<pre><code class=\"json\">{     \"type\": \"array\",     \"length\": {         \"type\": \"number\",         \"constant\": 1     },     \"content\": {         \"type\": \"object\",         \"email\": {             \"type\": \"unique\",             \"content\": {                 \"type\": \"string\",                 \"faker\": {                     \"generator\": \"free_email\"                 }             }         },         \"first\": {             \"type\": \"string\",             \"faker\": {                 \"generator\": \"first_name\"             }         },         \"last\": {             \"type\": \"string\",             \"faker\": {                 \"generator\": \"last_name\"             }         },         \"city\": {             \"type\": \"string\",             \"faker\": {                 \"generator\": \"city_name\"             }         },         \"country\": {             \"type\": \"string\",             \"faker\": {                 \"generator\": \"country_name\"             }         },         \"age\": {             \"type\": \"number\",             \"subtype\": \"i32\",             \"range\": {                 \"low\": 18,                 \"high\": 55,                 \"step\": 1             }         }     } }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"bash\">$ synth generate .\/ --to postgres:\/\/postgres:123456@localhost:5432\/testbench --size 100000<\/code><\/pre>\n<p>\u0411\u0414 \u0433\u043e\u0442\u043e\u0432\u0430, \u043f\u0438\u0448\u0435\u043c \u0441\u0430\u043c\u0438 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0438\u0441\u044b. <\/p>\n<details class=\"spoiler\">\n<summary>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430 Go:<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"go\">package main  import ( \"bufio\" \"database\/sql\" \"log\" \"os\" \"strings\" \"time\"  \"github.com\/gofiber\/fiber\/v2\" \"github.com\/golang-jwt\/jwt\/v5\" _ \"github.com\/lib\/pq\" )  type MyCustomClaims struct { Email string `json:\"email\"` jwt.RegisteredClaims }  type User struct { Email  string First  string Last   string City   string Country string Age    int }  var jwtSecret = \"mysuperPUPERsecret100500security\"  func getToken(c *fiber.Ctx) string { hdr := c.Get(\"Authorization\") if hdr == \"\" { return \"\" }  token := strings.Split(hdr, \"Bearer \")[1] return token }  func main() { app := fiber.New() db, err := sql.Open(\"postgres\", \"user=postgres password=123456 dbname=testbench sslmode=disable\")  if err != nil { return }  defer db.Close()  db.SetMaxOpenConns(10) db.SetMaxIdleConns(10)  app.Get(\"\/\", func(c *fiber.Ctx) error { tokenString := getToken(c) if tokenString == \"\" { return c.SendStatus(fiber.StatusUnauthorized) } token, err := jwt.ParseWithClaims(tokenString, &amp;MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) { return []byte(jwtSecret), nil })  if err != nil { log.Println(err) return c.SendStatus(fiber.StatusUnauthorized) }  claims := token.Claims.(*MyCustomClaims)  query := \"SELECT * FROM users WHERE email=$1\" row := db.QueryRow(query, claims.Email)  var user User = User{} err2 := row.Scan(&amp;user.Email, &amp;user.First, &amp;user.Last, &amp;user.Country, &amp;user.City, &amp;user.Age) if err2 == sql.ErrNoRows { return c.SendStatus(fiber.StatusNotFound) } if err2 != nil { log.Println(err2) return c.SendStatus(fiber.StatusInternalServerError) }  return c.JSON(user) })  \/\/\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0440\u0443\u0447\u043a\u0430 app.Get(\"\/randomtoken\", func(c *fiber.Ctx) error { file, err := os.Create(\"tokens.txt\") if err != nil { log.Println(err) return c.SendStatus(fiber.StatusInternalServerError) }  writer := bufio.NewWriter(file)  rows, err := db.Query(\"SELECT * FROM USERS OFFSET floor(random() * 100000) LIMIT 10\") if err != nil { return c.SendStatus(fiber.StatusInternalServerError) }  for rows.Next() { var user User err = rows.Scan(&amp;user.Email, &amp;user.First, &amp;user.Last, &amp;user.Country, &amp;user.City, &amp;user.Age) if err != nil { log.Println(err) return c.SendStatus(fiber.StatusInternalServerError) }  claims := MyCustomClaims{ user.Email, jwt.RegisteredClaims{ ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)), }, }  token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) ss, err := token.SignedString([]byte(jwtSecret))  if err != nil { log.Println(err) return c.SendStatus(fiber.StatusInternalServerError) }  _, err = writer.WriteString(ss + \"\\n\") if err != nil { file.Close() log.Println(err) return c.SendStatus(fiber.StatusInternalServerError) }  }  writer.Flush() file.Close()  return c.SendFile(file.Name()) })  log.Fatal(app.Listen(\":3000\")) } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430 Rust<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"rust\">use axum::{     extract::State,     http::{header::AUTHORIZATION, HeaderMap, StatusCode},     response::IntoResponse,     routing::get,     Json, Router, }; use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; use serde::{Deserialize, Serialize}; use sqlx::{postgres::PgPoolOptions, Pool, Postgres};  #[derive(Debug, Serialize, Deserialize)] struct Claims {     email: String,     exp: usize, }  #[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] pub struct User {     pub email: String,     pub first: Option&lt;String>,     pub last: Option&lt;String>,     pub city: Option&lt;String>,     pub country: Option&lt;String>,     pub age: Option&lt;i32>, }  type ConnectionPool = Pool&lt;Postgres>;  async fn root(headers: HeaderMap, State(pool): State&lt;ConnectionPool>) -> impl IntoResponse {     let jwt_secret = \"mysuperPUPERsecret100500security\";     let validation = Validation::new(Algorithm::HS256);      let auth_header = headers.get(AUTHORIZATION).expect(\"no authorization header\");     let mut auth_hdr: &amp;str = auth_header.to_str().unwrap();     auth_hdr = &amp;auth_hdr.strip_prefix(\"Bearer \").unwrap();      let token = match decode::&lt;Claims>(         &amp;auth_hdr,         &amp;DecodingKey::from_secret(jwt_secret.as_ref()),         &amp;validation,     ) {         Ok(c) => c,         Err(e) => {             println!(\"Application error: {e}\");             return (StatusCode::INTERNAL_SERVER_ERROR, \"invalid token\").into_response();         }     };      let email = token.claims.email;     let query_result: Result&lt;User, sqlx::Error> =         sqlx::query_as(r#\"SELECT *  FROM USERS WHERE email=$1\"#)             .bind(email)             .fetch_one(&amp;pool)             .await;      match query_result {         Ok(user) => {             return (StatusCode::ACCEPTED, Json(user)).into_response();         }         Err(sqlx::Error::RowNotFound) => {             return (StatusCode::NOT_FOUND, \"user not found\").into_response();         }         Err(_e) => {             println!(\"error: {}\", _e.to_string());             return (StatusCode::INTERNAL_SERVER_ERROR, \"error\").into_response();         }     }; }  #[tokio::main] async fn main() -> Result&lt;(), Box&lt;dyn std::error::Error>> {     let database_url = String::from(\"postgres:\/\/postgres:123456@localhost\/testbench\");     let pool = PgPoolOptions::new()         .max_connections(10)         .connect(&amp;database_url)         .await         .expect(\"can't connect to database\");      println!(\"DB connect success\");      let app = Router::new()         .route(\"\/\", get(root))         .with_state(pool);      let listener = tokio::net::TcpListener::bind(\"127.0.0.1:3000\")         .await         .unwrap();     axum::serve(listener, app).await?;     Ok(()) }  \/\/ NOTE ---- \/\/ Rust code has been built in release mode for all performance tests <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0414\u043b\u044f Go \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u0444\u0430\u0439\u043b \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439: <em>go build, <\/em>\u0434\u043b\u044f Rust: <em>cargo build &#8212;release<\/em><\/p>\n<p><strong>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435:<\/strong><\/p>\n<p>\u041f\u041a: Windows 11 22H2, Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz, 32 \u0413\u0431.<\/p>\n<p>Go 1.21.4<\/p>\n<p>Rust 1.74.0<\/p>\n<p>\u0414\u0435\u043b\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u043c 100\u041a \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043f\u0440\u0438 10,50 \u0438 100 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <a href=\"https:\/\/github.com\/rogerwelin\/cassowary\" rel=\"noopener noreferrer nofollow\">Cassowary<\/a>. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c 10 &#171;\u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445&#187; \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0432 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0438\u0441 \u043d\u0430 Go: <a href=\"http:\/\/127.0.0.1:3000\/randomtoken\" rel=\"noopener noreferrer nofollow\">http:\/\/127.0.0.1:3000\/randomtoken<\/a><\/p>\n<p>\u0412\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u043b\u044e\u0431\u043e\u0439 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b (\u043f\u043e\u0434\u0441\u0442\u0430\u0432\u0438\u0432 \u0441\u0432\u043e\u0439 \u0442\u043e\u043a\u0435\u043d):<\/p>\n<pre><code class=\"bash\">$ cassowary.exe run -u http:\/\/127.0.0.1:3000 -c 10 -n 100000 -H \"Authorization:Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImFsZWphbmRyaW5fZG9sb3JlbXF1ZUBnbWFpbC5jb20iLCJleHAiOjE3MDEzMDYyMTl9.5mT3KVV9Q69yd5gx-z97LVr6tgNA1yVJxpeJEXSq6U0\"<\/code><\/pre>\n<p>\u0414\u0435\u043b\u0430\u0435\u043c \u043f\u043e 3 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0438 \u0431\u0435\u0440\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b Go:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>Go 10<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\">\n<div><figcaption>Go 50<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\">\n<div><figcaption>Go 100<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b Rust:<\/p>\n<figure class=\"full-width\">\n<div><figcaption>Rust 10<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\">\n<div><figcaption>Rust 50<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\">\n<div><figcaption>Rust 100<\/figcaption><\/div>\n<\/figure>\n<p>\u0420\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 10,50 \u0438 100 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0432 Go \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f, \u0430 \u0432 Rust \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435\u0442. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 \u0432 \u043f\u0443\u043b\u0435 \u0411\u0414 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043e 10-\u0442\u044c\u044e. \u0422\u0430\u043a \u0431\u044b\u043b\u043e \u0432 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u0438 \u044f \u043d\u0435 \u0441\u0442\u0430\u043b \u044d\u0442\u043e \u043c\u0435\u043d\u044f\u0442\u044c (\u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 \u0432 \u043f\u0443\u043b\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0432 \u0440\u0430\u0437\u044b \u043c\u0435\u043d\u044c\u0448\u0435 \u0447\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432).<\/p>\n<h3>\u0418\u0442\u043e\u0433<\/h3>\n<p>Go \u0431\u044b\u0441\u0442\u0440\u0435\u0435 Rust \u043d\u0430 35% \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438.<\/p>\n<p>\u0412 Rust \u044f \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u043c\u0435\u043d\u044f\u0442\u044c sqlx \u043d\u0430 tokio-postgres, \u0443\u0431\u0438\u0440\u0430\u043b \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u043a\u0443 JWT, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0442\u043e\u0442 \u0436\u0435. <\/p>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 <a href=\"https:\/\/github.com\/ITmind\/RustGoBench\" rel=\"noopener noreferrer nofollow\">Github<\/a> \u0434\u043b\u044f \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043d\u0430 \u0441\u0432\u043e\u0438\u0445 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u0445.<\/p>\n<p><strong>UPD<\/strong><\/p>\n<p>\u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u043d\u0438\u0437\u043a\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 Rust \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 sqlx. \u0417\u0430\u043c\u0435\u043d\u0430 sqlx \u043d\u0430 diesel \u0441\u0440\u0430\u0432\u043d\u044f\u043b\u0430 Go \u0438 Rust. (\u0432 Go \u0442\u043e\u0436\u0435 \u043f\u043e\u043c\u0435\u043d\u044f\u043b pq \u043d\u0430 pgx). \u0410 \u0432\u043e\u0442 \u043f\u0430\u043c\u044f\u0442\u0438 Go \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0432 3 \u0440\u0430\u0437\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u0447\u0435\u043c Rust.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>Go 50<\/figcaption><\/div>\n<\/figure>\n<figure class=\"full-width\">\n<div><figcaption>Rust 50<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0412\u044b\u0432\u043e\u0434<\/strong><\/p>\n<p>\u0412 \u043e\u0431\u043e\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-361482","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/361482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=361482"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/361482\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=361482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=361482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=361482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}