Skip to content

Commit

Permalink
fix(compiler): add a benchmark for #862
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Jun 5, 2024
1 parent ad30e71 commit 5b9875b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/apollo-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ name = "fields-validation"
path = "benches/fields_validation.rs"
harness = false

[[bench]]
name = "fragments-validation"
path = "benches/fragments_validation.rs"
harness = false

[[test]]
name = "main"

Expand Down
51 changes: 51 additions & 0 deletions crates/apollo-compiler/benches/fragments_validation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use apollo_compiler::ExecutableDocument;
use apollo_compiler::Schema;
use criterion::*;
use std::fmt::Write;

fn bench_big_schema_many_fragments(c: &mut Criterion) {
const NUM_INTERFACES: usize = 200;
const NUM_OBJECTS: usize = 10_000;

let mut sdl = String::new();
for i in 0..NUM_INTERFACES {
_ = writeln!(&mut sdl, r#"interface Intf{i} {{ field: Int! }}"#);
}
for o in 0..NUM_OBJECTS {
let i = o % NUM_INTERFACES;
_ = writeln!(
&mut sdl,
r#"type Ty{o} implements Intf{i} {{ field: Int! }}"#
);
}

_ = writeln!(&mut sdl, "type Query {{");
for i in 0..NUM_INTERFACES {
_ = writeln!(&mut sdl, " intf{i}: Intf{i}");
}
_ = writeln!(&mut sdl, "}}");

let schema = Schema::parse_and_validate(sdl, "schema.graphql").unwrap();
let mut selection = String::new();
let mut fragments = String::new();
for i in 0..NUM_INTERFACES {
_ = writeln!(&mut selection, " intf{i} {{ ...frag{i} }}");
_ = writeln!(&mut fragments, "fragment frag{i} on Ty{i} {{ field }}");
}
let query = format!(
"query {{
{selection}}}
{fragments}"
);

c.bench_function("big_schema_many_fragments", move |b| {
b.iter(|| {
let doc =
ExecutableDocument::parse_and_validate(&schema, &query, "query.graphql").unwrap();
black_box(doc);
});
});
}

criterion_group!(fragments, bench_big_schema_many_fragments,);
criterion_main!(fragments);

0 comments on commit 5b9875b

Please sign in to comment.