diff options
| author | mat <git@matdoes.dev> | 2026-02-22 11:35:41 +0700 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2026-02-22 11:35:41 +0700 |
| commit | 2756eb419af2210eed3d0574e20a620918e4e577 (patch) | |
| tree | 0d209e70f65db16b7b982c3e941355589b7d5181 /azalea/examples/testbot | |
| parent | ca2fbe329b0879496cf12de4e85d81ca3aa3c351 (diff) | |
| download | azalea-drasl-2756eb419af2210eed3d0574e20a620918e4e577.tar.xz | |
optimizations at high entity counts
Diffstat (limited to 'azalea/examples/testbot')
| -rw-r--r-- | azalea/examples/testbot/main.rs | 2 | ||||
| -rw-r--r-- | azalea/examples/testbot/mspt.rs | 69 |
2 files changed, 71 insertions, 0 deletions
diff --git a/azalea/examples/testbot/main.rs b/azalea/examples/testbot/main.rs index c74dcee7..63d83c8a 100644 --- a/azalea/examples/testbot/main.rs +++ b/azalea/examples/testbot/main.rs @@ -24,6 +24,7 @@ mod commands; pub mod killaura; +pub mod mspt; use std::{env, process, sync::Arc, thread, time::Duration}; @@ -75,6 +76,7 @@ async fn main() -> AppExit { args, commands: Arc::new(commands), }) + // .add_plugins(mspt::MsptPlugin) .start(join_address) .await } diff --git a/azalea/examples/testbot/mspt.rs b/azalea/examples/testbot/mspt.rs new file mode 100644 index 00000000..b4931d71 --- /dev/null +++ b/azalea/examples/testbot/mspt.rs @@ -0,0 +1,69 @@ +//! A debugging plugin that logs the duration of ECS `Update`s every tick. + +use std::time::{Duration, Instant}; + +use azalea::prelude::Resource; +use bevy_app::Plugin; +use bevy_ecs::{schedule::IntoScheduleConfigs, system::ResMut}; + +pub struct MsptPlugin; +impl Plugin for MsptPlugin { + fn build(&self, app: &mut bevy_app::App) { + app.insert_resource(MsptData { + last_log_time: Instant::now(), + this_update_start_time: Instant::now(), + update_times: Vec::new(), + }) + .add_systems(bevy_app::PreUpdate, on_update_start) + .add_systems( + bevy_app::PostUpdate, + (on_update_end, log_mspt_stats).chain(), + ); + } +} + +#[derive(Resource)] +struct MsptData { + this_update_start_time: Instant, + last_log_time: Instant, + + update_times: Vec<Duration>, +} +fn log_mspt_stats(mut stats: ResMut<MsptData>) { + if stats.last_log_time.elapsed() < Duration::from_secs(1) { + return; + } + stats.last_log_time = Instant::now(); + + let mut fastest_update_duration = None; + let mut summed_update_durations = Duration::ZERO; + let mut num_updates = 0; + for update in stats.update_times.drain(..) { + summed_update_durations += update; + num_updates += 1; + let Some(fastest_update) = &mut fastest_update_duration else { + fastest_update_duration = Some(update); + continue; + }; + if update < *fastest_update { + *fastest_update = update; + } + } + + if num_updates > 0 + && let Some(fastest_update_duration) = fastest_update_duration + { + let avg_update_duration = summed_update_durations / num_updates; + + println!(); + println!("Average update duration: {avg_update_duration:?}"); + println!("Fastest update duration: {fastest_update_duration:?}"); + } +} +fn on_update_start(mut data: ResMut<MsptData>) { + data.this_update_start_time = Instant::now(); +} +fn on_update_end(mut data: ResMut<MsptData>) { + let elapsed = data.this_update_start_time.elapsed(); + data.update_times.push(elapsed); +} |
