(filters: &[String], methods: &[String], vars: &[String])
| 175 | } |
| 176 | |
| 177 | fn 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}}; |
| 184 | use perro_api::prelude::*; |
| 185 | use perro_runtime::Runtime; |
| 186 | use perro_api::runtime_api::sub_apis::NodeAPI; |
| 187 | use perro_api::scripting::ScriptBehavior; |
| 188 | use perro_api::variant::Variant; |
| 189 | |
| 190 | const SCRIPT_FILTERS: &[&str] = {filters}; |
| 191 | const METHODS: &[&str] = {methods}; |
| 192 | const VARS: &[&str] = {vars}; |
| 193 | |
| 194 | fn 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 | |
| 201 | fn 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 | |
| 210 | fn 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 | |
| 222 | fn 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))); |
no test coverage detected