benchmark
This commit is contained in:
+138
-4
@@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user