Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Add boon to benchmarks #485

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion jsonschema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ getrandom = { version = "0.2", features = ["js"] }

[dev-dependencies]
bench_helpers = { path = "../bench_helpers" }
boon = "0.5"
codspeed-criterion-compat = "2.6.0"
criterion = { version = "0.5.1", features = [], default-features = false }
json_schema_test_suite = { version = "0.3.0", path = "../jsonschema-test-suite" }
jsonschema-valid = "0.5"
lazy_static = "1.4" # Needed for json schema test suite
lazy_static = "1.4" # Needed for json schema test suite
mockito = "0.31"
paste = "1.0"
test-case = "3"
Expand All @@ -93,6 +94,11 @@ name = "valico"
harness = false
name = "jsonschema_valid"

# Benchmarks for `boon`
[[bench]]
harness = false
name = "boon"

[profile.release]
codegen-units = 1
lto = "fat"
53 changes: 53 additions & 0 deletions jsonschema/benches/boon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use bench_helpers::{bench_citm, bench_fast, bench_geojson, bench_openapi, bench_swagger};
use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion};

macro_rules! boon_bench {
($c:tt, $name:expr, $schema:ident, $instance:ident) => {{
let mut schemas = boon::Schemas::new();
let mut compiler = boon::Compiler::new();
compiler.add_resource("schema.json", $schema).unwrap();
let id = compiler.compile("schema.json", &mut schemas).unwrap();
assert!(schemas.validate(&$instance, id).is_ok(), "Invalid instance");
$c.bench_function(&format!("{} boon/validate", $name), |b| {
b.iter(|| {
let _ = schemas.validate(&$instance, id).is_ok();
});
});
}};
}

fn large_schemas(c: &mut Criterion) {
// Open API JSON Schema
// Only `jsonschema` works correctly - other libraries do not recognize `zuora` as valid
bench_openapi(&mut |name, schema, instance| boon_bench!(c, name, schema, instance));
// Swagger JSON Schema
bench_swagger(&mut |name, schema, instance| boon_bench!(c, name, schema, instance));
// Canada borders in GeoJSON
bench_geojson(&mut |name, schema, instance| boon_bench!(c, name, schema, instance));
// CITM catalog
bench_citm(&mut |name, schema, instance| boon_bench!(c, name, schema, instance));
}

fn fast_schema(c: &mut Criterion) {
bench_fast(&mut |name, schema, valid, invalid| {
let mut schemas = boon::Schemas::new();
let mut compiler = boon::Compiler::new();
compiler.add_resource("schema.json", schema).unwrap();
let id = compiler.compile("schema.json", &mut schemas).unwrap();
assert!(schemas.validate(&valid, id).is_ok(), "Invalid instance");
assert!(schemas.validate(&invalid, id).is_err(), "Invalid instance");
c.bench_function(&format!("{} boon/is_valid/valid", name), |b| {
b.iter(|| {
let _ = schemas.validate(&valid, id).is_ok();
});
});
c.bench_function(&format!("{} boon/is_valid/invalid", name), |b| {
b.iter(|| {
let _ = schemas.validate(&invalid, id).is_ok();
});
});
});
}

criterion_group!(arbitrary, large_schemas, fast_schema);
criterion_main!(arbitrary);