From 4ddf1a2cc87af8e2ae14b064188d2cc098ffce5b Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 14 Oct 2024 12:12:06 +0200 Subject: [PATCH] Another fix for network profiler to properly handle stats updating when recording is disabled #2815 --- Source/Editor/Windows/Profiler/Network.cs | 48 +++++++++++-------- .../Editor/Windows/Profiler/ProfilerMode.cs | 7 +++ .../Editor/Windows/Profiler/ProfilerWindow.cs | 6 +++ 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Source/Editor/Windows/Profiler/Network.cs b/Source/Editor/Windows/Profiler/Network.cs index c2418f590..6e99d6338 100644 --- a/Source/Editor/Windows/Profiler/Network.cs +++ b/Source/Editor/Windows/Profiler/Network.cs @@ -44,7 +44,8 @@ namespace FlaxEditor.Windows.Profiler private readonly Table _tableRep; private List _tableRowsCache; private SamplesBuffer _events; - private NetworkDriverStats _prevStats; + private NetworkDriverStats _frameStats; + private NetworkDriverStats _prevTotalStats; private List _stats; public Network() @@ -109,34 +110,41 @@ namespace FlaxEditor.Windows.Profiler _dataReceivedRateChart.Clear(); _events?.Clear(); _stats?.Clear(); - _prevStats = new NetworkDriverStats(); + _frameStats = _prevTotalStats = new NetworkDriverStats(); + } + + /// + public override void UpdateStats() + { + // Gather peer stats + var peers = NetworkPeer.Peers; + var totalStats = new NetworkDriverStats(); + totalStats.RTT = Time.UnscaledGameTime; // Store sample time in RTT + foreach (var peer in peers) + { + var peerStats = peer.NetworkDriver.GetStats(); + totalStats.TotalDataSent += peerStats.TotalDataSent; + totalStats.TotalDataReceived += peerStats.TotalDataReceived; + } + var stats = totalStats; + stats.TotalDataSent = (uint)Mathf.Max((long)totalStats.TotalDataSent - (long)_prevTotalStats.TotalDataSent, 0); + stats.TotalDataReceived = (uint)Mathf.Max((long)totalStats.TotalDataReceived - (long)_prevTotalStats.TotalDataReceived, 0); + _frameStats = stats; + _prevTotalStats = totalStats; } /// public override void Update(ref SharedUpdateData sharedData) { - // Gather peer stats - var peers = NetworkPeer.Peers; - var thisStats = new NetworkDriverStats(); - thisStats.RTT = Time.UnscaledGameTime; // Store sample time in RTT - foreach (var peer in peers) - { - var peerStats = peer.NetworkDriver.GetStats(); - thisStats.TotalDataSent += peerStats.TotalDataSent; - thisStats.TotalDataReceived += peerStats.TotalDataReceived; - } - var stats = thisStats; - stats.TotalDataSent = (uint)Mathf.Max((long)thisStats.TotalDataSent - (long)_prevStats.TotalDataSent, 0); - stats.TotalDataReceived = (uint)Mathf.Max((long)thisStats.TotalDataReceived - (long)_prevStats.TotalDataReceived, 0); - _dataSentChart.AddSample(stats.TotalDataSent); - _dataReceivedChart.AddSample(stats.TotalDataReceived); - _prevStats = thisStats; + // Add this-frame stats + _dataSentChart.AddSample(_frameStats.TotalDataSent); + _dataReceivedChart.AddSample(_frameStats.TotalDataReceived); if (_stats == null) _stats = new List(); - _stats.Add(stats); + _stats.Add(_frameStats); // Remove all stats older than 1 second - while (_stats.Count > 0 && thisStats.RTT - _stats[0].RTT >= 1.0f) + while (_stats.Count > 0 && _frameStats.RTT - _stats[0].RTT >= 1.0f) _stats.RemoveAt(0); // Calculate average data rates (from last second) diff --git a/Source/Editor/Windows/Profiler/ProfilerMode.cs b/Source/Editor/Windows/Profiler/ProfilerMode.cs index 6aa9b8cf6..088edd558 100644 --- a/Source/Editor/Windows/Profiler/ProfilerMode.cs +++ b/Source/Editor/Windows/Profiler/ProfilerMode.cs @@ -120,6 +120,13 @@ namespace FlaxEditor.Windows.Profiler { } + /// + /// Tick called before updating any modes display. Can be used to update per-frame statistics data. + /// + public virtual void UpdateStats() + { + } + /// /// Updates the mode view. Called after init and on selected frame changed. /// diff --git a/Source/Editor/Windows/Profiler/ProfilerWindow.cs b/Source/Editor/Windows/Profiler/ProfilerWindow.cs index 0b4ac39b4..6b7a41db2 100644 --- a/Source/Editor/Windows/Profiler/ProfilerWindow.cs +++ b/Source/Editor/Windows/Profiler/ProfilerWindow.cs @@ -230,6 +230,12 @@ namespace FlaxEditor.Windows.Profiler /// public override void OnUpdate() { + for (int i = 0; i < _tabs.ChildrenCount; i++) + { + if (_tabs.Children[i] is ProfilerMode mode) + mode.UpdateStats(); + } + if (LiveRecording) { FlaxEngine.Profiler.BeginEvent("ProfilerWindow.OnUpdate");