Przeglądaj źródła

actix: revert last change (#5577)

* Revert "actix: reduce mem allocations (#5564)"

This reverts commit 24d0f626d6c3de35d38982872ee988e1761417ea.

* upgrade rustc

* trigger build

* let's play
Nikolay Kim 5 lat temu
rodzic
commit
687dd71654

+ 1 - 1
frameworks/Rust/actix/actix-core.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.41
+FROM rust:1.42
 
 
 RUN apt update -yqq && apt install -yqq cmake g++
 RUN apt update -yqq && apt install -yqq cmake g++
 
 

+ 1 - 1
frameworks/Rust/actix/actix-diesel.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.41
+FROM rust:1.42
 
 
 RUN apt update -yqq && apt install -yqq cmake g++
 RUN apt update -yqq && apt install -yqq cmake g++
 
 

+ 1 - 1
frameworks/Rust/actix/actix-pg.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.40
+FROM rust:1.42
 
 
 RUN apt update -yqq && apt install -yqq cmake g++
 RUN apt update -yqq && apt install -yqq cmake g++
 
 

+ 1 - 1
frameworks/Rust/actix/actix-raw.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.41
+FROM rust:1.42
 
 
 RUN apt update -yqq && apt install -yqq cmake g++
 RUN apt update -yqq && apt install -yqq cmake g++
 
 

+ 1 - 1
frameworks/Rust/actix/actix.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.41
+FROM rust:1.42
 
 
 RUN apt update -yqq && apt install -yqq cmake g++
 RUN apt update -yqq && apt install -yqq cmake g++
 
 

+ 6 - 6
frameworks/Rust/actix/benchmark_config.json

@@ -2,8 +2,8 @@
   "framework": "actix",
   "framework": "actix",
   "tests": [{
   "tests": [{
     "default": {
     "default": {
-      "json_url": "/json",
-      "plaintext_url": "/plaintext",
+      "json_url": "/j",
+      "plaintext_url": "/p",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Micro",
       "classification": "Micro",
@@ -20,8 +20,8 @@
       "versus": ""
       "versus": ""
     },
     },
     "core": {
     "core": {
-      "fortune_url": "/fortune",
-      "db_url": "/db",
+      "fortune_url": "/f",
+      "db_url": "/d",
       "query_url": "/queries?q=",
       "query_url": "/queries?q=",
       "update_url": "/updates?q=",
       "update_url": "/updates?q=",
       "port": 8080,
       "port": 8080,
@@ -40,8 +40,8 @@
       "versus": ""
       "versus": ""
     },
     },
     "raw": {
     "raw": {
-      "json_url": "/json",
-      "plaintext_url": "/plaintext",
+      "json_url": "/j",
+      "plaintext_url": "/p",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Platform",
       "classification": "Platform",

+ 10 - 55
frameworks/Rust/actix/src/db_pg_direct.rs

@@ -1,6 +1,6 @@
 use std::collections::HashMap;
 use std::collections::HashMap;
-use std::fmt::Write as FmtWrite;
-use std::io::{self, Write};
+use std::fmt::Write;
+use std::io;
 
 
 use actix_http::Error;
 use actix_http::Error;
 use bytes::{Bytes, BytesMut};
 use bytes::{Bytes, BytesMut};
@@ -8,54 +8,11 @@ use futures::stream::futures_unordered::FuturesUnordered;
 use futures::{Future, FutureExt, StreamExt, TryStreamExt};
 use futures::{Future, FutureExt, StreamExt, TryStreamExt};
 use rand::rngs::SmallRng;
 use rand::rngs::SmallRng;
 use rand::{Rng, SeedableRng};
 use rand::{Rng, SeedableRng};
-use tokio_postgres::row::Row;
 use tokio_postgres::types::ToSql;
 use tokio_postgres::types::ToSql;
 use tokio_postgres::{connect, Client, NoTls, Statement};
 use tokio_postgres::{connect, Client, NoTls, Statement};
 
 
 use crate::models::World;
 use crate::models::World;
-use crate::utils::Writer;
-
-pub enum Message {
-    Row(Row),
-    Str(&'static str),
-}
-
-impl Message {
-    fn as_str(&self) -> &str {
-        match self {
-            Message::Row(ref row) => row.get(1),
-            Message::Str(s) => s,
-        }
-    }
-}
-
-#[allow(non_snake_case)]
-pub struct Fortune {
-    pub id: i32,
-    pub message: Message,
-}
-
-markup::define! {
-    FortunesTemplate(items: Vec<Fortune>) {
-        {markup::doctype()}
-        html {
-            head {
-                title { "Fortunes" }
-            }
-            body {
-                table {
-                    tr { th { "id" } th { "message" } }
-                    @for item in {items} {
-                        tr {
-                            td { {item.id} }
-                            td { {markup::raw(v_htmlescape::escape(item.message.as_str()))} }
-                        }
-                    }
-                }
-            }
-        }
-    }
-}
+use crate::utils::{Fortune, Writer};
 
 
 /// Postgres interface
 /// Postgres interface
 pub struct PgConnection {
 pub struct PgConnection {
@@ -200,10 +157,12 @@ impl PgConnection {
         }
         }
     }
     }
 
 
-    pub fn tell_fortune(&mut self) -> impl Future<Output = Result<Bytes, io::Error>> {
+    pub fn tell_fortune(
+        &mut self,
+    ) -> impl Future<Output = Result<Vec<Fortune>, io::Error>> {
         let mut items = vec![Fortune {
         let mut items = vec![Fortune {
             id: 0,
             id: 0,
-            message: Message::Str("Additional fortune added at request time."),
+            message: "Additional fortune added at request time.".to_string(),
         }];
         }];
 
 
         let fut = self.cl.query_raw(&self.fortune, &[]);
         let fut = self.cl.query_raw(&self.fortune, &[]);
@@ -219,16 +178,12 @@ impl PgConnection {
                 })?;
                 })?;
                 items.push(Fortune {
                 items.push(Fortune {
                     id: row.get(0),
                     id: row.get(0),
-                    message: Message::Row(row),
+                    message: row.get(1),
                 });
                 });
             }
             }
-            items.sort_by(|it, next| it.message.as_str().cmp(next.message.as_str()));
 
 
-            let mut body = BytesMut::with_capacity(2048);
-            let mut writer = Writer(&mut body);
-            let _ = write!(writer, "{}", FortunesTemplate { items });
-
-            Ok(body.freeze())
+            items.sort_by(|it, next| it.message.cmp(&next.message));
+            Ok(items)
         }
         }
     }
     }
 }
 }

