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 CodSpeed #484

Merged
merged 1 commit into from
May 10, 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
31 changes: 31 additions & 0 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Benchmarks

on:
push:
branches:
- "master"
pull_request:
workflow_dispatch:

jobs:
rust:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@stable

- uses: Swatinem/rust-cache@v2
with:
workspaces: jsonschema

- run: cargo install cargo-codspeed

- run: cargo codspeed build
working-directory: ./jsonschema

- uses: CodSpeedHQ/action@v2
with:
run: cargo codspeed run jsonschema
token: ${{ secrets.CODSPEED_TOKEN }}
working-directory: ./jsonschema
1 change: 1 addition & 0 deletions bench_helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ license = "MIT"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
criterion = { version = "0.5.1", features = [], default-features = false }
codspeed-criterion-compat = "2.6.0"
2 changes: 1 addition & 1 deletion bench_helpers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::Criterion;
use codspeed_criterion_compat::Criterion;
use serde::Deserialize;
use serde_json::{from_reader, Value};
use std::{
Expand Down
5 changes: 3 additions & 2 deletions jsonschema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ getrandom = { version = "0.2", features = ["js"] }

[dev-dependencies]
bench_helpers = { path = "../bench_helpers" }
codspeed-criterion-compat = "2.6.0"
criterion = { version = "0.5.1", features = [], default-features = false }
lazy_static = "1.4" # Needed for json schema test suite
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
mockito = "0.31"
paste = "1.0"
test-case = "3"
valico = "3.6"
valico = "4"

# Benchmarks for `jsonschema`
[[bench]]
Expand Down
49 changes: 22 additions & 27 deletions jsonschema/benches/jsonschema.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
use bench_helpers::{
bench_citm, bench_fast, bench_geojson, bench_keywords, bench_openapi, bench_swagger,
};
use criterion::{criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
use jsonschema::{paths::JsonPointerNode, JSONSchema};
use serde_json::Value;

macro_rules! jsonschema_rs_bench {
macro_rules! jsonschema_bench {
($c:tt, $name:expr, $schema:ident, $instance:ident) => {{
let compiled = JSONSchema::options()
.with_meta_schemas()
.compile(&$schema)
.expect("Invalid schema");
assert!(compiled.is_valid(&$instance), "Invalid instance");
assert!(compiled.validate(&$instance).is_ok(), "Invalid instance");
$c.bench_function(&format!("{} jsonschema_rs/compile", $name), |b| {
$c.bench_function(&format!("{} jsonschema/compile", $name), |b| {
b.iter(|| JSONSchema::options().with_meta_schemas().compile(&$schema))
});
$c.bench_function(&format!("{} jsonschema_rs/is_valid", $name), |b| {
$c.bench_function(&format!("{} jsonschema/is_valid", $name), |b| {
b.iter(|| compiled.is_valid(&$instance))
});
$c.bench_function(&format!("{} jsonschema_rs/validate", $name), |b| {
$c.bench_function(&format!("{} jsonschema/validate", $name), |b| {
b.iter(|| compiled.validate(&$instance).ok())
});
}};
Expand All @@ -28,33 +28,33 @@ macro_rules! jsonschema_rs_bench {
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| jsonschema_rs_bench!(c, name, schema, instance));
bench_openapi(&mut |name, schema, instance| jsonschema_bench!(c, name, schema, instance));
// Swagger JSON Schema
bench_swagger(&mut |name, schema, instance| jsonschema_rs_bench!(c, name, schema, instance));
bench_swagger(&mut |name, schema, instance| jsonschema_bench!(c, name, schema, instance));
// Canada borders in GeoJSON
bench_geojson(&mut |name, schema, instance| jsonschema_rs_bench!(c, name, schema, instance));
bench_geojson(&mut |name, schema, instance| jsonschema_bench!(c, name, schema, instance));
// CITM catalog
bench_citm(&mut |name, schema, instance| jsonschema_rs_bench!(c, name, schema, instance));
bench_citm(&mut |name, schema, instance| jsonschema_bench!(c, name, schema, instance));
}

fn fast_schema(c: &mut Criterion) {
bench_fast(&mut |name, schema, valid, invalid| {
let compiled = JSONSchema::compile(&schema).expect("Valid schema");
assert!(compiled.is_valid(&valid));
assert!(!compiled.is_valid(&invalid));
c.bench_function(&format!("{} jsonschema_rs/compile", name), |b| {
c.bench_function(&format!("{} jsonschema/compile", name), |b| {
b.iter(|| JSONSchema::compile(&schema).expect("Valid schema"))
});
c.bench_function(&format!("{} jsonschema_rs/is_valid/valid", name), |b| {
c.bench_function(&format!("{} jsonschema/is_valid/valid", name), |b| {
b.iter(|| compiled.is_valid(&valid))
});
c.bench_function(&format!("{} jsonschema_rs/validate/valid", name), |b| {
c.bench_function(&format!("{} jsonschema/validate/valid", name), |b| {
b.iter(|| compiled.validate(&valid).ok())
});
c.bench_function(&format!("{} jsonschema_rs/is_valid/invalid", name), |b| {
c.bench_function(&format!("{} jsonschema/is_valid/invalid", name), |b| {
b.iter(|| compiled.is_valid(&invalid))
});
c.bench_function(&format!("{} jsonschema_rs/validate/invalid", name), |b| {
c.bench_function(&format!("{} jsonschema/validate/invalid", name), |b| {
b.iter(|| {
let _: Vec<_> = compiled
.validate(&invalid)
Expand All @@ -75,7 +75,7 @@ fn keywords(c: &mut Criterion) {
},
&mut |c: &mut Criterion, name: &str, schema: &Value| {
c.bench_with_input(
BenchmarkId::new(name, "jsonschema_rs/compile"),
BenchmarkId::new(name, "jsonschema/compile"),
schema,
|b, schema| {
b.iter(|| {
Expand All @@ -92,7 +92,7 @@ fn keywords(c: &mut Criterion) {
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"),
BenchmarkId::new(name, "jsonschema/is_valid/valid"),
instance,
|b, instance| {
b.iter(|| {
Expand All @@ -101,7 +101,7 @@ fn validate_valid(c: &mut Criterion, name: &str, schema: &Value, instance: &Valu
},
);
c.bench_with_input(
BenchmarkId::new(name, "jsonschema_rs/validate/valid"),
BenchmarkId::new(name, "jsonschema/validate/valid"),
instance,
|b, instance| {
b.iter(|| {
Expand All @@ -114,7 +114,7 @@ fn validate_valid(c: &mut Criterion, name: &str, schema: &Value, instance: &Valu
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"),
BenchmarkId::new(name, "jsonschema/is_valid/invalid"),
instance,
|b, instance| {
b.iter(|| {
Expand All @@ -123,7 +123,7 @@ fn validate_invalid(c: &mut Criterion, name: &str, schema: &Value, instance: &Va
},
);
c.bench_with_input(
BenchmarkId::new(name, "jsonschema_rs/validate/invalid"),
BenchmarkId::new(name, "jsonschema/validate/invalid"),
instance,
|b, instance| {
b.iter(|| {
Expand Down Expand Up @@ -162,11 +162,6 @@ fn json_pointer_node(c: &mut Criterion) {
c.bench_with_input(BenchmarkId::new("jsonpointer", "big"), &node, bench);
}

criterion_group!(
arbitrary,
large_schemas,
fast_schema,
keywords,
json_pointer_node
);
criterion_main!(arbitrary);
criterion_group!(common, large_schemas, fast_schema, json_pointer_node);
criterion_group!(specific, keywords);
criterion_main!(common, specific);
2 changes: 1 addition & 1 deletion jsonschema/benches/jsonschema_valid.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bench_helpers::{bench_citm, bench_fast, bench_geojson, bench_keywords};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use codspeed_criterion_compat::{criterion_group, criterion_main, BenchmarkId, Criterion};
use jsonschema_valid::schemas;
use serde_json::Value;

Expand Down
2 changes: 1 addition & 1 deletion jsonschema/benches/valico.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bench_helpers::{bench_citm, bench_fast, bench_geojson, bench_keywords, bench_swagger};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use codspeed_criterion_compat::{criterion_group, criterion_main, BenchmarkId, Criterion};
use serde_json::Value;
use valico::json_schema;

Expand Down