1use std::{collections::HashMap, fs::read_dir, hint::black_box, path::Path};23use criterion::{Criterion, criterion_group, criterion_main};4use jrsonnet_evaluator::{5 FileImportResolver, State, apply_tla,6 manifest::{BlackBoxFormat, JsonFormat},7 stack::limit_stack_depth,8 trace::PathResolver,9};1011#[global_allocator]12static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1314fn bench_entry(c: &mut Criterion, path: &Path) {15 c.bench_function(16 path.file_name()17 .expect("file path")18 .to_str()19 .expect("name is utf-8"),20 |b| {21 let _stack = limit_stack_depth(200_000);2223 let mut s = State::builder();2425 s.context_initializer(jrsonnet_stdlib::ContextInitializer::new(26 PathResolver::Absolute,27 ))28 .import_resolver(FileImportResolver::new(vec![]));2930 let s = s.build();31 let _s = s.enter();3233 b.iter(|| {34 let imported = s.import(path).expect("evaluated");35 let res = apply_tla(&HashMap::new(), imported).expect("tla applied");36 black_box(res.manifest(JsonFormat::cli(3)).expect("manifested"));37 });38 },39 );40}41fn criterion_benchmark(c: &mut Criterion) {42 for entry in read_dir("go_builtin_benchmarks").expect("dir exists") {43 let entry = entry.expect("entry is valid");44 assert!(entry.metadata().expect("entry is valid").is_file());45 bench_entry(c, &entry.path());46 }47 for entry in read_dir("cpp_perf_tests").expect("dir exists") {48 let entry = entry.expect("entry is valid");49 assert!(entry.metadata().expect("entry is valid").is_file());50 bench_entry(c, &entry.path());51 }52 for entry in read_dir("cpp_benchmarks").expect("dir exists") {53 let entry = entry.expect("entry is valid");54 if entry.path().extension().is_none_or(|e| e != "jsonnet") {55 continue;56 }57 assert!(entry.metadata().expect("entry is valid").is_file());58 bench_entry(c, &entry.path());59 }60}6162criterion_group!(benches, criterion_benchmark);63criterion_main!(benches);