-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathMLAPIProfilerModule.cs
More file actions
112 lines (97 loc) · 5.25 KB
/
MLAPIProfilerModule.cs
File metadata and controls
112 lines (97 loc) · 5.25 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
using System;
using System.Collections.Generic;
using MLAPI.Profiling;
using Unity.Profiling;
using UnityEditor;
using UnityEngine;
namespace MLAPI
{
[InitializeOnLoad]
public static class MLAPIProfilerModule
{
#if UNITY_2020_2_OR_NEWER && ENABLE_PROFILER
const string RPCModuleName = "MLAPI RPCs";
const string OperationModuleName = "MLAPI Operations";
const string MessageModuleName = "MLAPI Messages";
/// <summary>
/// This needs to be in synced with the internal dynamic module structure to provide our own counters
/// </summary>
[Serializable]
public class MLAPIProfilerCounter
{
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]
public class MLAPIProfilerModuleData
{
public List<MLAPIProfilerCounter> m_ChartCounters = new List<MLAPIProfilerCounter>();
public List<MLAPIProfilerCounter> m_DetailCounters = new List<MLAPIProfilerCounter>();
public string m_Name;
}
[Serializable]
public class MLAPIModules
{
public List<MLAPIProfilerModuleData> m_Modules;
}
private static List<MLAPIProfilerCounter> CreateRPCCounters() => new List<MLAPIProfilerCounter>()
{
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberOfRPCsSent, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberOfRPCsReceived, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberOfRPCBatchesSent, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberOfRPCBatchesReceived, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberOfRPCQueueProcessed, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberOfRPCsInQueueSize, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberOfRPCsOutQueueSize, m_Category = ProfilerCategory.Network.Name },
};
private static List<MLAPIProfilerCounter> CreateOperationsCounters() => new List<MLAPIProfilerCounter>()
{
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberOfConnections, 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.NumberOfNamedMessages, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberOfUnnamedMessages, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberBytesSent, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberBytesReceived, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter() { m_Name = ProfilerConstants.NumberNetworkVarsReceived, 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();
newModule.m_Name = moduleName;
newModule.m_ChartCounters = counterListFactoryDelegate();
newModule.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, RPCModuleName, CreateRPCCounters);
wasCreated |= CreateMLAPIDynamicModule(ref dynamicModules, OperationModuleName, CreateOperationsCounters);
wasCreated |= CreateMLAPIDynamicModule(ref dynamicModules, MessageModuleName, CreateMessagesCounters);
if (wasCreated)
{
EditorPrefs.SetString("ProfilerWindow.DynamicModules", JsonUtility.ToJson(dynamicModules));
}
}
#endif
}
}
}