| 1 | use codspeed_criterion_compat::{ |
| 2 | criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion, |
| 3 | }; |
| 4 | use referencing::{Draft, Registry, SPECIFICATIONS}; |
| 5 | |
| 6 | static DRAFT4: &[u8] = include_bytes!("../../benchmark/data/subresources/draft4.json"); |
| 7 | static DRAFT6: &[u8] = include_bytes!("../../benchmark/data/subresources/draft6.json"); |
| 8 | static DRAFT7: &[u8] = include_bytes!("../../benchmark/data/subresources/draft7.json"); |
| 9 | static DRAFT201909: &[u8] = include_bytes!("../../benchmark/data/subresources/draft201909.json"); |
| 10 | static DRAFT202012: &[u8] = include_bytes!("../../benchmark/data/subresources/draft202012.json"); |
| 11 | |
| 12 | fn bench_subresources(c: &mut Criterion) { |
| 13 | let drafts = [ |
| 14 | (Draft::Draft4, DRAFT4, "draft 4"), |
| 15 | (Draft::Draft6, DRAFT6, "draft 6"), |
| 16 | (Draft::Draft7, DRAFT7, "draft 7"), |
| 17 | (Draft::Draft201909, DRAFT201909, "draft 2019-09"), |
| 18 | (Draft::Draft202012, DRAFT202012, "draft 2020-12"), |
| 19 | ]; |
| 20 | |
| 21 | let mut group = c.benchmark_group("registry"); |
| 22 | |
| 23 | for (draft, data, name) in &drafts { |
| 24 | let schema = benchmark::read_json(data); |
| 25 | |
| 26 | group.bench_with_input(BenchmarkId::new("try_new", name), &schema, |b, schema| { |
| 27 | b.iter_batched( |
| 28 | || draft.create_resource(schema.clone()), |
| 29 | |resource| { |
| 30 | Registry::try_new("http://example.com/schema.json", resource) |
| 31 | .expect("Invalid registry input") |
| 32 | }, |
| 33 | BatchSize::SmallInput, |
| 34 | ); |
| 35 | }); |
| 36 | } |
| 37 | let drafts = [ |
| 38 | (Draft::Draft4, benchmark::GEOJSON, "GeoJSON"), |
| 39 | (Draft::Draft4, benchmark::SWAGGER, "Swagger"), |
| 40 | (Draft::Draft4, benchmark::OPEN_API, "Open API"), |
| 41 | (Draft::Draft4, benchmark::CITM_SCHEMA, "CITM"), |
| 42 | (Draft::Draft7, benchmark::FAST_SCHEMA, "Fast"), |
| 43 | ]; |
| 44 | |
| 45 | for (draft, data, name) in &drafts { |
| 46 | let schema = benchmark::read_json(data); |
| 47 | |
| 48 | group.bench_with_input( |
| 49 | BenchmarkId::new("try_with_resource", name), |
| 50 | &schema, |
| 51 | |b, schema| { |
| 52 | b.iter_batched( |
| 53 | || { |
| 54 | ( |
| 55 | draft.create_resource(schema.clone()), |
| 56 | SPECIFICATIONS.clone(), |
| 57 | ) |
| 58 | }, |
| 59 | |(resource, registry)| { |
| 60 | registry.try_with_resource("http://example.com/schema.json", resource) |
| 61 | }, |
| 62 | BatchSize::SmallInput, |
| 63 | ); |
| 64 | }, |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | group.finish(); |
| 69 | } |
| 70 | |
| 71 | criterion_group!(benches, bench_subresources); |
| 72 | criterion_main!(benches); |