This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
NetStats.cs
208 lines (181 loc) · 7.34 KB
/
NetStats.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
namespace Improbable.Gdk.Core.NetworkStats
{
/// <summary>
/// Storage object for network data for a fixed number of frames.
/// </summary>
/// <remarks>
/// The underlying data is stored in a ring buffer.
/// </remarks>
public class NetStats
{
private readonly int sequenceLength;
private readonly Dictionary<MessageTypeUnion, DataSequence> Data = new Dictionary<MessageTypeUnion, DataSequence>();
private readonly float[] frameTimes;
private int nextFrameIndex;
/// <summary>
/// Creates an instance of network stats storage with a specific size.
/// </summary>
/// <param name="sequenceLength">
/// The maximum number of frames to store data for at any given time.
/// </param>
public NetStats(int sequenceLength)
{
this.sequenceLength = sequenceLength;
nextFrameIndex = sequenceLength - 1;
frameTimes = new float[sequenceLength];
}
/// <summary>
/// Sets the network statistics for a given direction for the current frame.
/// </summary>
/// <param name="frameData">The network statistics.</param>
/// <param name="direction">The direction of those statistics.</param>
public void SetFrameStats(NetFrameStats frameData, Direction direction)
{
// First we need to zero out the data before processing the _sparse_ data in the NetFrameStats.
foreach (var pair in Data)
{
pair.Value.Clear(nextFrameIndex, direction);
}
foreach (var pair in frameData.Messages)
{
if (!Data.TryGetValue(pair.Key, out var data))
{
data = new DataSequence(sequenceLength);
}
data.AddFrame(nextFrameIndex, pair.Value, direction);
Data[pair.Key] = data;
}
}
/// <summary>
/// Sets the frame time for the current frame.
/// </summary>
/// <param name="dt">The frame time.</param>
public void SetFrameTime(float dt)
{
frameTimes[nextFrameIndex] = dt;
}
/// <summary>
/// Finalize data capture for this frame.
/// </summary>
public void FinishFrame()
{
if (--nextFrameIndex < 0)
{
nextFrameIndex += sequenceLength;
}
}
/// <summary>
/// Retrieves summary statistics for a given message type for the last <see cref="numFrames"/> frames.
/// </summary>
/// <param name="messageType">The type of message to get summary statistics for.</param>
/// <param name="numFrames">The number of frames to query against.</param>
/// <param name="direction">The direction of the message type to get summary statistics for.</param>
/// <returns>
/// A tuple where:
/// - the first element is a summed <see cref="DataPoint"/> for the given message type over the last <see cref="numFrames"/> frames.
/// - the second element is the summed time that the last <see cref="numFrames"/> took.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <see cref="numFrames"/> is larger than the number of frames this stores.</exception>
public (DataPoint, float) GetSummary(MessageTypeUnion messageType, int numFrames, Direction direction)
{
if (numFrames > sequenceLength)
{
throw new ArgumentOutOfRangeException($"Cannot fetch {numFrames} worth of data. This instance can only store to up {sequenceLength} frames.");
}
float totalTime = 0.0f;
for (var i = 1; i <= numFrames; i++)
{
// Iterate through the ring buffer (can potentially wrap around).
var index = (nextFrameIndex + i) % sequenceLength;
totalTime += frameTimes[index];
}
if (!Data.TryGetValue(messageType, out var data))
{
return (new DataPoint(), totalTime);
}
DataPoint[] frameData;
switch (direction)
{
case Direction.Incoming:
frameData = data.Incoming;
break;
case Direction.Outgoing:
frameData = data.Outgoing;
break;
default:
throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
}
var summary = new DataPoint();
for (var i = 1; i <= numFrames; i++)
{
// Iterate through the ring buffer (can potentially wrap around).
var index = (nextFrameIndex + i) % sequenceLength;
summary += frameData[index];
}
return (summary, totalTime);
}
private struct DataSequence : IEquatable<DataSequence>
{
public readonly DataPoint[] Incoming;
public readonly DataPoint[] Outgoing;
public DataSequence(int size)
{
Incoming = new DataPoint[size];
Outgoing = new DataPoint[size];
}
public void AddFrame(int index, DataPoint data, Direction direction)
{
switch (direction)
{
case Direction.Incoming:
Incoming[index] = data;
break;
case Direction.Outgoing:
Outgoing[index] = data;
break;
default:
throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
}
}
public void Clear(int index, Direction direction)
{
switch (direction)
{
case Direction.Incoming:
Incoming[index] = new DataPoint();
break;
case Direction.Outgoing:
Outgoing[index] = new DataPoint();
break;
default:
throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
}
}
public bool Equals(DataSequence other)
{
return Equals(Incoming, other.Incoming) && Equals(Outgoing, other.Outgoing);
}
public override bool Equals(object obj)
{
return obj is DataSequence other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
return ((Incoming != null ? Incoming.GetHashCode() : 0) * 397) ^ (Outgoing != null ? Outgoing.GetHashCode() : 0);
}
}
public static bool operator ==(DataSequence left, DataSequence right)
{
return left.Equals(right);
}
public static bool operator !=(DataSequence left, DataSequence right)
{
return !left.Equals(right);
}
}
}
}