-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathMLAPIProfiler.cs
More file actions
353 lines (308 loc) · 13.7 KB
/
MLAPIProfiler.cs
File metadata and controls
353 lines (308 loc) · 13.7 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System.Collections.Generic;
using System.IO;
using MLAPI.Profiling;
using MLAPI.Serialization;
using UnityEngine;
namespace UnityEditor
{
public class MLAPIProfiler : EditorWindow
{
#if !UNITY_2020_2_OR_LATER
[MenuItem("Window/MLAPI Profiler")]
public static void ShowWindow()
{
GetWindow<MLAPIProfiler>();
}
#endif
GUIStyle wrapStyle
{
get
{
Color color = EditorStyles.label.normal.textColor;
GUIStyle style = EditorStyles.centeredGreyMiniLabel;
style.wordWrap = true;
style.normal.textColor = color;
return style;
}
}
float hoverAlpha = 0f;
float updateDelay = 1f;
int captureCount = 100;
float showMax = 0;
float showMin = 0;
AnimationCurve curve = AnimationCurve.Linear(0, 0, 1, 0);
readonly List<ProfilerTick> currentTicks = new List<ProfilerTick>();
float lastDrawn = 0;
class ProfilerContainer
{
public ProfilerTick[] ticks;
public byte[] ToBytes()
{
NetworkBuffer buffer = new NetworkBuffer();
NetworkWriter writer = new NetworkWriter(buffer);
writer.WriteUInt16Packed((ushort)ticks.Length);
for (int i = 0; i < ticks.Length; i++)
{
ticks[i].SerializeToStream(buffer);
}
return buffer.ToArray();
}
public static ProfilerContainer FromBytes(byte[] bytes)
{
ProfilerContainer container = new ProfilerContainer();
NetworkBuffer buffer = new NetworkBuffer(bytes);
NetworkReader reader = new NetworkReader(buffer);
ushort count = reader.ReadUInt16Packed();
container.ticks = new ProfilerTick[count];
for (int i = 0; i < count; i++)
{
container.ticks[i] = ProfilerTick.FromStream(buffer);
}
return container;
}
}
private void StopRecording()
{
NetworkProfiler.Stop();
}
private void StartRecording()
{
if (NetworkProfiler.IsRunning)
StopRecording();
if (NetworkProfiler.Ticks != null && NetworkProfiler.Ticks.Count >= 2)
curve = AnimationCurve.Constant(NetworkProfiler.Ticks.ElementAt(0).Frame, NetworkProfiler.Ticks.ElementAt(NetworkProfiler.Ticks.Count - 1).Frame, 0);
else
curve = AnimationCurve.Constant(0, 1, 0);
lastDrawn = 0;
NetworkProfiler.Start(captureCount);
}
private void ClearDrawing()
{
currentTicks.Clear();
curve = AnimationCurve.Constant(0, 1, 0);
lastDrawn = 0;
}
private void ChangeRecordState()
{
if (NetworkProfiler.IsRunning) StopRecording();
else StartRecording();
}
TickEvent eventHover = null;
double lastSetup = 0;
private void OnGUI()
{
bool recording = NetworkProfiler.IsRunning;
float deltaTime = (float)(EditorApplication.timeSinceStartup - lastSetup);
lastSetup = EditorApplication.timeSinceStartup;
//Draw top bar
EditorGUILayout.BeginVertical();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button(recording ? "Stop" : "Capture")) ChangeRecordState();
if (GUILayout.Button("Clear")) ClearDrawing();
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
if (GUILayout.Button("Import datafile"))
{
string path = EditorUtility.OpenFilePanel("Choose a NetworkProfiler file", "", "");
if (!string.IsNullOrEmpty(path))
{
ProfilerTick[] ticks = ProfilerContainer.FromBytes(File.ReadAllBytes(path)).ticks;
if (ticks.Length >= 2)
{
curve = AnimationCurve.Constant(ticks[0].EventId, ticks[(ticks.Length - 1)].EventId, 0);
showMax = ticks.Length;
showMin = ticks.Length - Mathf.Clamp(100, 0, ticks.Length);
}
else
curve = AnimationCurve.Constant(0, 1, 0);
currentTicks.Clear();
for (int i = 0; i < ticks.Length; i++)
{
currentTicks.Add(ticks[i]);
uint bytes = 0;
if (ticks[i].Events.Count > 0)
{
for (int j = 0; j < ticks[i].Events.Count; j++)
{
TickEvent tickEvent = ticks[i].Events[j];
bytes += tickEvent.Bytes;
}
}
curve.AddKey(ticks[i].EventId, bytes);
}
}
}
if (GUILayout.Button("Export datafile"))
{
int max = (int)showMax;
int min = (int)showMin;
int ticksInRange = max - min;
ProfilerTick[] ticks = new ProfilerTick[ticksInRange];
for (int i = min; i < max; i++) ticks[i - min] = currentTicks[i];
string path = EditorUtility.SaveFilePanel("Save NetworkProfiler data", "", "networkProfilerData", "");
if (!string.IsNullOrEmpty(path)) File.WriteAllBytes(path, new ProfilerContainer() { ticks = ticks }.ToBytes());
}
EditorGUILayout.EndHorizontal();
float prevHis = captureCount;
captureCount = EditorGUILayout.DelayedIntField("History count", captureCount);
if (captureCount <= 0)
captureCount = 1;
updateDelay = EditorGUILayout.Slider("Refresh delay", updateDelay, 0.1f, 10f);
EditorGUILayout.EndVertical();
if (prevHis != captureCount) StartRecording();
//Cache
if (NetworkProfiler.IsRunning)
{
if (Time.unscaledTime - lastDrawn > updateDelay)
{
lastDrawn = Time.unscaledTime;
currentTicks.Clear();
if (NetworkProfiler.Ticks.Count >= 2)
curve = AnimationCurve.Constant(NetworkProfiler.Ticks.ElementAt(0).EventId, NetworkProfiler.Ticks.ElementAt(NetworkProfiler.Ticks.Count - 1).EventId, 0);
for (int i = 0; i < NetworkProfiler.Ticks.Count; i++)
{
ProfilerTick tick = NetworkProfiler.Ticks.ElementAt(i);
currentTicks.Add(tick);
uint bytes = 0;
if (tick.Events.Count > 0)
{
for (int j = 0; j < tick.Events.Count; j++)
{
TickEvent tickEvent = tick.Events[j];
bytes += tickEvent.Bytes;
}
}
curve.AddKey(tick.EventId, bytes);
}
}
}
//Draw Animation curve and slider
curve = EditorGUILayout.CurveField(curve);
EditorGUILayout.MinMaxSlider(ref showMin, ref showMax, 0, currentTicks.Count);
//Verify slider values
if (showMin < 0)
showMin = 0;
if (showMax > currentTicks.Count)
showMax = currentTicks.Count;
if (showMin <= 0 && showMax <= 0)
{
showMin = 0;
showMax = currentTicks.Count;
}
//Draw main board
bool hover = false;
int nonEmptyTicks = 0;
int largestTickCount = 0;
int totalTicks = ((int)showMax - (int)showMin);
for (int i = (int)showMin; i < (int)showMax; i++)
{
if (currentTicks[i].Events.Count > 0) nonEmptyTicks++; //Count non empty ticks
if (currentTicks[i].Events.Count > largestTickCount) largestTickCount = currentTicks[i].Events.Count; //Get how many events the tick with most events has
}
int emptyTicks = totalTicks - nonEmptyTicks;
float equalWidth = position.width / totalTicks;
float propWidth = equalWidth * 0.3f;
float widthPerTick = ((position.width - emptyTicks * propWidth) / nonEmptyTicks);
float currentX = 0;
int emptyStreak = 0;
for (int i = (int)showMin; i < (int)showMax; i++)
{
ProfilerTick tick = currentTicks[i];
if (tick.Events.Count == 0 && i != totalTicks - 1)
{
emptyStreak++;
continue;
}
else if (emptyStreak > 0 || i == totalTicks - 1)
{
Rect dataRect = new Rect(currentX, 140f, propWidth * emptyStreak, position.height - 140f);
currentX += propWidth * emptyStreak;
if (emptyStreak >= 2) EditorGUI.LabelField(new Rect(dataRect.x, dataRect.y, dataRect.width, dataRect.height), emptyStreak.ToString(), wrapStyle);
emptyStreak = 0;
}
if (tick.Events.Count > 0)
{
float heightPerEvent = ((position.height - 140f) - (5f * largestTickCount)) / largestTickCount;
float currentY = 140f;
for (int j = 0; j < tick.Events.Count; j++)
{
TickEvent tickEvent = tick.Events[j];
Rect dataRect = new Rect(currentX, currentY, widthPerTick, heightPerEvent);
if (dataRect.Contains(Event.current.mousePosition))
{
hover = true;
eventHover = tickEvent;
}
EditorGUI.DrawRect(dataRect, TickTypeToColor(tickEvent.EventType, true));
EditorGUI.LabelField(new Rect(dataRect.x, dataRect.y, dataRect.width, dataRect.height / 2), tickEvent.EventType.ToString(), wrapStyle);
EditorGUI.LabelField(new Rect(dataRect.x, dataRect.y + dataRect.height / 2, dataRect.width, dataRect.height / 2), tickEvent.Bytes + "B", wrapStyle);
currentY += heightPerEvent + 5f;
}
}
EditorGUI.DrawRect(new Rect(currentX, 100, widthPerTick, 40), TickTypeToColor(tick.Type, false));
EditorGUI.LabelField(new Rect(currentX, 100, widthPerTick, 20), tick.Type.ToString(), wrapStyle);
EditorGUI.LabelField(new Rect(currentX, 120, widthPerTick, 20), tick.Frame.ToString(), wrapStyle);
currentX += widthPerTick;
}
//Calculate alpha
if (hover)
{
hoverAlpha += deltaTime * 10f;
if (hoverAlpha > 1f) hoverAlpha = 1f;
else if (hoverAlpha < 0f) hoverAlpha = 0f;
}
else
{
hoverAlpha -= deltaTime * 10f;
if (hoverAlpha > 1f) hoverAlpha = 1f;
else if (hoverAlpha < 0f) hoverAlpha = 0f;
}
//Draw hover thingy
if (eventHover != null)
{
Rect rect = new Rect(Event.current.mousePosition, new Vector2(500, 100));
EditorGUI.DrawRect(rect, GetEditorColorWithAlpha(hoverAlpha));
float heightPerField = (rect.height - 5) / 4;
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + 5, rect.width, rect.height), "EventType: " + eventHover.EventType.ToString(), GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha));
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 1 + 5, rect.width, rect.height), "Size: " + eventHover.Bytes + "B", GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha));
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 2 + 5, rect.width, rect.height), "Channel: " + eventHover.ChannelName, GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha));
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 3 + 5, rect.width, rect.height), "MessageType: " + eventHover.MessageType, GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha));
}
Repaint();
}
private Color TickTypeToColor(TickType type, bool alpha)
{
switch (type)
{
case TickType.Event:
return new Color(0.58f, 0f, 0.56f, alpha ? 0.37f : 0.7f);
case TickType.Receive:
return new Color(0f, 0.85f, 0.85f, alpha ? 0.28f : 0.7f);
case TickType.Send:
return new Color(0, 0.55f, 1f, alpha ? 0.06f : 0.7f);
default:
return Color.clear;
}
}
private Color EditorColor
{
get
{
return EditorGUIUtility.isProSkin ? new Color32(56, 56, 56, 255) : new Color32(194, 194, 194, 255);
}
}
private Color GetEditorColorWithAlpha(float alpha)
{
return EditorGUIUtility.isProSkin ? new Color(0.22f, 0.22f, 0.22f, alpha) : new Color(0.76f, 0.76f, 0.76f, alpha);
}
private GUIStyle GetStyleWithTextAlpha(GUIStyle style, float alpha)
{
Color textColor = style.normal.textColor;
textColor.a = alpha;
GUIStyle newStyle = new GUIStyle(style);
newStyle.normal.textColor = textColor;
return newStyle;
}
}
}