Browse Source

actix: optimize json and plaintext benchmarks (#4789)

Nikolay Kim 6 years ago
parent
commit
96f40163f4

+ 0 - 1
frameworks/Rust/actix/src/db_pg_direct.rs

@@ -126,7 +126,6 @@ impl PgConnection {
         }
 
         let mut cl = self.cl.clone();
-        let mut rng = self.rng.clone();
         stream::futures_unordered(worlds)
             .collect()
             .and_then(move |worlds| {

+ 10 - 15
frameworks/Rust/actix/src/main_platform.rs

@@ -27,10 +27,10 @@ use crate::db_pg_direct::PgConnection;
 use crate::utils::{FortunesTemplate, Writer};
 
 struct App {
+    db: PgConnection,
     hdr_srv: HeaderValue,
     hdr_ctjson: HeaderValue,
     hdr_cthtml: HeaderValue,
-    db: PgConnection,
 }
 
 impl Service for App {
@@ -46,13 +46,12 @@ impl Service for App {
 
     fn call(&mut self, req: Request) -> Self::Future {
         let path = req.path();
-        match path.len() {
-            3 if path == "/db" => {
-                let fut = self.db.get_world();
+        match path {
+            "/db" => {
                 let h_srv = self.hdr_srv.clone();
                 let h_ct = self.hdr_ctjson.clone();
 
-                Box::new(fut.map(move |body| {
+                Box::new(self.db.get_world().map(move |body| {
                     let mut res = Response::with_body(StatusCode::OK, Body::Bytes(body));
                     let hdrs = res.headers_mut();
                     hdrs.insert(SERVER, h_srv);
@@ -60,12 +59,11 @@ impl Service for App {
                     res
                 }))
             }
-            8 if path == "/fortune" => {
-                let fut = self.db.tell_fortune();
+            "/fortune" => {
                 let h_srv = self.hdr_srv.clone();
                 let h_ct = self.hdr_cthtml.clone();
 
-                Box::new(fut.from_err().map(move |fortunes| {
+                Box::new(self.db.tell_fortune().from_err().map(move |fortunes| {
                     let mut body = BytesMut::with_capacity(2048);
                     let mut writer = Writer(&mut body);
                     let _ = write!(writer, "{}", FortunesTemplate { fortunes });
@@ -77,13 +75,12 @@ impl Service for App {
                     res
                 }))
             }
-            8 if path == "/queries" => {
+            "/queries" => {
                 let q = utils::get_query_param(req.uri().query().unwrap_or("")) as usize;
-                let fut = self.db.get_worlds(q);
                 let h_srv = self.hdr_srv.clone();
                 let h_ct = self.hdr_ctjson.clone();
 
-                Box::new(fut.from_err().map(move |worlds| {
+                Box::new(self.db.get_worlds(q).from_err().map(move |worlds| {
                     let mut body = BytesMut::with_capacity(35 * worlds.len());
                     to_writer(Writer(&mut body), &worlds).unwrap();
                     let mut res =
@@ -94,13 +91,12 @@ impl Service for App {
                     res
                 }))
             }
-            8 if path == "/updates" => {
+            "/updates" => {
                 let q = utils::get_query_param(req.uri().query().unwrap_or("")) as usize;
-                let fut = self.db.update(q);
                 let h_srv = self.hdr_srv.clone();
                 let h_ct = self.hdr_ctjson.clone();
 
-                Box::new(fut.from_err().map(move |worlds| {
+                Box::new(self.db.update(q).from_err().map(move |worlds| {
                     let mut body = BytesMut::with_capacity(35 * worlds.len());
                     to_writer(Writer(&mut body), &worlds).unwrap();
                     let mut res =
@@ -144,7 +140,6 @@ impl NewService for AppFactory {
 fn main() -> std::io::Result<()> {
     let sys = actix_rt::System::builder().stop_on_panic(false).build();
 
-    // start http server
     Server::build()
         .backlog(1024)
         .bind("techempower", "0.0.0.0:8080", || {

+ 22 - 27
frameworks/Rust/actix/src/main_raw.rs

@@ -6,7 +6,7 @@ extern crate serde_derive;
 #[macro_use]
 extern crate diesel;
 
-use std::{io, io::Write};
+use std::io;
 
 use actix_codec::{AsyncRead, AsyncWrite, Decoder};
 use actix_http::h1;
@@ -28,7 +28,8 @@ const HTTPNFOUND: &[u8] = b"HTTP/1.1 400 OK\r\n";
 const HDR_SERVER: &[u8] = b"Server: Actix\r\n";
 const HDR_CTPLAIN: &[u8] = b"Content-Type: text/plain\r\n";
 const HDR_CTJSON: &[u8] = b"Content-Type: application/json\r\n";
-const HDR_CLEN: &[u8] = b"Content-Length: ";
+const HDR_PL_LEN: &[u8] = b"Content-Length: 13\r\n";
+const HDR_JS_LEN: &[u8] = b"Content-Length: 27\r\n";
 const BODY: &[u8] = b"Hello, World!";
 
 struct App<T> {
@@ -41,34 +42,29 @@ struct App<T> {
 impl<T: AsyncRead + AsyncWrite> App<T> {
     fn handle_request(&mut self, req: Request) {
         let path = req.path();
-        match path.len() {
-            10 if path == "/plaintext" => {
-                self.write_buf.extend_from_slice(HTTPOK);
-                self.write_buf.extend_from_slice(HDR_SERVER);
-                self.write_buf.extend_from_slice(HDR_CTPLAIN);
-                self.write_buf.extend_from_slice(HDR_CLEN);
-                let _ = write!(Writer(&mut self.write_buf), "{}\r\n", 13);
-                self.codec.config().set_date(&mut self.write_buf);
-                self.write_buf.extend_from_slice(BODY);
-            }
-            5 if path == "/json" => {
+        match path {
+            "/json" => {
                 let message = Message {
                     message: "Hello, World!",
                 };
-
-                self.write_buf.extend_from_slice(HTTPOK);
-                self.write_buf.extend_from_slice(HDR_SERVER);
-                self.write_buf.extend_from_slice(HDR_CTJSON);
-                self.write_buf.extend_from_slice(HDR_CLEN);
-                let _ = write!(Writer(&mut self.write_buf), "{}\r\n", 27);
+                self.write_buf.put_slice(HTTPOK);
+                self.write_buf.put_slice(HDR_SERVER);
+                self.write_buf.put_slice(HDR_CTJSON);
+                self.write_buf.put_slice(HDR_JS_LEN);
                 self.codec.config().set_date(&mut self.write_buf);
                 to_writer(Writer(&mut self.write_buf), &message).unwrap();
             }
+            "/plaintext" => {
+                self.write_buf.put_slice(HTTPOK);
+                self.write_buf.put_slice(HDR_SERVER);
+                self.write_buf.put_slice(HDR_CTPLAIN);
+                self.write_buf.put_slice(HDR_PL_LEN);
+                self.codec.config().set_date(&mut self.write_buf);
+                self.write_buf.put_slice(BODY);
+            }
             _ => {
-                self.write_buf.extend_from_slice(HTTPNFOUND);
-                self.write_buf.extend_from_slice(HDR_SERVER);
-                self.write_buf.extend_from_slice(HDR_CLEN);
-                let _ = write!(Writer(&mut self.write_buf), "0\r\n");
+                self.write_buf.put_slice(HTTPNFOUND);
+                self.write_buf.put_slice(HDR_SERVER);
             }
         }
     }
@@ -79,10 +75,6 @@ impl<T: AsyncRead + AsyncWrite> Future for App<T> {
     type Error = ();
 
     fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
-        if self.write_buf.remaining_mut() < 1024 {
-            self.write_buf.reserve(32_768);
-        }
-
         loop {
             if self.read_buf.remaining_mut() < 4096 {
                 self.read_buf.reserve(32_768);
@@ -102,6 +94,9 @@ impl<T: AsyncRead + AsyncWrite> Future for App<T> {
         }
 
         loop {
+            if self.write_buf.remaining_mut() < 1024 {
+                self.write_buf.reserve(32_768);
+            }
             match self.codec.decode(&mut self.read_buf) {
                 Ok(Some(h1::Message::Item(req))) => self.handle_request(req),
                 Ok(None) => break,