Created
May 15, 2020 19:04
-
-
Save insanitybit/5b327d49fd53e62019eaa2c1b787e1fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "bench-translate" | |
version = "0.1.0" | |
authors = ["colin <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
serde_json = "1.0.53" | |
bincode = "1.2.1" | |
serde = { version = "1.0", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(test)] | |
extern crate test; | |
use test::Bencher; | |
use std::fmt::Debug; | |
use serde::{Serialize, Deserialize}; | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct User { | |
id: String, | |
login: String, | |
email: String, | |
company: String, | |
} | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct TeamMembers { | |
role: String, | |
user: User, | |
} | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct PageInfo { | |
end_cursor: String, | |
has_next_page: bool, | |
} | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct TeamMembersEdges { | |
page_info: PageInfo, | |
team_members_edges: Vec<TeamMembers> | |
} | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct Team { | |
id: String, | |
name: String, | |
team_members_edges: Vec<TeamMembersEdges> | |
} | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct Organization { | |
teams: Vec<Team> | |
} | |
// Copies | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct UserCopy { | |
id: String, | |
login: String, | |
email: String, | |
company: String, | |
} | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct TeamMembersCopy { | |
role: String, | |
user: UserCopy, | |
} | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct PageInfoCopy { | |
end_cursor: String, | |
has_next_page: bool, | |
} | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct TeamMembersEdgesCopy { | |
page_info: PageInfoCopy, | |
team_members_edges: Vec<TeamMembersCopy> | |
} | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct TeamCopy { | |
id: String, | |
name: String, | |
team_members_edges: Vec<TeamMembersEdgesCopy> | |
} | |
#[derive(Clone, Debug, Serialize, Deserialize)] | |
pub struct OrganizationCopy { | |
teams: Vec<TeamCopy> | |
} | |
pub fn organization() -> Organization { | |
Organization { | |
teams: vec![Team { | |
id: "aoiefvaeor==".to_string(), | |
name: "my-team".to_string(), | |
team_members_edges: vec![ | |
TeamMembersEdges { | |
page_info: PageInfo { | |
end_cursor: "aoerivjaeorvaerv==".to_string(), | |
has_next_page: true | |
}, | |
team_members_edges: vec![ | |
TeamMembers { | |
role: "MEMBER".to_string(), | |
user: User { | |
id: "aeoirvjaerv==".to_string(), | |
login: "fiiaoeiv".to_string(), | |
email: "[email protected]".to_string(), | |
company: "beepbop".to_string() | |
} | |
}; | |
100 | |
] | |
} | |
] | |
}; 100] | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
use std::hint::black_box; | |
#[bench] | |
fn bench_clone(b: &mut Bencher) { | |
let organization = black_box(organization()); | |
b.iter(|| { | |
black_box(organization.clone()); | |
}); | |
} | |
#[bench] | |
fn bench_serde_value(b: &mut Bencher) { | |
let organization = black_box(organization()); | |
b.iter(|| { | |
let s = serde_json::to_value(&organization).unwrap(); | |
let s: OrganizationCopy = serde_json::from_value(s).unwrap(); | |
black_box(s); | |
}); | |
} | |
#[bench] | |
fn bench_serde_slice(b: &mut Bencher) { | |
let organization = black_box(organization()); | |
b.iter(|| { | |
let s = serde_json::to_vec(&organization).unwrap(); | |
let s: OrganizationCopy = serde_json::from_slice(&s).unwrap(); | |
black_box(s); | |
}); | |
} | |
#[bench] | |
fn bench_serde_str(b: &mut Bencher) { | |
let organization = black_box(organization()); | |
b.iter(|| { | |
let s = serde_json::to_string(&organization).unwrap(); | |
let s: OrganizationCopy = serde_json::from_str(&s).unwrap(); | |
black_box(s); | |
}); | |
} | |
#[bench] | |
fn bench_bincode(b: &mut Bencher) { | |
let organization = black_box(organization()); | |
b.iter(|| { | |
let s = bincode::serialize(&organization).unwrap(); | |
let s: OrganizationCopy = bincode::deserialize(&s).unwrap(); | |
black_box(s); | |
}); | |
} | |
#[bench] | |
fn bench_transmute(b: &mut Bencher) { | |
let organization = black_box(organization()); | |
b.iter(|| { | |
unsafe { | |
let s: OrganizationCopy = std::mem::transmute(organization.clone()); | |
black_box(s); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment