forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLAPIProfilerModule.cs
More file actions
116 lines (103 loc) · 5.76 KB
/
MLAPIProfilerModule.cs
File metadata and controls
116 lines (103 loc) · 5.76 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
using System;
using System.Collections.Generic;
using MLAPI.Profiling;
using Unity.Profiling;
using UnityEditor;
using UnityEngine;
namespace MLAPI
{
[InitializeOnLoad]
internal static class MLAPIProfilerModule
{
#if UNITY_2020_2_OR_NEWER && ENABLE_PROFILER
private const string k_RpcModuleName = "MLAPI RPCs";
private const string k_OperationModuleName = "MLAPI Operations";
private const string k_MessageModuleName = "MLAPI Messages";
/// <summary>
/// This needs to be in synced with the internal dynamic module structure to provide our own counters
/// </summary>
[Serializable]
private class MLAPIProfilerCounter
{
// Note: These fields are named this way for internal serialization
public string m_Name;
public string m_Category;
}
/// <summary>
/// This needs to be in synced with the internal dynamic module structure to provide our own counters
/// </summary>
[Serializable]
private class MLAPIProfilerModuleData
{
// Note: These fields are named this way for internal serialization
public List<MLAPIProfilerCounter> m_ChartCounters = new List<MLAPIProfilerCounter>();
public List<MLAPIProfilerCounter> m_DetailCounters = new List<MLAPIProfilerCounter>();
public string m_Name;
}
[Serializable]
private class MLAPIModules
{
// Note: These fields are named this way for internal serialization
public List<MLAPIProfilerModuleData> m_Modules;
}
private static List<MLAPIProfilerCounter> CreateRPCCounters() => new List<MLAPIProfilerCounter>()
{
new MLAPIProfilerCounter { m_Name = ProfilerConstants.RpcSent, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.RpcReceived, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.RpcBatchesSent, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.RpcBatchesReceived, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.RpcQueueProcessed, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.RpcInQueueSize, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.RpcOutQueueSize, m_Category = ProfilerCategory.Network.Name },
};
private static List<MLAPIProfilerCounter> CreateOperationsCounters() => new List<MLAPIProfilerCounter>()
{
new MLAPIProfilerCounter { m_Name = ProfilerConstants.Connections, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.ReceiveTickRate, m_Category = ProfilerCategory.Network.Name },
};
private static List<MLAPIProfilerCounter> CreateMessagesCounters() => new List<MLAPIProfilerCounter>()
{
new MLAPIProfilerCounter { m_Name = ProfilerConstants.NamedMessageReceived, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.UnnamedMessageReceived, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.NamedMessageSent, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.UnnamedMessageSent, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.ByteSent, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.ByteReceived, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.NetworkVarUpdates, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.NetworkVarDeltas, m_Category = ProfilerCategory.Network.Name },
};
private delegate List<MLAPIProfilerCounter> CounterListFactoryDelegate();
private static bool CreateMLAPIDynamicModule(ref MLAPIModules mlapiModules, string moduleName, CounterListFactoryDelegate counterListFactoryDelegate)
{
var module = mlapiModules.m_Modules.Find(x => x.m_Name == moduleName);
if (module == null)
{
var newModule = new MLAPIProfilerModuleData
{
m_Name = moduleName, m_ChartCounters = counterListFactoryDelegate(), m_DetailCounters = counterListFactoryDelegate(),
};
mlapiModules.m_Modules.Add(newModule);
return true;
}
return false;
}
#endif
static MLAPIProfilerModule()
{
#if UNITY_2020_2_OR_NEWER && ENABLE_PROFILER
var dynamicModulesJson = EditorPrefs.GetString("ProfilerWindow.DynamicModules");
var dynamicModules = JsonUtility.FromJson<MLAPIModules>(dynamicModulesJson);
if (dynamicModules != null)
{
bool wasCreated = CreateMLAPIDynamicModule(ref dynamicModules, k_RpcModuleName, CreateRPCCounters);
wasCreated |= CreateMLAPIDynamicModule(ref dynamicModules, k_OperationModuleName, CreateOperationsCounters);
wasCreated |= CreateMLAPIDynamicModule(ref dynamicModules, k_MessageModuleName, CreateMessagesCounters);
if (wasCreated)
{
EditorPrefs.SetString("ProfilerWindow.DynamicModules", JsonUtility.ToJson(dynamicModules));
}
}
#endif
}
}
}