Last active
November 26, 2017 22:44
-
-
Save kamek-pf/fd51d9d9b8417be2659bd0dc6be2a70e to your computer and use it in GitHub Desktop.
Rust AWS client init benchmark
This file contains hidden or 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
// In this test, I benchmark 3 different ways to initialize AWS clients in Rust, using the Rusoto crates | |
// I wanted to know what was more efficient : | |
// - Create a new client for every call (Boxed or not) | |
// - Initialize once using the lazy_static macro and a mutex | |
// - Initialize one using the thread_local macro | |
#![feature(test)] | |
extern crate test; | |
extern crate rusoto_core; | |
extern crate rusoto_sns; | |
#[macro_use] | |
extern crate lazy_static; | |
use std::sync::Mutex; | |
use rusoto_core::{default_tls_client, DefaultCredentialsProvider, Region}; | |
use rusoto_sns::{Sns, SnsClient, UnsubscribeInput}; | |
thread_local! { | |
pub static SNS: Box<Sns> = init_sns(); | |
} | |
lazy_static! { | |
pub static ref SNS_LS: Mutex<Box<Sns + Send>> = Mutex::new(init_sns()); | |
} | |
fn init_sns() -> Box<Sns + Send> { | |
let provider = DefaultCredentialsProvider::new().unwrap(); | |
let client = SnsClient::new(default_tls_client().unwrap(), provider, Region::UsEast1); | |
Box::new(client) | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
use test::Bencher; | |
#[bench] | |
fn raw_init(b: &mut Bencher) { | |
let input = UnsubscribeInput { | |
subscription_arn: "kek".to_string() | |
}; | |
b.iter(|| { | |
let provider = DefaultCredentialsProvider::new().unwrap(); | |
let client = SnsClient::new(default_tls_client().unwrap(), provider, Region::UsEast1); | |
client.unsubscribe(&input); | |
}) | |
} | |
#[bench] | |
fn raw_init_boxed(b: &mut Bencher) { | |
let input = UnsubscribeInput { | |
subscription_arn: "kek".to_string() | |
}; | |
b.iter(|| { | |
let client = init_sns(); | |
client.unsubscribe(&input); | |
}) | |
} | |
#[bench] | |
fn thread_local(b: &mut Bencher) { | |
let input = UnsubscribeInput { | |
subscription_arn: "kek".to_string() | |
}; | |
b.iter(|| { | |
SNS.with(|client| { | |
client.unsubscribe(&input); | |
}) | |
}); | |
} | |
#[bench] | |
fn lazy_static(b: &mut Bencher) { | |
let input = UnsubscribeInput { | |
subscription_arn: "kek".to_string() | |
}; | |
b.iter(|| { | |
let client = SNS_LS.lock().unwrap(); | |
client.unsubscribe(&input); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rustc 1.23.0-nightly (aabfed5e0 2017-11-17)
Results here (
rustup nightly run cargo bench
), looks like cargo runs about 300 iterations for each test :So
thread_local
andlazy_static
are slightly better. I wouldn't worry too much about this, just go with what's more convenient.