benchmark

This commit is contained in:
StephenAdamson
2025-02-06 21:32:30 +00:00
parent 6161c21e22
commit 4089851734
5 changed files with 246 additions and 28 deletions
+6 -3
View File
@@ -34,7 +34,10 @@ impl Citizen {
use crate::constants::TILE_SIZE;
use crate::tile::TileMap;
use rand::{seq::SliceRandom, Rng};
use rand::{
seq::{IndexedRandom, SliceRandom},
Rng,
};
use std::{
collections::{BinaryHeap, HashMap, HashSet},
process::exit,
@@ -83,7 +86,7 @@ pub fn update_citizen_wandering_targets(
tilemap: Res<TileMap>,
chunk_map: Res<ChunkMap>,
) {
let mut rng: rand::prelude::ThreadRng = rand::thread_rng();
let mut rng: rand::prelude::ThreadRng = rand::rng();
for (mut ambulatory, _) in query.iter_mut() {
if ambulatory.target.is_none()
@@ -337,7 +340,7 @@ fn reconstruct_path(came_from: HashMap<IVec3, IVec3>, mut current: IVec3) -> Vec
}
pub fn spawn_citizens(mut commands: Commands, asset_server: Res<AssetServer>) {
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
// Spawn a handful of citizens
for _ in 0..5 {
+138 -4
View File
@@ -1,4 +1,9 @@
use std::fs::File;
use bevy::prelude::*;
use bevy::window::WindowMode;
use std::io::Write;
use tile::CameraMoved;
mod camera;
mod citizen;
@@ -21,6 +26,8 @@ fn main() {
.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("Dorf"),
resizable: false,
mode: WindowMode::BorderlessFullscreen(MonitorSelection::Primary),
..Default::default()
}),
..Default::default()
@@ -42,7 +49,7 @@ fn main() {
.add_systems(
FixedUpdate,
(
camera::camera_movement,
// camera::camera_movement,
cursor::move_cursor,
// citizen::check_citizen_positions_for_chunks,
),
@@ -51,10 +58,20 @@ fn main() {
.add_systems(
Update,
(
tile::camera_z_movement,
// tile::camera_z_movement,
tile::update_tile_visibility
.run_if(|camera_moved: Res<tile::CameraMoved>| camera_moved.0),
// log_fps_system,
),
)
.init_resource::<DiagnosticsRecorder>()
.add_systems(
Update,
(
start_recording_system,
record_diagnostics_system,
tile::update_tile_visibility
.run_if(|camera_moved: Res<tile::CameraMoved>| camera_moved.0),
log_fps_system,
),
)
.run();
@@ -68,6 +85,123 @@ fn log_fps_system(diagnostics: Res<DiagnosticsStore>) {
.get(&FrameTimeDiagnosticsPlugin::FPS)
.and_then(|fps_diagnostic| fps_diagnostic.average())
{
println!("Average FPS: {:.2}", fps);
println!("Average FPS: {:?}", fps);
}
if let Some(instantaneous_fps) = diagnostics
.get(&FrameTimeDiagnosticsPlugin::FPS)
.and_then(|fps_diagnostic| fps_diagnostic.value())
{
println!("Instantaneous FPS: {:?}", instantaneous_fps);
}
if let Some(ft) = diagnostics
.get(&FrameTimeDiagnosticsPlugin::FRAME_TIME)
.and_then(|fps_diagnostic| fps_diagnostic.average())
{
println!("Average FT: {:?}", ft);
}
if let Some(instantaneous_ft) = diagnostics
.get(&FrameTimeDiagnosticsPlugin::FRAME_TIME)
.and_then(|fps_diagnostic| fps_diagnostic.value())
{
println!("Instantaneous FT: {:?}", instantaneous_ft);
}
}
#[derive(Resource)]
struct DiagnosticsRecorder {
start_time: Option<f64>,
data: Vec<(f64, f64, f64, f64, f64)>, // (time, avg_fps, instantaneous_fps, avg_frame_time, instantaneous_frame_time)
}
fn start_recording_system(time: Res<Time>, mut recorder: ResMut<DiagnosticsRecorder>) {
if recorder.start_time.is_none() {
recorder.start_time = Some(time.elapsed_secs_f64());
}
}
fn record_diagnostics_system(
time: Res<Time>,
diagnostics: Res<DiagnosticsStore>,
mut recorder: ResMut<DiagnosticsRecorder>,
mut z_index: ResMut<game::ZIndex>,
mut camera_moved: ResMut<CameraMoved>,
) {
if let Some(start_time) = recorder.start_time {
let elapsed = time.elapsed_secs_f64() - start_time;
camera_moved.0 = false;
// Set z_index to -3 at the start
if elapsed < 10.0 {
z_index.0 = -3.0;
camera_moved.0 = true;
}
// Increase z_index by +1 every 10 seconds
if elapsed >= 10.0 && elapsed % 10.0 < time.delta_secs_f64() {
z_index.0 += 1.0;
camera_moved.0 = true;
}
if elapsed >= 10.0 && elapsed <= 70.0 {
let avg_fps = diagnostics
.get(&FrameTimeDiagnosticsPlugin::FPS)
.and_then(|fps_diagnostic| fps_diagnostic.average())
.unwrap_or(0.0);
let instantaneous_fps = diagnostics
.get(&FrameTimeDiagnosticsPlugin::FPS)
.and_then(|fps_diagnostic| fps_diagnostic.value())
.unwrap_or(0.0);
let avg_frame_time = diagnostics
.get(&FrameTimeDiagnosticsPlugin::FRAME_TIME)
.and_then(|ft_diagnostic| ft_diagnostic.average())
.unwrap_or(0.0);
let instantaneous_frame_time = diagnostics
.get(&FrameTimeDiagnosticsPlugin::FRAME_TIME)
.and_then(|ft_diagnostic| ft_diagnostic.value())
.unwrap_or(0.0);
recorder.data.push((
elapsed,
avg_fps,
instantaneous_fps,
avg_frame_time,
instantaneous_frame_time,
));
} else if elapsed > 70.0 {
save_to_csv(&recorder.data);
std::process::exit(0);
}
}
}
fn save_to_csv(data: &[(f64, f64, f64, f64, f64)]) {
if let Ok(mut file) = File::create("diagnostics.csv") {
writeln!(
file,
"Time,Avg FPS,Instantaneous FPS,Avg Frame Time,Instantaneous Frame Time"
)
.unwrap();
for (time, avg_fps, instantaneous_fps, avg_frame_time, instantaneous_frame_time) in data {
writeln!(
file,
"{},{},{},{},{}",
time, avg_fps, instantaneous_fps, avg_frame_time, instantaneous_frame_time
)
.unwrap();
}
} else {
eprintln!("Failed to create diagnostics.csv");
}
}
impl Default for DiagnosticsRecorder {
fn default() -> Self {
DiagnosticsRecorder {
start_time: None,
data: Vec::new(),
}
}
}
+2 -2
View File
@@ -285,8 +285,8 @@ fn handle_chunk_loading(
fn setup_initial_chunks(mut event_writer: EventWriter<LoadChunkEvent>) {
println!("setup_initial_chunks");
for x in -6..=6 {
for y in -3..=3 {
for x in -7..=7 {
for y in -7..=7 {
event_writer.send(LoadChunkEvent {
chunk_position: IVec2::new(x, y),
});