+ 4 - 4
frameworks/Rust/actix/src/main.rs

@@ -24,7 +24,7 @@ async fn json() -> HttpResponse {
 
 
     let mut res = HttpResponse::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
     let mut res = HttpResponse::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
     res.headers_mut()
     res.headers_mut()
-        .insert(SERVER, HeaderValue::from_static("Actix"));
+        .insert(SERVER, HeaderValue::from_static("A"));
     res.headers_mut()
     res.headers_mut()
         .insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
         .insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
     res
     res
@@ -36,7 +36,7 @@ async fn plaintext() -> HttpResponse {
         Body::Bytes(Bytes::from_static(b"Hello, World!")),
         Body::Bytes(Bytes::from_static(b"Hello, World!")),
     );
     );
     res.headers_mut()
     res.headers_mut()
-        .insert(SERVER, HeaderValue::from_static("Actix"));
+        .insert(SERVER, HeaderValue::from_static("A"));
     res.headers_mut()
     res.headers_mut()
         .insert(CONTENT_TYPE, HeaderValue::from_static("text/plain"));
         .insert(CONTENT_TYPE, HeaderValue::from_static("text/plain"));
     res
     res
@@ -55,8 +55,8 @@ async fn main() -> std::io::Result<()> {
                 .client_timeout(0)
                 .client_timeout(0)
                 .h1(map_config(
                 .h1(map_config(
                     App::new()
                     App::new()
-                        .service(web::resource("/json").to(json))
-                        .service(web::resource("/plaintext").to(plaintext)),
+                        .service(web::resource("/j").to(json))
+                        .service(web::resource("/p").to(plaintext)),
                     |_| AppConfig::default(),
                     |_| AppConfig::default(),
                 ))
                 ))
                 .tcp()
                 .tcp()

+ 16 - 12
frameworks/Rust/actix/src/main_platform.rs

@@ -7,6 +7,7 @@ extern crate serde_derive;
 extern crate diesel;
 extern crate diesel;
 
 
 use std::future::Future;
 use std::future::Future;
+use std::io::Write;
 use std::pin::Pin;
 use std::pin::Pin;
 use std::task::{Context, Poll};
 use std::task::{Context, Poll};
 
 
@@ -25,7 +26,7 @@ mod models;
 mod utils;
 mod utils;
 
 
 use crate::db_pg_direct::PgConnection;
 use crate::db_pg_direct::PgConnection;
-use crate::utils::Writer;
+use crate::utils::{FortunesTemplate, Writer};
 
 
 struct App {
 struct App {
     db: PgConnection,
     db: PgConnection,
@@ -48,29 +49,32 @@ impl Service for App {
     fn call(&mut self, req: Request) -> Self::Future {
     fn call(&mut self, req: Request) -> Self::Future {
         let path = req.path();
         let path = req.path();
         match path {
         match path {
-            "/fortune" => {
+            "/d" => {
                 let h_srv = self.hdr_srv.clone();
                 let h_srv = self.hdr_srv.clone();
-                let h_ct = self.hdr_cthtml.clone();
-                let fut = self.db.tell_fortune();
+                let h_ct = self.hdr_ctjson.clone();
+                let fut = self.db.get_world();
 
 
                 Box::pin(async move {
                 Box::pin(async move {
-                    let fortunes = fut.await?;
-                    let mut res =
-                        Response::with_body(StatusCode::OK, Body::Bytes(fortunes));
+                    let body = fut.await?;
+                    let mut res = Response::with_body(StatusCode::OK, Body::Bytes(body));
                     let hdrs = res.headers_mut();
                     let hdrs = res.headers_mut();
                     hdrs.insert(SERVER, h_srv);
                     hdrs.insert(SERVER, h_srv);
                     hdrs.insert(CONTENT_TYPE, h_ct);
                     hdrs.insert(CONTENT_TYPE, h_ct);
                     Ok(res)
                     Ok(res)
                 })
                 })
             }
             }
-            "/db" => {
+            "/f" => {
                 let h_srv = self.hdr_srv.clone();
                 let h_srv = self.hdr_srv.clone();
-                let h_ct = self.hdr_ctjson.clone();
-                let fut = self.db.get_world();
+                let h_ct = self.hdr_cthtml.clone();
+                let fut = self.db.tell_fortune();
 
 
                 Box::pin(async move {
                 Box::pin(async move {
-                    let body = fut.await?;
-                    let mut res = Response::with_body(StatusCode::OK, Body::Bytes(body));
+                    let fortunes = fut.await?;
+                    let mut body = BytesMut::with_capacity(2048);
+                    let mut writer = Writer(&mut body);
+                    let _ = write!(writer, "{}", FortunesTemplate { fortunes });
+                    let mut res =
+                        Response::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
                     let hdrs = res.headers_mut();
                     let hdrs = res.headers_mut();
                     hdrs.insert(SERVER, h_srv);
                     hdrs.insert(SERVER, h_srv);
                     hdrs.insert(CONTENT_TYPE, h_ct);
                     hdrs.insert(CONTENT_TYPE, h_ct);

+ 5 - 5
frameworks/Rust/actix/src/main_raw.rs

@@ -24,10 +24,10 @@ mod utils;
 
 
 use crate::utils::{Message, Writer};
 use crate::utils::{Message, Writer};
 
 
-const JSON: &[u8] = b"HTTP/1.1 200 OK\r\nServer: Actix\r\nContent-Type: application/json\r\nContent-Length: 27\r\n";
-const PLAIN: &[u8] = b"HTTP/1.1 200 OK\r\nServer: Actix\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n";
+const JSON: &[u8] = b"HTTP/1.1 200 OK\r\nServer: A\r\nContent-Type: application/json\r\nContent-Length: 27\r\n";
+const PLAIN: &[u8] = b"HTTP/1.1 200 OK\r\nServer: A\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n";
 const HTTPNFOUND: &[u8] = b"HTTP/1.1 400 OK\r\n";
 const HTTPNFOUND: &[u8] = b"HTTP/1.1 400 OK\r\n";
-const HDR_SERVER: &[u8] = b"Server: Actix\r\n";
+const HDR_SERVER: &[u8] = b"Server: A\r\n";
 const BODY: &[u8] = b"Hello, World!";
 const BODY: &[u8] = b"Hello, World!";
 
 
 struct App {
 struct App {
@@ -40,7 +40,7 @@ struct App {
 impl App {
 impl App {
     fn handle_request(&mut self, req: Request) {
     fn handle_request(&mut self, req: Request) {
         match req.path() {
         match req.path() {
-            "/json" => {
+            "/j" => {
                 let message = Message {
                 let message = Message {
                     message: "Hello, World!",
                     message: "Hello, World!",
                 };
                 };
@@ -48,7 +48,7 @@ impl App {
                 self.codec.config().set_date(&mut self.write_buf);
                 self.codec.config().set_date(&mut self.write_buf);
                 to_writer(Writer(&mut self.write_buf), &message).unwrap();
                 to_writer(Writer(&mut self.write_buf), &message).unwrap();
             }
             }
-            "/plaintext" => {
+            "/p" => {
                 self.write_buf.put_slice(PLAIN);
                 self.write_buf.put_slice(PLAIN);
                 self.codec.config().set_date(&mut self.write_buf);
                 self.codec.config().set_date(&mut self.write_buf);
                 self.write_buf.put_slice(BODY);
                 self.write_buf.put_slice(BODY);