1use std::{collections::HashMap, fs, fs::read_dir, hint::black_box, path::Path};23use criterion::{Criterion, criterion_group, criterion_main};4use jrsonnet_evaluator::{5 FileImportResolver, State, apply_tla, manifest::JsonFormat, stack::limit_stack_depth,6 trace::PathResolver,7};89#[global_allocator]10static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1112fn bench_entry(c: &mut Criterion, path: &Path) {13 let name = path14 .file_name()15 .expect("file path")16 .to_str()17 .expect("name is utf-8")18 .to_owned();19 let code = fs::read_to_string(path).expect("read bench source");2021 c.bench_function(&name, |b| {22 let _stack = limit_stack_depth(200_000);2324 let mut s = State::builder();25 s.context_initializer(jrsonnet_stdlib::ContextInitializer::new(26 PathResolver::Absolute,27 ))28 .import_resolver(FileImportResolver::new(vec![]));29 let s = s.build();30 let _entered = s.enter();3132 33 34 let prepared = s35 .prepare_snippet(name.clone(), code.clone())36 .expect("prepared");3738 b.iter(|| {39 let imported = s.evaluate_prepared_snippet(&prepared).expect("evaluated");40 let res = apply_tla(&HashMap::new(), imported).expect("tla applied");41 black_box(res.manifest(JsonFormat::cli(3)).expect("manifested"));42 });43 });44}45fn criterion_benchmark(c: &mut Criterion) {46 for entry in read_dir("go_builtin_benchmarks").expect("dir exists") {47 let entry = entry.expect("entry is valid");48 assert!(entry.metadata().expect("entry is valid").is_file());49 bench_entry(c, &entry.path());50 }51 for entry in read_dir("cpp_perf_tests").expect("dir exists") {52 let entry = entry.expect("entry is valid");53 assert!(entry.metadata().expect("entry is valid").is_file());54 bench_entry(c, &entry.path());55 }56 for entry in read_dir("cpp_benchmarks").expect("dir exists") {57 let entry = entry.expect("entry is valid");58 if entry.path().extension().is_none_or(|e| e != "jsonnet") {59 continue;60 }61 assert!(entry.metadata().expect("entry is valid").is_file());62 bench_entry(c, &entry.path());63 }64}6566criterion_group!(benches, criterion_benchmark);67criterion_main!(benches);