Skip to content

perf: add script loading benchmark #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/macro_benchmarks/loading/empty.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function on_script_loaded()
end
3 changes: 3 additions & 0 deletions assets/macro_benchmarks/loading/empty.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn on_script_loaded(){

}
32 changes: 31 additions & 1 deletion benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use criterion::{BatchSize, BenchmarkFilter};
use regex::Regex;
use script_integration_test_harness::test_functions::rand::Rng;
use script_integration_test_harness::{
perform_benchmark_with_generator, run_lua_benchmark, run_rhai_benchmark,
make_test_lua_plugin, make_test_rhai_plugin, perform_benchmark_with_generator,
run_lua_benchmark, run_plugin_script_load_benchmark, run_rhai_benchmark,
};
use std::collections::HashMap;
use std::{path::PathBuf, sync::LazyLock, time::Duration};
Expand Down Expand Up @@ -241,6 +242,34 @@ fn conversion_benchmarks(criterion: &mut Criterion) {
);
}

fn script_load_benchmarks(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("loading");
let reload_probability = 0.5;
// lua
let plugin = make_test_lua_plugin();
let content = include_str!("../assets/macro_benchmarks/loading/empty.lua");
run_plugin_script_load_benchmark(
plugin,
"empty Lua",
content,
&mut group,
|rand| format!("{rand}.lua"),
reload_probability,
);

// rhai
let plugin = make_test_rhai_plugin();
let content = include_str!("../assets/macro_benchmarks/loading/empty.rhai");
run_plugin_script_load_benchmark(
plugin,
"empty Rhai",
content,
&mut group,
|rand| format!("{rand}.rhai"),
reload_probability,
);
}

pub fn benches() {
maybe_with_profiler(|_profiler| {
let mut criterion: criterion::Criterion<_> = (criterion::Criterion::default())
Expand All @@ -265,6 +294,7 @@ pub fn benches() {

script_benchmarks(&mut criterion, filter);
conversion_benchmarks(&mut criterion);
script_load_benchmarks(&mut criterion);
});
}
criterion_main!(benches);
87 changes: 71 additions & 16 deletions crates/testing_crates/script_integration_test_harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ use bevy::{
event::{Event, Events},
schedule::{IntoSystemConfigs, SystemConfigs},
system::{IntoSystem, Local, Res, Resource, SystemState},
world::{FromWorld, Mut},
world::{Command, FromWorld, Mut},
},
log::Level,
prelude::{Entity, World},
reflect::{Reflect, TypeRegistry},
utils::tracing,
};
use bevy_mod_scripting_core::{
asset::ScriptAsset,
Expand All @@ -26,6 +28,7 @@ use bevy_mod_scripting_core::{
ReflectAccessId, WorldAccessGuard, WorldGuard,
},
callback_labels,
commands::CreateOrUpdateScript,
error::{InteropError, ScriptError},
event::{IntoCallbackLabel, ScriptErrorEvent},
extractors::{HandlerContext, WithWorldGuard},
Expand Down Expand Up @@ -68,6 +71,22 @@ impl<L: IntoCallbackLabel, P: IntoScriptPluginParams> TestCallbackBuilder<P, L>
}
}

pub fn install_test_plugin<P: IntoScriptPluginParams + Plugin>(
app: &mut bevy::app::App,
plugin: P,
include_test_functions: bool,
) {
app.add_plugins((
ScriptFunctionsPlugin,
CoreScriptGlobalsPlugin::default(),
BMSScriptingInfrastructurePlugin,
plugin,
));
if include_test_functions {
register_test_functions(app);
}
}

#[cfg(feature = "lua")]
pub fn make_test_lua_plugin() -> bevy_mod_scripting_lua::LuaScriptingPlugin {
use bevy_mod_scripting_core::{bindings::WorldContainer, ConfigureScriptPlugin};
Expand Down Expand Up @@ -203,14 +222,7 @@ pub fn execute_integration_test<

let mut app = setup_integration_test(init);

app.add_plugins((
ScriptFunctionsPlugin,
CoreScriptGlobalsPlugin::default(),
BMSScriptingInfrastructurePlugin,
plugin,
));

register_test_functions(&mut app);
install_test_plugin(&mut app, plugin, true);

app.add_event::<TestEventFinished>();

Expand Down Expand Up @@ -411,13 +423,7 @@ where

let mut app = setup_integration_test(|_, _| {});

app.add_plugins((
ScriptFunctionsPlugin,
CoreScriptGlobalsPlugin::default(),
BMSScriptingInfrastructurePlugin,
plugin,
));
register_test_functions(&mut app);
install_test_plugin(&mut app, plugin, true);

let script_id = script_id.to_owned();
let script_id_clone = script_id.clone();
Expand Down Expand Up @@ -468,6 +474,55 @@ where
}
}

pub fn run_plugin_script_load_benchmark<
P: IntoScriptPluginParams + Plugin + FromWorld,
M: Measurement,
>(
plugin: P,
benchmark_id: &str,
content: &str,
criterion: &mut criterion::BenchmarkGroup<M>,
script_id_generator: impl Fn(u64) -> String,
reload_probability: f32,
) {
let mut app = setup_integration_test(|_, _| {});
install_test_plugin(&mut app, plugin, false);
let mut rng_guard = RNG.lock().unwrap();
*rng_guard = rand_chacha::ChaCha12Rng::from_seed([42u8; 32]);
drop(rng_guard);
criterion.bench_function(benchmark_id, |c| {
c.iter_batched(
|| {
let mut rng = RNG.lock().unwrap();
let is_reload = rng.random_range(0f32..=1f32) < reload_probability;
let random_id = if is_reload { 0 } else { rng.random::<u64>() };

let random_script_id = script_id_generator(random_id);
// we manually load the script inside a command
let content = content.to_string().into_boxed_str();
(
CreateOrUpdateScript::<P>::new(
random_script_id.into(),
content.clone().into(),
None,
),
is_reload,
)
},
|(command, _is_reload)| {
tracing::event!(
Level::TRACE,
"profiling_iter {} is reload?: {}",
benchmark_id,
_is_reload
);
command.apply(app.world_mut());
},
BatchSize::LargeInput,
);
});
}

pub fn perform_benchmark_with_generator<
M: Measurement,
I,
Expand Down
Loading
Loading