diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 23b3981f..2da7b27c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,6 +23,7 @@ jobs: pr: if: ${{ github.event_name == 'pull_request' }} needs: + - bench - clippy - example - feature @@ -73,6 +74,19 @@ jobs: # Testing # ########### + bench: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + + - run: cargo clippy -p juniper_benchmarks --benches -- -D warnings + - run: cargo bench -p juniper_benchmarks + example: strategy: fail-fast: false @@ -295,6 +309,7 @@ jobs: release-github: name: Release on GitHub needs: + - bench - clippy - example - feature diff --git a/benches/Cargo.toml b/benches/Cargo.toml index 57a1c9d4..1cabd057 100644 --- a/benches/Cargo.toml +++ b/benches/Cargo.toml @@ -10,7 +10,7 @@ futures = "0.3" juniper = { path = "../juniper" } [dev-dependencies] -criterion = "0.3" +criterion = "0.4" tokio = { version = "1.0", features = ["rt-multi-thread"] } [[bench]] diff --git a/benches/benches/benchmark.rs b/benches/benches/benchmark.rs index c2ba501c..b6f82773 100644 --- a/benches/benches/benchmark.rs +++ b/benches/benches/benchmark.rs @@ -1,10 +1,11 @@ -use criterion::{criterion_group, criterion_main, Criterion, ParameterizedBenchmark}; +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use juniper::InputValue; use juniper_benchmarks as j; fn bench_sync_vs_async_users_flat_instant(c: &mut Criterion) { - const ASYNC_QUERY: &'static str = r#" + // language=GraphQL + const ASYNC_QUERY: &str = r#" query Query($id: Int) { users_async_instant(ids: [$id]!) { id @@ -15,74 +16,70 @@ fn bench_sync_vs_async_users_flat_instant(c: &mut Criterion) { } "#; - const SYNC_QUERY: &'static str = r#" - query Query($id: Int) { - users_sync_instant(ids: [$id]!) { - id - kind - username - email + // language=GraphQL + const SYNC_QUERY: &str = r#" + query Query($id: Int) { + users_sync_instant(ids: [$id]!) { + id + kind + username + email + } } + "#; + + let mut group = c.benchmark_group("Sync vs Async - Users Flat - Instant"); + for count in [1, 10] { + group.bench_function(BenchmarkId::new("Sync", count), |b| { + let ids = (0..count) + .map(|x| InputValue::scalar(x as i32)) + .collect::>(); + let ids = InputValue::list(ids); + b.iter(|| { + j::execute_sync( + SYNC_QUERY, + vec![("ids".to_owned(), ids.clone())].into_iter().collect(), + ) + }) + }); + + group.bench_function(BenchmarkId::new("Async - Single Thread", count), |b| { + let rt = tokio::runtime::Builder::new_current_thread() + .build() + .unwrap(); + + let ids = (0..count) + .map(|x| InputValue::scalar(x as i32)) + .collect::>(); + let ids = InputValue::list(ids); + + b.iter(|| { + let f = j::execute( + ASYNC_QUERY, + vec![("ids".to_owned(), ids.clone())].into_iter().collect(), + ); + rt.block_on(f) + }) + }); + + group.bench_function(BenchmarkId::new("Async - Threadpool", count), |b| { + let rt = tokio::runtime::Builder::new_multi_thread().build().unwrap(); + + let ids = (0..count) + .map(|x| InputValue::scalar(x as i32)) + .collect::>(); + let ids = InputValue::list(ids); + + b.iter(|| { + let f = j::execute( + ASYNC_QUERY, + vec![("ids".to_owned(), ids.clone())].into_iter().collect(), + ); + rt.block_on(f) + }) + }); } -"#; - - c.bench( - "Sync vs Async - Users Flat - Instant", - ParameterizedBenchmark::new( - "Sync", - |b, count| { - let ids = (0..*count) - .map(|x| InputValue::scalar(x as i32)) - .collect::>(); - let ids = InputValue::list(ids); - b.iter(|| { - j::execute_sync( - SYNC_QUERY, - vec![("ids".to_owned(), ids.clone())].into_iter().collect(), - ) - }) - }, - vec![1, 10], - ) - .with_function("Async - Single Thread", |b, count| { - let mut rt = tokio::runtime::Builder::new() - .basic_scheduler() - .build() - .unwrap(); - - let ids = (0..*count) - .map(|x| InputValue::scalar(x as i32)) - .collect::>(); - let ids = InputValue::list(ids); - - b.iter(|| { - let f = j::execute( - ASYNC_QUERY, - vec![("ids".to_owned(), ids.clone())].into_iter().collect(), - ); - rt.block_on(f) - }) - }) - .with_function("Async - Threadpool", |b, count| { - let mut rt = tokio::runtime::Builder::new() - .threaded_scheduler() - .build() - .unwrap(); - - let ids = (0..*count) - .map(|x| InputValue::scalar(x as i32)) - .collect::>(); - let ids = InputValue::list(ids); - - b.iter(|| { - let f = j::execute( - ASYNC_QUERY, - vec![("ids".to_owned(), ids.clone())].into_iter().collect(), - ); - rt.block_on(f) - }) - }), - ); + group.finish(); } criterion_group!(benches, bench_sync_vs_async_users_flat_instant);