@@ -10,18 +10,20 @@ use bevy::{
1010 app:: { Last , Plugin , PostUpdate , Startup , Update } ,
1111 asset:: { AssetServer , Handle } ,
1212 ecs:: {
13+ component:: Component ,
1314 event:: { Event , Events } ,
1415 schedule:: { IntoSystemConfigs , SystemConfigs } ,
15- system:: { IntoSystem , Local , Res , SystemState } ,
16+ system:: { IntoSystem , Local , Res , Resource , SystemState } ,
1617 world:: { FromWorld , Mut } ,
1718 } ,
1819 prelude:: { Entity , World } ,
19- reflect:: TypeRegistry ,
20+ reflect:: { Reflect , TypeRegistry } ,
2021} ;
2122use bevy_mod_scripting_core:: {
2223 asset:: ScriptAsset ,
2324 bindings:: {
24- pretty_print:: DisplayWithWorld , script_value:: ScriptValue , WorldAccessGuard , WorldGuard ,
25+ pretty_print:: DisplayWithWorld , script_value:: ScriptValue , ReflectAccessId ,
26+ WorldAccessGuard , WorldGuard ,
2527 } ,
2628 callback_labels,
2729 error:: { InteropError , ScriptError } ,
@@ -33,7 +35,7 @@ use bevy_mod_scripting_core::{
3335} ;
3436use bevy_mod_scripting_functions:: ScriptFunctionsPlugin ;
3537use criterion:: { measurement:: Measurement , BatchSize } ;
36- use rand:: SeedableRng ;
38+ use rand:: { Rng , SeedableRng } ;
3739use test_functions:: { register_test_functions, RNG } ;
3840use test_utils:: test_data:: setup_integration_test;
3941
@@ -468,7 +470,23 @@ pub fn perform_benchmark_with_generator<
468470 group : & mut criterion:: BenchmarkGroup < M > ,
469471 batch_size : BatchSize ,
470472) {
473+ #[ derive( Reflect , Component , Resource ) ]
474+ struct Fake1 ;
475+ #[ derive( Reflect , Component , Resource ) ]
476+ struct Fake2 ;
477+ #[ derive( Reflect , Resource ) ]
478+ struct Fake3 ;
479+ #[ derive( Reflect , Resource ) ]
480+ struct Fake4 ;
481+ #[ derive( Reflect , Resource ) ]
482+ struct Fake5 ;
483+
471484 let mut world = std:: mem:: take ( setup_integration_test ( |_, _| { } ) . world_mut ( ) ) ;
485+ let f1 = world. register_component :: < Fake1 > ( ) ;
486+ let f2 = world. register_component :: < Fake2 > ( ) ;
487+ let f3 = world. register_resource :: < Fake3 > ( ) ;
488+ let f4 = world. register_resource :: < Fake4 > ( ) ;
489+ let f5 = world. register_resource :: < Fake5 > ( ) ;
472490
473491 let world_guard = WorldAccessGuard :: new_exclusive ( & mut world) ;
474492 let mut rng_guard = RNG . lock ( ) . unwrap ( ) ;
@@ -484,6 +502,20 @@ pub fn perform_benchmark_with_generator<
484502 let mut allocator = allocator. write ( ) ;
485503 allocator. clean_garbage_allocations ( ) ;
486504 }
505+
506+ // lock a random amount of fake components/resources, to make benchmarks more natural
507+ for _ in 0 ..rng_guard. random_range ( 0 ..=5 ) {
508+ // pick random component
509+ match rng_guard. random_range ( 0 ..=4 ) {
510+ 0 => world_guard. claim_write_access ( ReflectAccessId :: for_component_id ( f1) ) ,
511+ 1 => world_guard. claim_write_access ( ReflectAccessId :: for_component_id ( f2) ) ,
512+ 2 => world_guard. claim_write_access ( ReflectAccessId :: for_component_id ( f3) ) ,
513+ 3 => world_guard. claim_write_access ( ReflectAccessId :: for_component_id ( f4) ) ,
514+ 4 => world_guard. claim_write_access ( ReflectAccessId :: for_component_id ( f5) ) ,
515+ _ => false ,
516+ } ;
517+ }
518+
487519 (
488520 generator ( & mut rng_guard, world_guard. clone ( ) ) ,
489521 world_guard. clone ( ) ,
0 commit comments