forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLAPIProfiler.cs
More file actions
361 lines (303 loc) · 14.1 KB
/
MLAPIProfiler.cs
File metadata and controls
361 lines (303 loc) · 14.1 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
354
355
356
357
358
359
360
361
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_NEWER
[MenuItem("Window/MLAPI Profiler")]
public static void ShowWindow()
{
GetWindow<MLAPIProfiler>();
}
#endif
private static GUIStyle s_WrapStyle
{
get
{
Color color = EditorStyles.label.normal.textColor;
GUIStyle style = EditorStyles.centeredGreyMiniLabel;
style.wordWrap = true;
style.normal.textColor = color;
return style;
}
}
private float m_HoverAlpha = 0f;
private float m_UpdateDelay = 1f;
private int m_CaptureCount = 100;
private float m_ShowMax = 0;
private float m_ShowMin = 0;
private AnimationCurve m_Curve = AnimationCurve.Linear(0, 0, 1, 0);
private readonly List<ProfilerTick> m_CurrentTicks = new List<ProfilerTick>();
private float m_LastDrawn = 0;
private class ProfilerContainer
{
public ProfilerTick[] Ticks;
public byte[] ToBytes()
{
var buffer = new NetworkBuffer();
var 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)
{
var container = new ProfilerContainer();
var buffer = new NetworkBuffer(bytes);
var reader = new NetworkReader(buffer);
var 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)
{
m_Curve = AnimationCurve.Constant(NetworkProfiler.Ticks.ElementAt(0).Frame, NetworkProfiler.Ticks.ElementAt(NetworkProfiler.Ticks.Count - 1).Frame, 0);
}
else
{
m_Curve = AnimationCurve.Constant(0, 1, 0);
}
m_LastDrawn = 0;
NetworkProfiler.Start(m_CaptureCount);
}
private void ClearDrawing()
{
m_CurrentTicks.Clear();
m_Curve = AnimationCurve.Constant(0, 1, 0);
m_LastDrawn = 0;
}
private void ChangeRecordState()
{
if (NetworkProfiler.IsRunning) StopRecording();
else StartRecording();
}
private TickEvent m_EventHover = null;
private double m_LastSetup = 0;
private void OnGUI()
{
bool recording = NetworkProfiler.IsRunning;
float deltaTime = (float)(EditorApplication.timeSinceStartup - m_LastSetup);
m_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))
{
var ticks = ProfilerContainer.FromBytes(File.ReadAllBytes(path)).Ticks;
if (ticks.Length >= 2)
{
m_Curve = AnimationCurve.Constant(ticks[0].EventId, ticks[(ticks.Length - 1)].EventId, 0);
m_ShowMax = ticks.Length;
m_ShowMin = ticks.Length - Mathf.Clamp(100, 0, ticks.Length);
}
else
{
m_Curve = AnimationCurve.Constant(0, 1, 0);
}
m_CurrentTicks.Clear();
for (int i = 0; i < ticks.Length; i++)
{
m_CurrentTicks.Add(ticks[i]);
uint bytes = 0;
if (ticks[i].Events.Count > 0)
{
for (int j = 0; j < ticks[i].Events.Count; j++)
{
var tickEvent = ticks[i].Events[j];
bytes += tickEvent.Bytes;
}
}
m_Curve.AddKey(ticks[i].EventId, bytes);
}
}
}
if (GUILayout.Button("Export datafile"))
{
int max = (int)m_ShowMax;
int min = (int)m_ShowMin;
int ticksInRange = max - min;
var ticks = new ProfilerTick[ticksInRange];
for (int i = min; i < max; i++) ticks[i - min] = m_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 = m_CaptureCount;
m_CaptureCount = EditorGUILayout.DelayedIntField("History count", m_CaptureCount);
if (m_CaptureCount <= 0) m_CaptureCount = 1;
m_UpdateDelay = EditorGUILayout.Slider("Refresh delay", m_UpdateDelay, 0.1f, 10f);
EditorGUILayout.EndVertical();
if (prevHis != m_CaptureCount) StartRecording();
//Cache
if (NetworkProfiler.IsRunning)
{
if (Time.unscaledTime - m_LastDrawn > m_UpdateDelay)
{
m_LastDrawn = Time.unscaledTime;
m_CurrentTicks.Clear();
if (NetworkProfiler.Ticks.Count >= 2)
{
m_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++)
{
var tick = NetworkProfiler.Ticks.ElementAt(i);
m_CurrentTicks.Add(tick);
uint bytes = 0;
if (tick.Events.Count > 0)
{
for (int j = 0; j < tick.Events.Count; j++)
{
var tickEvent = tick.Events[j];
bytes += tickEvent.Bytes;
}
}
m_Curve.AddKey(tick.EventId, bytes);
}
}
}
//Draw Animation curve and slider
m_Curve = EditorGUILayout.CurveField(m_Curve);
EditorGUILayout.MinMaxSlider(ref m_ShowMin, ref m_ShowMax, 0, m_CurrentTicks.Count);
//Verify slider values
if (m_ShowMin < 0) m_ShowMin = 0;
if (m_ShowMax > m_CurrentTicks.Count) m_ShowMax = m_CurrentTicks.Count;
if (m_ShowMin <= 0 && m_ShowMax <= 0)
{
m_ShowMin = 0;
m_ShowMax = m_CurrentTicks.Count;
}
//Draw main board
bool hover = false;
int nonEmptyTicks = 0;
int largestTickCount = 0;
int totalTicks = ((int)m_ShowMax - (int)m_ShowMin);
for (int i = (int)m_ShowMin; i < (int)m_ShowMax; i++)
{
if (m_CurrentTicks[i].Events.Count > 0) nonEmptyTicks++; //Count non empty ticks
if (m_CurrentTicks[i].Events.Count > largestTickCount) largestTickCount = m_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)m_ShowMin; i < (int)m_ShowMax; i++)
{
var tick = m_CurrentTicks[i];
if (tick.Events.Count == 0 && i != totalTicks - 1)
{
emptyStreak++;
continue;
}
if (emptyStreak > 0 || i == totalTicks - 1)
{
var 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(), s_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++)
{
var tickEvent = tick.Events[j];
var dataRect = new Rect(currentX, currentY, widthPerTick, heightPerEvent);
if (dataRect.Contains(Event.current.mousePosition))
{
hover = true;
m_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(), s_WrapStyle);
EditorGUI.LabelField(new Rect(dataRect.x, dataRect.y + dataRect.height / 2, dataRect.width, dataRect.height / 2), tickEvent.Bytes + "B", s_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(), s_WrapStyle);
EditorGUI.LabelField(new Rect(currentX, 120, widthPerTick, 20), tick.Frame.ToString(), s_WrapStyle);
currentX += widthPerTick;
}
//Calculate alpha
if (hover)
{
m_HoverAlpha += deltaTime * 10f;
if (m_HoverAlpha > 1f) m_HoverAlpha = 1f;
else if (m_HoverAlpha < 0f) m_HoverAlpha = 0f;
}
else
{
m_HoverAlpha -= deltaTime * 10f;
if (m_HoverAlpha > 1f) m_HoverAlpha = 1f;
else if (m_HoverAlpha < 0f) m_HoverAlpha = 0f;
}
//Draw hover thingy
if (m_EventHover != null)
{
var rect = new Rect(Event.current.mousePosition, new Vector2(500, 100));
EditorGUI.DrawRect(rect, GetEditorColorWithAlpha(m_HoverAlpha));
float heightPerField = (rect.height - 5) / 4;
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + 5, rect.width, rect.height), "EventType: " + m_EventHover.EventType, GetStyleWithTextAlpha(EditorStyles.label, m_HoverAlpha));
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 1 + 5, rect.width, rect.height), "Size: " + m_EventHover.Bytes + "B", GetStyleWithTextAlpha(EditorStyles.label, m_HoverAlpha));
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 2 + 5, rect.width, rect.height), "Channel: " + m_EventHover.ChannelName, GetStyleWithTextAlpha(EditorStyles.label, m_HoverAlpha));
EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 3 + 5, rect.width, rect.height), "MessageType: " + m_EventHover.MessageType, GetStyleWithTextAlpha(EditorStyles.label, m_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 => EditorGUIUtility.isProSkin ? new Color32(56, 56, 56, 255) : new Color32(194, 194, 194, 255);
private Color GetEditorColorWithAlpha(float alpha) => 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;
}
}
}