Skip to content

Commit

Permalink
Diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Veritius committed Apr 11, 2024
1 parent daff993 commit 5f3f5be
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
27 changes: 27 additions & 0 deletions stardust/src/diagnostics/connections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use bevy::{prelude::*, diagnostic::*};
use crate::prelude::*;

/// Adds diagnostics about various
pub struct ConnectionDiagnosticPlugin;

impl Plugin for ConnectionDiagnosticPlugin {
fn build(&self, app: &mut App) {
app.register_diagnostic(Diagnostic::new(Self::COUNT)
.with_smoothing_factor(0.0)
.with_max_history_length(1));

app.add_systems(Update, diagnostic_system);
}
}

impl ConnectionDiagnosticPlugin {
/// Diagnostic path for the amount of connections (entities with [`NetworkPeer`])
pub const COUNT: DiagnosticPath = DiagnosticPath::const_new("connection_count");
}

fn diagnostic_system(
mut diagnostics: Diagnostics,
query: Query<(), With<NetworkPeer>>,
) {
diagnostics.add_measurement(&ConnectionDiagnosticPlugin::COUNT, || query.iter().count() as f64);
}
45 changes: 45 additions & 0 deletions stardust/src/diagnostics/messages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use bevy::{prelude::*, diagnostic::*};
use crate::prelude::*;

/// Adds diagnostics about how many messages are being sent.
pub struct MessageCountDiagnosticsPlugin;

impl Plugin for MessageCountDiagnosticsPlugin {
fn build(&self, app: &mut App) {
app.register_diagnostic(Diagnostic::new(Self::INCOMING_COUNT));
app.register_diagnostic(Diagnostic::new(Self::OUTGOING_COUNT));

app.add_systems(Update, diagnostic_system);
}
}

impl MessageCountDiagnosticsPlugin {
/// The number of incoming messages in queues for all peers.
pub const INCOMING_COUNT: DiagnosticPath = DiagnosticPath::const_new("incoming_message_count");

/// The number of outgoing messages in queues for all peers.
pub const OUTGOING_COUNT: DiagnosticPath = DiagnosticPath::const_new("outgoing_message_count");
}

type QueryFilter = (Or<(With<NetworkMessages<Incoming>>, With<NetworkMessages<Outgoing>>)>, With<NetworkPeer>);

fn diagnostic_system(
mut diagnostics: Diagnostics,
query: Query<(Option<&NetworkMessages<Incoming>>, Option<&NetworkMessages<Outgoing>>), QueryFilter>,
) {
let mut incoming_count: usize = 0;
let mut outgoing_count: usize = 0;

for (incoming, outgoing) in query.iter() {
if let Some(incoming) = incoming {
incoming_count += incoming.count();
}

if let Some(outgoing) = outgoing {
outgoing_count += outgoing.count();
}
}

diagnostics.add_measurement(&MessageCountDiagnosticsPlugin::INCOMING_COUNT, || incoming_count as f64);
diagnostics.add_measurement(&MessageCountDiagnosticsPlugin::OUTGOING_COUNT, || outgoing_count as f64);
}
7 changes: 7 additions & 0 deletions stardust/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Diagnostics about various parts of Stardust.
mod connections;
mod messages;

pub use connections::*;
pub use messages::*;
1 change: 1 addition & 0 deletions stardust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

pub mod channels;
pub mod connections;
pub mod diagnostics;
pub mod messages;
pub mod plugin;
pub mod prelude;
Expand Down

0 comments on commit 5f3f5be

Please sign in to comment.