@@ -14,10 +14,12 @@ use bevy::{
14
14
event:: { Event , Events } ,
15
15
schedule:: { IntoSystemConfigs , SystemConfigs } ,
16
16
system:: { IntoSystem , Local , Res , Resource , SystemState } ,
17
- world:: { FromWorld , Mut } ,
17
+ world:: { Command , FromWorld , Mut } ,
18
18
} ,
19
+ log:: Level ,
19
20
prelude:: { Entity , World } ,
20
21
reflect:: { Reflect , TypeRegistry } ,
22
+ utils:: tracing,
21
23
} ;
22
24
use bevy_mod_scripting_core:: {
23
25
asset:: ScriptAsset ,
@@ -26,6 +28,7 @@ use bevy_mod_scripting_core::{
26
28
ReflectAccessId , WorldAccessGuard , WorldGuard ,
27
29
} ,
28
30
callback_labels,
31
+ commands:: CreateOrUpdateScript ,
29
32
error:: { InteropError , ScriptError } ,
30
33
event:: { IntoCallbackLabel , ScriptErrorEvent } ,
31
34
extractors:: { HandlerContext , WithWorldGuard } ,
@@ -68,6 +71,22 @@ impl<L: IntoCallbackLabel, P: IntoScriptPluginParams> TestCallbackBuilder<P, L>
68
71
}
69
72
}
70
73
74
+ pub fn install_test_plugin < P : IntoScriptPluginParams + Plugin > (
75
+ app : & mut bevy:: app:: App ,
76
+ plugin : P ,
77
+ include_test_functions : bool ,
78
+ ) {
79
+ app. add_plugins ( (
80
+ ScriptFunctionsPlugin ,
81
+ CoreScriptGlobalsPlugin :: default ( ) ,
82
+ BMSScriptingInfrastructurePlugin ,
83
+ plugin,
84
+ ) ) ;
85
+ if include_test_functions {
86
+ register_test_functions ( app) ;
87
+ }
88
+ }
89
+
71
90
#[ cfg( feature = "lua" ) ]
72
91
pub fn make_test_lua_plugin ( ) -> bevy_mod_scripting_lua:: LuaScriptingPlugin {
73
92
use bevy_mod_scripting_core:: { bindings:: WorldContainer , ConfigureScriptPlugin } ;
@@ -203,14 +222,7 @@ pub fn execute_integration_test<
203
222
204
223
let mut app = setup_integration_test ( init) ;
205
224
206
- app. add_plugins ( (
207
- ScriptFunctionsPlugin ,
208
- CoreScriptGlobalsPlugin :: default ( ) ,
209
- BMSScriptingInfrastructurePlugin ,
210
- plugin,
211
- ) ) ;
212
-
213
- register_test_functions ( & mut app) ;
225
+ install_test_plugin ( & mut app, plugin, true ) ;
214
226
215
227
app. add_event :: < TestEventFinished > ( ) ;
216
228
@@ -411,13 +423,7 @@ where
411
423
412
424
let mut app = setup_integration_test ( |_, _| { } ) ;
413
425
414
- app. add_plugins ( (
415
- ScriptFunctionsPlugin ,
416
- CoreScriptGlobalsPlugin :: default ( ) ,
417
- BMSScriptingInfrastructurePlugin ,
418
- plugin,
419
- ) ) ;
420
- register_test_functions ( & mut app) ;
426
+ install_test_plugin ( & mut app, plugin, true ) ;
421
427
422
428
let script_id = script_id. to_owned ( ) ;
423
429
let script_id_clone = script_id. clone ( ) ;
@@ -468,6 +474,55 @@ where
468
474
}
469
475
}
470
476
477
+ pub fn run_plugin_script_load_benchmark <
478
+ P : IntoScriptPluginParams + Plugin + FromWorld ,
479
+ M : Measurement ,
480
+ > (
481
+ plugin : P ,
482
+ benchmark_id : & str ,
483
+ content : & str ,
484
+ criterion : & mut criterion:: BenchmarkGroup < M > ,
485
+ script_id_generator : impl Fn ( u64 ) -> String ,
486
+ reload_probability : f32 ,
487
+ ) {
488
+ let mut app = setup_integration_test ( |_, _| { } ) ;
489
+ install_test_plugin ( & mut app, plugin, false ) ;
490
+ let mut rng_guard = RNG . lock ( ) . unwrap ( ) ;
491
+ * rng_guard = rand_chacha:: ChaCha12Rng :: from_seed ( [ 42u8 ; 32 ] ) ;
492
+ drop ( rng_guard) ;
493
+ criterion. bench_function ( benchmark_id, |c| {
494
+ c. iter_batched (
495
+ || {
496
+ let mut rng = RNG . lock ( ) . unwrap ( ) ;
497
+ let is_reload = rng. random_range ( 0f32 ..=1f32 ) < reload_probability;
498
+ let random_id = if is_reload { 0 } else { rng. random :: < u64 > ( ) } ;
499
+
500
+ let random_script_id = script_id_generator ( random_id) ;
501
+ // we manually load the script inside a command
502
+ let content = content. to_string ( ) . into_boxed_str ( ) ;
503
+ (
504
+ CreateOrUpdateScript :: < P > :: new (
505
+ random_script_id. into ( ) ,
506
+ content. clone ( ) . into ( ) ,
507
+ None ,
508
+ ) ,
509
+ is_reload,
510
+ )
511
+ } ,
512
+ |( command, _is_reload) | {
513
+ tracing:: event!(
514
+ Level :: TRACE ,
515
+ "profiling_iter {} is reload?: {}" ,
516
+ benchmark_id,
517
+ _is_reload
518
+ ) ;
519
+ command. apply ( app. world_mut ( ) ) ;
520
+ } ,
521
+ BatchSize :: LargeInput ,
522
+ ) ;
523
+ } ) ;
524
+ }
525
+
471
526
pub fn perform_benchmark_with_generator <
472
527
M : Measurement ,
473
528
I ,
0 commit comments