MCPcopy Create free account
hub / github.com/PerroEngine/Perro / bench_harness_source

Function bench_harness_source

perro_source/devtools/perro_cli/src/bench.rs:177–285  ·  view source on GitHub ↗
(filters: &[String], methods: &[String], vars: &[String])

Source from the content-addressed store, hash-verified

175}
176
177fn bench_harness_source(filters: &[String], methods: &[String], vars: &[String]) -> String {
178 let filters = rust_str_array(filters);
179 let methods = rust_str_array(methods);
180 let vars = rust_str_array(vars);
181 let workspace = normalize_toml_path(&workspace_root());
182 format!(
183 r#"use criterion::{{black_box, criterion_group, criterion_main, Criterion}};
184use perro_api::prelude::*;
185use perro_runtime::Runtime;
186use perro_api::runtime_api::sub_apis::NodeAPI;
187use perro_api::scripting::ScriptBehavior;
188use perro_api::variant::Variant;
189
190const SCRIPT_FILTERS: &[&str] = {filters};
191const METHODS: &[&str] = {methods};
192const VARS: &[&str] = {vars};
193
194fn keep_script(hash: u64) -> bool {{
195 SCRIPT_FILTERS.is_empty()
196 || SCRIPT_FILTERS.iter().any(|filter| {{
197 filter == &hash.to_string() || filter == &format!("0x{{hash:x}}")
198 }})
199}}
200
201fn behavior_from_ctor(
202 ctor: perro_api::scripting::ScriptConstructor<perro_runtime::RuntimeScriptApi>,
203) -> Box<dyn ScriptBehavior<perro_runtime::RuntimeScriptApi>> {{
204 let raw = ctor();
205 assert!(!raw.is_null(), "script constructor returned null");
206 // SAFETY: Script ctor returns Box::into_raw pointer for expected trait object.
207 unsafe {{ Box::from_raw(raw) }}
208}}
209
210fn bench_script_ctor_state(c: &mut Criterion) {{
211 for (hash, ctor) in scripts::SCRIPT_REGISTRY.iter().copied().filter(|(hash, _)| keep_script(*hash)) {{
212 c.bench_function(&format!("script/{{hash}}/ctor_state"), |b| {{
213 b.iter(|| {{
214 let behavior = behavior_from_ctor(ctor);
215 let state = behavior.create_state();
216 black_box(state);
217 }});
218 }});
219 }}
220}}
221
222fn bench_script_lifecycle(c: &mut Criterion) {{
223 for (hash, ctor) in scripts::SCRIPT_REGISTRY.iter().copied().filter(|(hash, _)| keep_script(*hash)) {{
224 c.bench_function(&format!("script/{{hash}}/lifecycle/update"), |b| {{
225 let behavior = behavior_from_ctor(ctor);
226 let mut runtime = Runtime::new();
227 let id = NodeAPI::create::<Node3D>(&mut runtime);
228 b.iter(|| runtime.bench_with_script_context(id, |ctx| behavior.on_update(ctx)));
229 }});
230 c.bench_function(&format!("script/{{hash}}/lifecycle/fixed_update"), |b| {{
231 let behavior = behavior_from_ctor(ctor);
232 let mut runtime = Runtime::new();
233 let id = NodeAPI::create::<Node3D>(&mut runtime);
234 b.iter(|| runtime.bench_with_script_context(id, |ctx| behavior.on_fixed_update(ctx)));

Callers 1

Calls 3

rust_str_arrayFunction · 0.85
normalize_toml_pathFunction · 0.70
workspace_rootFunction · 0.70

Tested by

no test coverage detected