1use std::{2 collections::HashMap,3 fs,4 fs::read_dir,5 hint::black_box,6 path::{Path, PathBuf},7};89use criterion::{Criterion, criterion_group, criterion_main};10use jrsonnet_evaluator::{11 FileImportResolver, State, apply_tla, manifest::JsonFormat, stack::limit_stack_depth,12 trace::PathResolver,13};1415#[global_allocator]16static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1718fn bench_entry(c: &mut Criterion, path: &Path) {19 let name = path20 .file_name()21 .expect("file path")22 .to_str()23 .expect("name is utf-8")24 .to_owned();25 let code = fs::read_to_string(path).expect("read bench source");2627 c.bench_function(&name, |b| {28 let _stack = limit_stack_depth(200_000);2930 let mut s = State::builder();31 s.context_initializer(jrsonnet_stdlib::ContextInitializer::new(32 PathResolver::Absolute,33 ))34 .import_resolver(FileImportResolver::new(vec![]));35 let s = s.build();36 let _entered = s.enter();3738 39 40 let prepared = s41 .prepare_snippet(name.clone(), code.clone())42 .expect("prepared");4344 b.iter(|| {45 let imported = s.evaluate_prepared_snippet(&prepared).expect("evaluated");46 let res = apply_tla(&HashMap::new(), imported).expect("tla applied");47 black_box(res.manifest(JsonFormat::cli(3)).expect("manifested"));48 });49 });50}51fn criterion_benchmark(c: &mut Criterion) {52 if let Some(go_jsonnet) = std::env::var_os("GO_JSONNET_FOR_TESTS").map(PathBuf::from) {53 for entry in read_dir(go_jsonnet.join("builtin-benchmarks")).expect("dir exists") {54 let entry = entry.expect("entry is valid");55 assert!(entry.metadata().expect("entry is valid").is_file());56 bench_entry(c, &entry.path());57 }58 }59 if let Some(cpp_jsonnet) = std::env::var_os("CPP_JSONNET_FOR_TESTS").map(PathBuf::from) {60 for entry in read_dir(cpp_jsonnet.join("perf_tests")).expect("dir exists") {61 let entry = entry.expect("entry is valid");62 assert!(entry.metadata().expect("entry is valid").is_file());63 bench_entry(c, &entry.path());64 }65 for entry in read_dir(cpp_jsonnet.join("benchmarks")).expect("dir exists") {66 let entry = entry.expect("entry is valid");67 if entry.path().extension().is_none_or(|e| e != "jsonnet") {68 continue;69 }70 assert!(entry.metadata().expect("entry is valid").is_file());71 bench_entry(c, &entry.path());72 }73 }74}7576criterion_group!(benches, criterion_benchmark);77criterion_main!(benches);