Skip to content

Commit a0e3aff

Browse files
committed
simplify load benchmark to fix problems
1 parent 634c069 commit a0e3aff

File tree

2 files changed

+15
-60
lines changed

2 files changed

+15
-60
lines changed

benches/benchmarks.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,22 +266,13 @@ fn conversion_benchmarks(criterion: &mut Criterion) {
266266

267267
fn script_load_benchmarks(criterion: &mut Criterion) {
268268
let mut group = criterion.benchmark_group("loading");
269-
let reload_probability = 0.5;
270269
// lua
271-
let plugin = make_test_lua_plugin();
272270
let content = include_str!("../assets/macro_benchmarks/loading/empty.lua");
273-
run_plugin_script_load_benchmark(plugin, "empty Lua", content, &mut group, reload_probability);
271+
run_plugin_script_load_benchmark(make_test_lua_plugin, "empty Lua", content, &mut group);
274272

275273
// rhai
276-
let plugin = make_test_rhai_plugin();
277274
let content = include_str!("../assets/macro_benchmarks/loading/empty.rhai");
278-
run_plugin_script_load_benchmark(
279-
plugin,
280-
"empty Rhai",
281-
content,
282-
&mut group,
283-
reload_probability,
284-
);
275+
run_plugin_script_load_benchmark(make_test_rhai_plugin, "empty Rhai", content, &mut group);
285276
}
286277

287278
pub fn benches() {

crates/testing_crates/script_integration_test_harness/src/lib.rs

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub mod test_functions;
44

55
use std::{
66
path::PathBuf,
7-
sync::Mutex,
87
time::{Duration, Instant},
98
};
109

@@ -21,9 +20,7 @@ use ::{
2120
},
2221
bevy_reflect::Reflect,
2322
};
24-
use bevy_app::PreUpdate;
2523
use bevy_asset::Assets;
26-
use bevy_ecs::world::World;
2724
use bevy_mod_scripting_asset::ScriptAsset;
2825
use bevy_mod_scripting_bindings::{
2926
CoreScriptGlobalsPlugin, ReflectAccessId, ThreadWorldContainer, WorldAccessGuard,
@@ -341,73 +338,40 @@ where
341338

342339
pub fn run_plugin_script_load_benchmark<
343340
P: IntoScriptPluginParams + Plugin + FromWorld,
341+
F: Fn() -> P,
344342
M: Measurement,
345343
>(
346-
plugin: P,
344+
plugin_maker: F,
347345
benchmark_id: &str,
348346
content: &str,
349347
criterion: &mut criterion::BenchmarkGroup<M>,
350-
reload_probability: f32,
351348
) {
352-
let mut app = setup_integration_test(|_, _| {});
353-
install_test_plugin(&mut app, false);
354-
app.add_plugins(plugin);
355349
let content_boxed = content.to_string().into_bytes().into_boxed_slice();
356-
let mut rng_guard = RNG.lock().unwrap();
357-
*rng_guard = rand_chacha::ChaCha12Rng::from_seed([42u8; 32]);
358-
drop(rng_guard);
359350

360-
let world_ptr = app.world_mut() as *mut World;
361-
let world_guard = Mutex::<()>::new(());
362351
criterion.bench_function(benchmark_id, |c| {
363352
c.iter_batched(
364353
|| {
365-
let mut rng = RNG.lock().unwrap();
366-
let is_reload = rng.random_range(0f32..=1f32) < reload_probability;
354+
let mut app = setup_integration_test(|_, _| {});
355+
install_test_plugin(&mut app, false);
356+
app.add_plugins(plugin_maker());
367357

368-
// println!("Setup {idx}, {is_reload}");
369-
let guard = world_guard.lock().unwrap();
370358
// Safety: we claimed a unique guard, only code accessing this will need to do the same
371-
let world = unsafe { &mut *world_ptr };
359+
let world = app.world_mut();
372360
let mut assets = world.get_resource_or_init::<Assets<ScriptAsset>>();
373361

374-
let id = is_reload
375-
.then(|| assets.ids().next())
376-
.flatten()
377-
.and_then(|id| assets.get_strong_handle(id))
378-
.unwrap_or_else(|| {
379-
assets.add(ScriptAsset {
380-
content: content_boxed.clone(),
381-
language: P::LANGUAGE,
382-
})
383-
});
384-
// otherwise causes random overflows due to a u16 tracking strong handles
385-
386-
// also remove assets apart from the first two
387-
assets
388-
.ids()
389-
.skip(2)
390-
.collect::<Vec<_>>()
391-
.into_iter()
392-
.for_each(|asset| _ = assets.remove(asset));
393-
394-
// run track assets (lives in PreUpdate), so handles can be dropped internally
395-
world.run_schedule(PreUpdate);
396-
drop(guard);
362+
let id = assets.add(ScriptAsset {
363+
content: content_boxed.clone(),
364+
language: P::LANGUAGE,
365+
});
397366

398367
// We manually load the script inside a command.
399368
(
369+
app,
400370
AttachScript::<P>::new(ScriptAttachment::StaticScript(id)),
401-
is_reload,
402371
)
403372
},
404-
|(command, _is_reload)| {
405-
tracing::event!(
406-
Level::TRACE,
407-
"profiling_iter {} is reload?: {}",
408-
benchmark_id,
409-
_is_reload
410-
);
373+
|(mut app, command)| {
374+
tracing::event!(Level::TRACE, "profiling_iter {}", benchmark_id);
411375
command.apply(app.world_mut());
412376
},
413377
BatchSize::LargeInput,

0 commit comments

Comments
 (0)