juniper/juniper_benchmarks/benches/benchmark.rs

92 lines
2.6 KiB
Rust
Raw Normal View History

2019-08-21 05:07:30 -05:00
extern crate juniper_benchmarks;
2020-03-20 11:11:06 -05:00
use criterion::{criterion_group, criterion_main, Criterion, ParameterizedBenchmark};
2019-08-21 05:07:30 -05:00
2020-03-20 11:11:06 -05:00
use juniper::InputValue;
2019-08-21 05:07:30 -05:00
use juniper_benchmarks as j;
2020-01-22 01:37:45 -06:00
fn bench_sync_vs_async_users_flat_instant(c: &mut Criterion) {
const ASYNC_QUERY: &'static str = r#"
2019-08-21 05:07:30 -05:00
query Query($id: Int) {
2020-01-22 01:37:45 -06:00
users_async_instant(ids: [$id]!) {
2019-08-21 05:07:30 -05:00
id
kind
username
email
}
}
"#;
2020-01-22 01:37:45 -06:00
const SYNC_QUERY: &'static str = r#"
query Query($id: Int) {
users_sync_instant(ids: [$id]!) {
id
kind
username
email
}
}
"#;
2019-08-21 05:07:30 -05:00
c.bench(
2020-01-22 01:37:45 -06:00
"Sync vs Async - Users Flat - Instant",
2019-08-21 05:07:30 -05:00
ParameterizedBenchmark::new(
"Sync",
|b, count| {
let ids = (0..*count)
.map(|x| InputValue::scalar(x as i32))
.collect::<Vec<_>>();
let ids = InputValue::list(ids);
b.iter(|| {
j::execute_sync(
2020-01-22 01:37:45 -06:00
SYNC_QUERY,
2019-08-21 05:07:30 -05:00
vec![("ids".to_string(), ids.clone())].into_iter().collect(),
)
})
},
vec![1, 10],
)
.with_function("Async - Single Thread", |b, count| {
2020-01-22 01:37:45 -06:00
let mut rt = tokio::runtime::Builder::new()
.basic_scheduler()
.build()
.unwrap();
2019-08-21 05:07:30 -05:00
let ids = (0..*count)
.map(|x| InputValue::scalar(x as i32))
.collect::<Vec<_>>();
let ids = InputValue::list(ids);
b.iter(|| {
let f = j::execute(
2020-01-22 01:37:45 -06:00
ASYNC_QUERY,
2019-08-21 05:07:30 -05:00
vec![("ids".to_string(), ids.clone())].into_iter().collect(),
);
rt.block_on(f)
})
})
.with_function("Async - Threadpool", |b, count| {
2020-01-22 01:37:45 -06:00
let mut rt = tokio::runtime::Builder::new()
.threaded_scheduler()
.build()
.unwrap();
2019-08-21 05:07:30 -05:00
let ids = (0..*count)
.map(|x| InputValue::scalar(x as i32))
.collect::<Vec<_>>();
let ids = InputValue::list(ids);
b.iter(|| {
let f = j::execute(
2020-01-22 01:37:45 -06:00
ASYNC_QUERY,
2019-08-21 05:07:30 -05:00
vec![("ids".to_string(), ids.clone())].into_iter().collect(),
);
rt.block_on(f)
})
}),
);
}
2020-01-22 01:37:45 -06:00
criterion_group!(benches, bench_sync_vs_async_users_flat_instant);
2019-08-21 05:07:30 -05:00
criterion_main!(benches);