use std::path::{Path, PathBuf};
use criterion::{Criterion, black_box, criterion_group, criterion_main};
fn workspace_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).into()
}
fn test_plugin_wasm() -> PathBuf {
let p = workspace_root().join("target/wasm32-wasip2/release/test_plugin.wasm");
assert!(
p.exists(),
"test_plugin.wasm not found at {}; build it first with \
`cargo component build -p test_plugin --target wasm32-wasip2 --release`",
p.display()
);
p
}
fn make_loaded_manager() -> (yosh::plugin::PluginManager, yosh::env::ShellEnv) {
let mut env = yosh::env::ShellEnv::new("yosh", Vec::new());
env.vars
.set("BENCH_VAR", "bench_value")
.expect("set BENCH_VAR");
let mut mgr = yosh::plugin::PluginManager::new();
yosh::plugin::test_helpers::load_plugin_with_caps(
&mut mgr,
&test_plugin_wasm(),
&mut env,
yosh_plugin_api::CAP_ALL,
)
.expect("load test_plugin");
(mgr, env)
}
fn bench_exec_no_host_call(c: &mut Criterion) {
let (mut mgr, mut env) = make_loaded_manager();
let args: Vec<String> = vec!["bench".into()];
c.bench_function("plugin_exec_test_cmd", |b| {
b.iter(|| {
let r = mgr.exec_command(&mut env, "test_cmd", black_box(&args));
black_box(r);
});
});
}
fn bench_exec_with_var_get(c: &mut Criterion) {
let (mut mgr, mut env) = make_loaded_manager();
let args: Vec<String> = vec!["BENCH_VAR".into()];
c.bench_function("plugin_exec_echo_var", |b| {
b.iter(|| {
let r = mgr.exec_command(&mut env, "echo_var", black_box(&args));
black_box(r);
});
});
}
fn bench_hook_pre_exec(c: &mut Criterion) {
let (mut mgr, mut env) = make_loaded_manager();
c.bench_function("plugin_hook_pre_exec", |b| {
b.iter(|| {
mgr.call_pre_exec(&mut env, black_box("noop"));
});
});
}
criterion_group!(plugin_benches,
bench_exec_no_host_call,
bench_exec_with_var_get,
bench_hook_pre_exec,
);
criterion_main!(plugin_benches);