Pārlūkot izejas kodu

actix: update json benchmarks (#5613)

* actix: use simd-json for json serialization

* use simd-json Serialize trait

* use crates version
Nikolay Kim 5 gadi atpakaļ
vecāks
revīzija
0673f953cc

+ 2 - 0
frameworks/Rust/actix/Cargo.toml

@@ -44,6 +44,8 @@ bytes = "0.5.3"
 num_cpus = "1.0"
 futures = "0.3.1"
 http = "0.2"
+simd-json = "0.3"
+simd-json-derive = { git = "https://github.com/simd-lite/simd-json-derive.git" }
 diesel = { version = "1.4.3", features = ["postgres"] }
 url = { version="1.7", features=["query_encoding"] }
 log = { version = "0.4", features = ["release_max_level_debug"] }

+ 8 - 2
frameworks/Rust/actix/src/main.rs

@@ -11,16 +11,22 @@ use actix_web::http::header::{CONTENT_TYPE, SERVER};
 use actix_web::http::{HeaderValue, StatusCode};
 use actix_web::{web, App, HttpResponse};
 use bytes::{Bytes, BytesMut};
+use simd_json_derive::Serialize;
 
 mod utils;
-use utils::{Message, Writer, SIZE};
+use utils::{Writer, SIZE};
+
+#[derive(Serialize)]
+pub struct Message {
+    pub message: &'static str,
+}
 
 async fn json() -> HttpResponse {
     let message = Message {
         message: "Hello, World!",
     };
     let mut body = BytesMut::with_capacity(SIZE);
-    serde_json::to_writer(Writer(&mut body), &message).unwrap();
+    message.json_write(&mut Writer(&mut body)).unwrap();
 
     let mut res = HttpResponse::with_body(StatusCode::OK, Body::Bytes(body.freeze()));
     res.headers_mut()

+ 10 - 3
frameworks/Rust/actix/src/main_raw.rs

@@ -17,12 +17,12 @@ use actix_rt::net::TcpStream;
 use actix_server::Server;
 use actix_service::fn_service;
 use bytes::{Buf, BufMut, BytesMut};
-use serde_json::to_writer;
+use simd_json_derive::Serialize;
 
 mod models;
 mod utils;
 
-use crate::utils::{Message, Writer};
+use crate::utils::Writer;
 
 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";
@@ -30,6 +30,11 @@ const HTTPNFOUND: &[u8] = b"HTTP/1.1 400 OK\r\n";
 const HDR_SERVER: &[u8] = b"Server: A\r\n";
 const BODY: &[u8] = b"Hello, World!";
 
+#[derive(Serialize)]
+pub struct Message {
+    pub message: &'static str,
+}
+
 struct App {
     io: TcpStream,
     read_buf: BytesMut,
@@ -46,7 +51,9 @@ impl App {
                 };
                 self.write_buf.put_slice(JSON);
                 self.codec.config().set_date(&mut self.write_buf);
-                to_writer(Writer(&mut self.write_buf), &message).unwrap();
+                message
+                    .json_write(&mut Writer(&mut self.write_buf))
+                    .unwrap();
             }
             "/p" => {
                 self.write_buf.put_slice(PLAIN);