-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dmitry Dygalo <[email protected]>
- Loading branch information
1 parent
8dc33ea
commit c30bc40
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use bench_helpers::{ | ||
bench_citm, bench_fast, bench_geojson, bench_keywords, bench_openapi, bench_swagger, | ||
}; | ||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
use jsonschema::JSONSchema; | ||
use serde_json::Value; | ||
|
||
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(); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
fn keywords(c: &mut Criterion) { | ||
bench_keywords( | ||
c, | ||
&|_: &str| false, | ||
&|schema: &Value, instance: &Value| { | ||
let compiled = JSONSchema::compile(schema).expect("Valid schema"); | ||
compiled.is_valid(instance) | ||
}, | ||
&mut |c: &mut Criterion, name: &str, schema: &Value| { | ||
c.bench_with_input( | ||
BenchmarkId::new(name, "jsonschema_rs/compile"), | ||
schema, | ||
|b, schema| { | ||
b.iter(|| { | ||
JSONSchema::compile(schema).expect("Valid schema"); | ||
}) | ||
}, | ||
); | ||
}, | ||
validate_valid, | ||
validate_invalid, | ||
) | ||
} | ||
|
||
fn validate_valid(c: &mut Criterion, name: &str, schema: &Value, instance: &Value) { | ||
let compiled = JSONSchema::compile(schema).expect("Valid schema"); | ||
c.bench_with_input( | ||
BenchmarkId::new(name, "jsonschema_rs/is_valid/valid"), | ||
instance, | ||
|b, instance| { | ||
b.iter(|| { | ||
let _ = compiled.is_valid(instance); | ||
}) | ||
}, | ||
); | ||
c.bench_with_input( | ||
BenchmarkId::new(name, "jsonschema_rs/validate/valid"), | ||
instance, | ||
|b, instance| { | ||
b.iter(|| { | ||
compiled.validate(instance).ok(); | ||
}) | ||
}, | ||
); | ||
} | ||
|
||
fn validate_invalid(c: &mut Criterion, name: &str, schema: &Value, instance: &Value) { | ||
let compiled = JSONSchema::compile(schema).expect("Valid schema"); | ||
c.bench_with_input( | ||
BenchmarkId::new(name, "jsonschema_rs/is_valid/invalid"), | ||
instance, | ||
|b, instance| { | ||
b.iter(|| { | ||
let _ = compiled.is_valid(instance); | ||
}) | ||
}, | ||
); | ||
c.bench_with_input( | ||
BenchmarkId::new(name, "jsonschema_rs/validate/invalid"), | ||
instance, | ||
|b, instance| { | ||
b.iter(|| { | ||
let _: Vec<_> = compiled | ||
.validate(instance) | ||
.expect_err("There should be errors") | ||
.collect(); | ||
}) | ||
}, | ||
); | ||
} | ||
|
||
criterion_group!(arbitrary, large_schemas, fast_schema); | ||
criterion_main!(arbitrary); |