Skip to content

Commit e5589e1

Browse files
committed
adding sliding window utility
1 parent 7dab9bb commit e5589e1

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

Assets/BossRoom/Scripts/Shared/Net/NetworkStats.cs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,35 @@ namespace Unity.Multiplayer.Samples.BossRoom
1616
[RequireComponent(typeof(NetworkObject))]
1717
public class NetworkStats : NetworkBehaviour
1818
{
19+
private class MovingWindowAverage
20+
{
21+
public float LastRTT { get; private set; }
22+
Queue<float> m_MovingWindow = new Queue<float>();
23+
const int k_MaxWindowSizeSeconds = 3; // it should take x seconds for the value to react to change
24+
float m_MaxWindowSize => k_MaxWindowSizeSeconds / m_PingIntervalSeconds;
25+
26+
public void Add(float value)
27+
{
28+
m_MovingWindow.Enqueue(value);
29+
UpdateRTTSlidingWindowAverage();
30+
}
31+
32+
void UpdateRTTSlidingWindowAverage()
33+
{
34+
if (m_MovingWindow.Count > m_MaxWindowSize)
35+
{
36+
m_MovingWindow.Dequeue();
37+
}
38+
39+
float rttSum = 0;
40+
foreach (var singleRTT in m_MovingWindow)
41+
{
42+
rttSum += singleRTT;
43+
}
44+
45+
LastRTT = rttSum / m_MaxWindowSize;
46+
}
47+
}
1948
// RTT
2049
// Client sends a ping RPC to the server and starts it's timer.
2150
// The server receives the ping and sends a pong response to the client.
@@ -25,21 +54,17 @@ public class NetworkStats : NetworkBehaviour
2554
// Note: when adding more stats, it might be worth it to abstract these in their own classes instead of having a bunch
2655
// of attributes floating around.
2756

28-
public float LastRTT { get; private set; }
57+
MovingWindowAverage BossRoomRTT = new MovingWindowAverage();
58+
MovingWindowAverage UTP_RTT = new MovingWindowAverage();
2959

30-
[SerializeField]
31-
[Tooltip("The interval to send ping RPCs to calculate the RTT. The bigger the number, the less reactive the stat will be to RTT changes")]
32-
float m_PingIntervalSeconds = 0.1f;
60+
const float m_PingIntervalSeconds = 0.1f;
3361
float m_LastPingTime;
3462
Text m_TextStat;
3563
Text m_TextHostType;
3664

3765
// When receiving pong client RPCs, we need to know when the initiating ping sent it so we can calculate its individual RTT
3866
int m_CurrentRTTPingId;
3967

40-
Queue<float> m_MovingWindow = new Queue<float>();
41-
const int k_MaxWindowSizeSeconds = 3; // it should take x seconds for the value to react to change
42-
float m_MaxWindowSize => k_MaxWindowSizeSeconds / m_PingIntervalSeconds;
4368
Dictionary<int, float> m_PingHistoryStartTimes = new Dictionary<int, float>();
4469

4570
ClientRpcParams m_PongClientParams;
@@ -99,12 +124,14 @@ void FixedUpdate()
99124
m_PingHistoryStartTimes[m_CurrentRTTPingId] = Time.realtimeSinceStartup;
100125
m_CurrentRTTPingId++;
101126
m_LastPingTime = Time.realtimeSinceStartup;
127+
128+
UTP_RTT.Add(NetworkManager.NetworkConfig.NetworkTransport.GetCurrentRtt(NetworkManager.ServerClientId));
102129
}
103130

104131
if (m_TextStat != null)
105132
{
106-
var transportRTT = NetworkManager.NetworkConfig.NetworkTransport.GetCurrentRtt(NetworkManager.ServerClientId);
107-
textToDisplay = $"{textToDisplay}RTT: {(LastRTT * 1000).ToString("0")} ms;\nUTP RTT {transportRTT.ToString("0")} ms";
133+
134+
textToDisplay = $"{textToDisplay}RTT: {(BossRoomRTT.LastRTT * 1000).ToString("0")} ms;\nUTP RTT {UTP_RTT.LastRTT.ToString("0")} ms";
108135
}
109136
}
110137

@@ -131,24 +158,7 @@ public void PongClientRPC(int pingId, ClientRpcParams clientParams = default)
131158
{
132159
var startTime = m_PingHistoryStartTimes[pingId];
133160
m_PingHistoryStartTimes.Remove(pingId);
134-
m_MovingWindow.Enqueue(Time.realtimeSinceStartup - startTime);
135-
UpdateRTTSlidingWindowAverage();
136-
}
137-
138-
void UpdateRTTSlidingWindowAverage()
139-
{
140-
if (m_MovingWindow.Count > m_MaxWindowSize)
141-
{
142-
m_MovingWindow.Dequeue();
143-
}
144-
145-
float rttSum = 0;
146-
foreach (var singleRTT in m_MovingWindow)
147-
{
148-
rttSum += singleRTT;
149-
}
150-
151-
LastRTT = rttSum / m_MaxWindowSize;
161+
BossRoomRTT.Add(Time.realtimeSinceStartup - startTime);
152162
}
153163

154164
public override void OnNetworkDespawn()

0 commit comments

Comments
 (0)