Skip to content

Commit 7a4d187

Browse files
committed
Added NetworkProfiler
1 parent a44f939 commit 7a4d187

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace MLAPI.Data.NetworkProfiler
5+
{
6+
public static class NetworkProfiler
7+
{
8+
const int tickCount = 1024;
9+
public static readonly FixedQueue<ProfilerTick> Ticks = new FixedQueue<ProfilerTick>(tickCount);
10+
private static ProfilerTick CurrentTick;
11+
12+
internal static void StartTick(TickType type)
13+
{
14+
if (Ticks.Count == tickCount)
15+
Ticks.Dequeue();
16+
17+
ProfilerTick tick = new ProfilerTick()
18+
{
19+
StartTime = Time.unscaledTime,
20+
EndTime = -1,
21+
Type = type
22+
};
23+
Ticks.Enqueue(tick);
24+
CurrentTick = tick;
25+
}
26+
27+
internal static void EndTick()
28+
{
29+
if (CurrentTick == null)
30+
return;
31+
CurrentTick.EndTime = Time.unscaledTime;
32+
CurrentTick = null;
33+
}
34+
35+
internal static void StartEvent(TickType eventType, uint bytes, string channelName, string messageType)
36+
{
37+
if (CurrentTick == null)
38+
return;
39+
CurrentTick.StartEvent(eventType, bytes, channelName, messageType);
40+
}
41+
42+
internal static void EndEvent()
43+
{
44+
if (CurrentTick == null)
45+
return;
46+
CurrentTick.EndEvent();
47+
}
48+
}
49+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace MLAPI.Data.NetworkProfiler
6+
{
7+
public enum TickType
8+
{
9+
Event,
10+
Receive,
11+
Send
12+
}
13+
public class ProfilerTick
14+
{
15+
private readonly List<TickEvent> Events = new List<TickEvent>();
16+
17+
internal void EndEvent()
18+
{
19+
for (int i = Events.Count - 1; i >= 0; i--)
20+
{
21+
if (Events[i].EndTime == -1)
22+
{
23+
Events[i].EndTime = Time.unscaledTime;
24+
return;
25+
}
26+
}
27+
}
28+
29+
internal void StartEvent(TickType type, uint bytes, string channelName, string messageType)
30+
{
31+
TickEvent tickEvent = new TickEvent()
32+
{
33+
Bytes = bytes,
34+
ChannelName = string.IsNullOrEmpty(channelName) ? "NONE" : channelName,
35+
MessageType = string.IsNullOrEmpty(messageType) ? "NONE" : messageType,
36+
EndTime = -1,
37+
StartTime = Time.unscaledTime,
38+
EventType = type
39+
};
40+
Events.Add(tickEvent);
41+
}
42+
43+
public TickType Type;
44+
public float StartTime;
45+
public float EndTime;
46+
}
47+
48+
public class TickEvent
49+
{
50+
public TickType EventType;
51+
public uint Bytes;
52+
public string ChannelName;
53+
public string MessageType;
54+
public float StartTime = -1;
55+
public float EndTime = -1;
56+
}
57+
}

MLAPI/MLAPI.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,14 @@
129129
<Compile Include="NetworkingManagerComponents\Core\InternalMessageHandler.Send.cs" />
130130
<Compile Include="NetworkingManagerComponents\Core\InternalMessageHandler.Receive.cs" />
131131
<Compile Include="NetworkingManagerComponents\Core\InternalMessageHandler.cs" />
132+
<Compile Include="Data\NetworkProfiler\ProfilerTickData.cs" />
133+
<Compile Include="Data\NetworkProfiler\NetworkProfiler.cs" />
132134
</ItemGroup>
133135
<ItemGroup>
134136
<None Include="packages.config" />
135137
</ItemGroup>
138+
<ItemGroup>
139+
<Folder Include="Data\NetworkProfiler\" />
140+
</ItemGroup>
136141
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
137-
</Project>
142+
</Project>

0 commit comments

Comments
 (0)