This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
NetFrameStats.cs
184 lines (154 loc) · 6.51 KB
/
NetFrameStats.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
using System.Collections.Generic;
using System.Diagnostics;
using Improbable.Worker.CInterop;
namespace Improbable.Gdk.Core.NetworkStats
{
/// <summary>
/// Represents a single frame's data for either incoming or outgoing network messages.
/// </summary>
public class NetFrameStats
{
public readonly Dictionary<MessageTypeUnion, DataPoint> Messages = new Dictionary<MessageTypeUnion, DataPoint>();
internal TestDataInjector TestInjector => new TestDataInjector(this);
[Conditional("UNITY_EDITOR")]
public void AddUpdate(in ComponentUpdate update)
{
var messageType = MessageTypeUnion.Update(update.ComponentId);
var size = update.SchemaData.Value.GetFields().GetWriteBufferLength() +
update.SchemaData.Value.GetEvents().GetWriteBufferLength();
Messages.TryGetValue(messageType, out var metrics);
metrics.Count += 1;
metrics.Size += size;
Messages[messageType] = metrics;
}
[Conditional("UNITY_EDITOR")]
public void AddCommandRequest(in CommandRequest request)
{
var componentId = request.ComponentId;
var commandIndex = request.CommandIndex;
var messageType = MessageTypeUnion.CommandRequest(componentId, commandIndex);
var size = request.SchemaData.Value.GetObject().GetWriteBufferLength();
Messages.TryGetValue(messageType, out var metrics);
metrics.Count += 1;
metrics.Size += size;
Messages[messageType] = metrics;
}
[Conditional("UNITY_EDITOR")]
public void AddCommandResponse(in CommandResponse response, string message)
{
var componentId = response.ComponentId;
var commandIndex = response.CommandIndex;
var messageType = MessageTypeUnion.CommandResponse(componentId, commandIndex);
uint size;
if (response.SchemaData.HasValue)
{
size = response.SchemaData.Value.GetObject().GetWriteBufferLength();
}
else
{
// Approximation of on-wire size.
size = (uint) System.Text.Encoding.UTF8.GetByteCount(message);
}
Messages.TryGetValue(messageType, out var metrics);
metrics.Count += 1;
metrics.Size += size;
Messages[messageType] = metrics;
}
[Conditional("UNITY_EDITOR")]
public void AddCommandResponse(string message, uint componentId, uint commandIndex)
{
var messageType = MessageTypeUnion.CommandResponse(componentId, commandIndex);
// Approximation of on-wire size.
var size = (uint) System.Text.Encoding.UTF8.GetByteCount(message);
Messages.TryGetValue(messageType, out var metrics);
metrics.Count += 1;
metrics.Size += size;
Messages[messageType] = metrics;
}
// Note we cannot measure the on-wire size of a world command, so we just count them.
[Conditional("UNITY_EDITOR")]
public void AddWorldCommandRequest(WorldCommand command)
{
var messageType = MessageTypeUnion.WorldCommandRequest(command);
Messages.TryGetValue(messageType, out var metrics);
metrics.Count += 1;
Messages[messageType] = metrics;
}
// Note we cannot measure the on-wire size of a world command, so we just count them.
[Conditional("UNITY_EDITOR")]
public void AddWorldCommandResponse(WorldCommand command)
{
var messageType = MessageTypeUnion.WorldCommandResponse(command);
Messages.TryGetValue(messageType, out var metrics);
metrics.Count += 1;
Messages[messageType] = metrics;
}
internal void CopyFrom(NetFrameStats other)
{
Clear();
foreach (var pair in other.Messages)
{
Messages[pair.Key] = pair.Value;
}
}
internal void Merge(NetFrameStats other)
{
foreach (var pair in other.Messages)
{
Messages.TryGetValue(pair.Key, out var data);
data += pair.Value;
Messages[pair.Key] = data;
}
}
internal void Clear()
{
Messages.Clear();
}
internal class TestDataInjector
{
private NetFrameStats target;
public TestDataInjector(NetFrameStats target)
{
this.target = target;
}
public void AddComponentUpdate(uint componentId, uint size)
{
var messageType = MessageTypeUnion.Update(componentId);
target.Messages.TryGetValue(messageType, out var metrics);
metrics.Count += 1;
metrics.Size += size;
target.Messages[messageType] = metrics;
}
public void AddCommandRequest(uint componentId, uint commandIndex, uint size)
{
var messageType = MessageTypeUnion.CommandRequest(componentId, commandIndex);
target.Messages.TryGetValue(messageType, out var metrics);
metrics.Count += 1;
metrics.Size += size;
target.Messages[messageType] = metrics;
}
public void AddCommandResponse(uint componentId, uint commandIndex, uint size)
{
var messageType = MessageTypeUnion.CommandResponse(componentId, commandIndex);
target.Messages.TryGetValue(messageType, out var metrics);
metrics.Count += 1;
metrics.Size += size;
target.Messages[messageType] = metrics;
}
public void AddWorldCommandRequest(WorldCommand worldCommand)
{
var messageType = MessageTypeUnion.WorldCommandRequest(worldCommand);
target.Messages.TryGetValue(messageType, out var metrics);
metrics.Count += 1;
target.Messages[messageType] = metrics;
}
public void AddWorldCommandResponse(WorldCommand worldCommand)
{
var messageType = MessageTypeUnion.WorldCommandResponse(worldCommand);
target.Messages.TryGetValue(messageType, out var metrics);
metrics.Count += 1;
target.Messages[messageType] = metrics;
}
}
}
}