Skip to content

Commit 28f81d6

Browse files
fix: fixing the number of connections counter to be correctly named and function properly (#613)
* fix: fixing the number of connections counter to be correctly named and function properly * Make sure we clear the values if we retained any from a previous run
1 parent 9ffb544 commit 28f81d6

File tree

6 files changed

+77
-41
lines changed

6 files changed

+77
-41
lines changed

com.unity.multiplayer.mlapi/Editor/MLAPIProfilerModule.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal static class MLAPIProfilerModule
2121
[Serializable]
2222
private class MLAPIProfilerCounter
2323
{
24+
// Note: These fields are named this way for internal serialization
2425
public string m_Name;
2526
public string m_Category;
2627
}
@@ -31,6 +32,7 @@ private class MLAPIProfilerCounter
3132
[Serializable]
3233
private class MLAPIProfilerModuleData
3334
{
35+
// Note: These fields are named this way for internal serialization
3436
public List<MLAPIProfilerCounter> m_ChartCounters = new List<MLAPIProfilerCounter>();
3537
public List<MLAPIProfilerCounter> m_DetailCounters = new List<MLAPIProfilerCounter>();
3638
public string m_Name;
@@ -39,6 +41,7 @@ private class MLAPIProfilerModuleData
3941
[Serializable]
4042
private class MLAPIModules
4143
{
44+
// Note: These fields are named this way for internal serialization
4245
public List<MLAPIProfilerModuleData> m_Modules;
4346
}
4447

@@ -55,7 +58,7 @@ private class MLAPIModules
5558

5659
private static List<MLAPIProfilerCounter> CreateOperationsCounters() => new List<MLAPIProfilerCounter>()
5760
{
58-
new MLAPIProfilerCounter { m_Name = ProfilerConstants.NumberOfConnections, m_Category = ProfilerCategory.Network.Name },
61+
new MLAPIProfilerCounter { m_Name = ProfilerConstants.Connections, m_Category = ProfilerCategory.Network.Name },
5962
new MLAPIProfilerCounter { m_Name = ProfilerConstants.ReceiveTickRate, m_Category = ProfilerCategory.Network.Name },
6063
};
6164

@@ -75,10 +78,10 @@ private static bool CreateMLAPIDynamicModule(ref MLAPIModules mlapiModules, stri
7578
var module = mlapiModules.m_Modules.Find(x => x.m_Name == moduleName);
7679
if (module == null)
7780
{
78-
var newModule = new MLAPIProfilerModuleData();
79-
newModule.m_Name = moduleName;
80-
newModule.m_ChartCounters = counterListFactoryDelegate();
81-
newModule.m_DetailCounters = counterListFactoryDelegate();
81+
var newModule = new MLAPIProfilerModuleData
82+
{
83+
m_Name = moduleName, m_ChartCounters = counterListFactoryDelegate(), m_DetailCounters = counterListFactoryDelegate(),
84+
};
8285
mlapiModules.m_Modules.Add(newModule);
8386
return true;
8487
}
@@ -107,4 +110,4 @@ static MLAPIProfilerModule()
107110
#endif
108111
}
109112
}
110-
}
113+
}

com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ public void DisconnectClient(ulong clientId)
11981198
if (ConnectedClientsList[i].ClientId == clientId)
11991199
{
12001200
ConnectedClientsList.RemoveAt(i);
1201-
PerformanceDataManager.Increment(ProfilerConstants.NumberOfConnections, -1);
1201+
PerformanceDataManager.Increment(ProfilerConstants.Connections, -1);
12021202
ProfilerStatManager.Connections.Record(-1);
12031203
}
12041204
}
@@ -1263,7 +1263,7 @@ internal void OnClientDisconnectFromServer(ulong clientId)
12631263
if (ConnectedClientsList[i].ClientId == clientId)
12641264
{
12651265
ConnectedClientsList.RemoveAt(i);
1266-
PerformanceDataManager.Increment(ProfilerConstants.NumberOfConnections, -1);
1266+
PerformanceDataManager.Increment(ProfilerConstants.Connections, -1);
12671267
ProfilerStatManager.Connections.Record(-1);
12681268
break;
12691269
}
@@ -1307,7 +1307,7 @@ internal void HandleApproval(ulong clientId, bool createPlayerObject, ulong? pla
13071307
ConnectedClients.Add(clientId, client);
13081308
ConnectedClientsList.Add(client);
13091309

1310-
PerformanceDataManager.Increment(ProfilerConstants.NumberOfConnections);
1310+
PerformanceDataManager.Increment(ProfilerConstants.Connections);
13111311
ProfilerStatManager.Connections.Record();
13121312

13131313
// This packet is unreliable, but if it gets through it should provide a much better sync than the potentially huge approval message.

com.unity.multiplayer.mlapi/Runtime/Profiling/PerformanceTickData.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,10 @@ public int GetData(string fieldName)
2727
{
2828
return m_TickData.GetData(fieldName);
2929
}
30+
31+
public bool HasData(string fieldName)
32+
{
33+
return m_TickData.HasData(fieldName);
34+
}
3035
}
31-
}
36+
}

com.unity.multiplayer.mlapi/Runtime/Profiling/ProfilerConstants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace MLAPI.Profiling
22
{
33
public static class ProfilerConstants
44
{
5-
public const string NumberOfConnections = nameof(NumberOfConnections);
5+
public const string Connections = nameof(Connections);
66
public const string ReceiveTickRate = nameof(ReceiveTickRate);
77

88
public const string NumberOfNamedMessages = nameof(NumberOfNamedMessages);

com.unity.multiplayer.mlapi/Runtime/Profiling/ProfilerCountersInfo.cs

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,91 +10,119 @@ internal static class ProfilerCountersInfo
1010
#if UNITY_2020_2_OR_NEWER && ENABLE_PROFILER
1111
// Operations
1212
private static readonly ProfilerCounterValue<int> k_ConnectionsCounterValue =
13-
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfConnections,
13+
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.Connections,
1414
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
1515

1616
private static readonly ProfilerCounterValue<int> k_TickRateCounterValue =
1717
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.ReceiveTickRate,
18-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
18+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
1919

2020
// Messages
2121
private static readonly ProfilerCounterValue<int> k_NamedMessagesCounterValue =
2222
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfNamedMessages,
23-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
23+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
2424

2525
private static readonly ProfilerCounterValue<int> k_UnnamedMessagesCounterValue =
2626
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfUnnamedMessages,
27-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
27+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
2828

2929
private static readonly ProfilerCounterValue<int> k_BytesSentCounterValue =
3030
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberBytesSent,
31-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
31+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
3232

3333
private static readonly ProfilerCounterValue<int> k_BytesReceivedCounterValue =
3434
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberBytesReceived,
35-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
35+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
3636

3737
private static readonly ProfilerCounterValue<int> k_NetworkVarsCounterValue =
3838
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberNetworkVarsReceived,
39-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
39+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
4040

4141
// RPCs
4242
private static readonly ProfilerCounterValue<int> k_RPCsSentCounterValue =
4343
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfRPCsSent,
44-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
44+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
4545

4646
private static readonly ProfilerCounterValue<int> k_RPCsReceivedCounterValue =
4747
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfRPCsReceived,
48-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
48+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
4949

5050
private static readonly ProfilerCounterValue<int> k_RPCBatchesSentCounterValue =
5151
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfRPCBatchesSent,
52-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
52+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
5353

5454
private static readonly ProfilerCounterValue<int> k_RPCBatchesReceivedCounterValue =
5555
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfRPCBatchesReceived,
56-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
56+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
5757

5858
private static readonly ProfilerCounterValue<int> k_RPCQueueProcessedCounterValue =
5959
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfRPCQueueProcessed,
60-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
60+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
6161

6262
private static readonly ProfilerCounterValue<int> k_RPCsInQueueSizeCounterValue =
6363
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfRPCsInQueueSize,
64-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
64+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
6565

6666
private static readonly ProfilerCounterValue<int> k_RPCsOutQueueSizeCounterValue =
6767
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfRPCsOutQueueSize,
68-
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
68+
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);
6969

7070
[RuntimeInitializeOnLoadMethod]
7171
private static void RegisterMLAPIPerformanceEvent()
7272
{
73+
InitializeCounters();
7374
NetworkManager.OnPerformanceDataEvent += OnPerformanceTickData;
7475
}
7576

77+
private static void InitializeCounters()
78+
{
79+
k_ConnectionsCounterValue.Value = 0;
80+
k_TickRateCounterValue.Value = 0;
81+
82+
k_NamedMessagesCounterValue.Value = 0;
83+
k_UnnamedMessagesCounterValue.Value = 0;
84+
k_BytesSentCounterValue.Value = 0;
85+
k_BytesReceivedCounterValue.Value = 0;
86+
k_NetworkVarsCounterValue.Value = 0;
87+
88+
k_RPCsSentCounterValue.Value = 0;
89+
k_RPCsReceivedCounterValue.Value = 0;
90+
k_RPCBatchesSentCounterValue.Value = 0;
91+
k_RPCBatchesReceivedCounterValue.Value = 0;
92+
k_RPCQueueProcessedCounterValue.Value = 0;
93+
k_RPCsInQueueSizeCounterValue.Value = 0;
94+
k_RPCsOutQueueSizeCounterValue.Value = 0;
95+
}
96+
7697
private static void OnPerformanceTickData(PerformanceTickData tickData)
7798
{
7899
// Operations
79-
k_ConnectionsCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfConnections);
80-
k_TickRateCounterValue.Value = tickData.GetData(ProfilerConstants.ReceiveTickRate);
100+
UpdateIntCounter(tickData, k_ConnectionsCounterValue, ProfilerConstants.Connections);
101+
UpdateIntCounter(tickData, k_TickRateCounterValue, ProfilerConstants.ReceiveTickRate);
81102

82103
// Messages
83-
k_NamedMessagesCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfNamedMessages);
84-
k_UnnamedMessagesCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfUnnamedMessages);
85-
k_BytesSentCounterValue.Value = tickData.GetData(ProfilerConstants.NumberBytesSent);
86-
k_BytesReceivedCounterValue.Value = tickData.GetData(ProfilerConstants.NumberBytesReceived);
87-
k_NetworkVarsCounterValue.Value = tickData.GetData(ProfilerConstants.NumberNetworkVarsReceived);
104+
UpdateIntCounter(tickData, k_NamedMessagesCounterValue, ProfilerConstants.NumberOfNamedMessages);
105+
UpdateIntCounter(tickData, k_UnnamedMessagesCounterValue, ProfilerConstants.NumberOfUnnamedMessages);
106+
UpdateIntCounter(tickData, k_BytesSentCounterValue, ProfilerConstants.NumberBytesSent);
107+
UpdateIntCounter(tickData, k_BytesReceivedCounterValue, ProfilerConstants.NumberBytesReceived);
108+
UpdateIntCounter(tickData, k_NetworkVarsCounterValue, ProfilerConstants.NumberNetworkVarsReceived);
88109

89110
// RPCs
90-
k_RPCsSentCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCsSent);
91-
k_RPCsReceivedCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCsReceived);
92-
k_RPCBatchesSentCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCBatchesSent);
93-
k_RPCBatchesReceivedCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCBatchesReceived);
94-
k_RPCBatchesReceivedCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCBatchesReceived);
95-
k_RPCQueueProcessedCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCQueueProcessed);
96-
k_RPCsInQueueSizeCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCsInQueueSize);
97-
k_RPCsOutQueueSizeCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCsOutQueueSize);
111+
UpdateIntCounter(tickData, k_RPCsSentCounterValue, ProfilerConstants.NumberOfRPCsSent);
112+
UpdateIntCounter(tickData, k_RPCsReceivedCounterValue, ProfilerConstants.NumberOfRPCsReceived);
113+
UpdateIntCounter(tickData, k_RPCBatchesSentCounterValue, ProfilerConstants.NumberOfRPCBatchesSent);
114+
UpdateIntCounter(tickData, k_RPCBatchesReceivedCounterValue, ProfilerConstants.NumberOfRPCBatchesReceived);
115+
UpdateIntCounter(tickData, k_RPCBatchesReceivedCounterValue, ProfilerConstants.NumberOfRPCQueueProcessed);
116+
UpdateIntCounter(tickData, k_RPCQueueProcessedCounterValue, ProfilerConstants.NumberOfRPCsInQueueSize);
117+
UpdateIntCounter(tickData, k_RPCsInQueueSizeCounterValue, ProfilerConstants.NumberOfRPCsOutQueueSize);
118+
}
119+
120+
private static void UpdateIntCounter(PerformanceTickData tickData, ProfilerCounterValue<int> counter, string fieldName)
121+
{
122+
if (tickData.HasData(fieldName))
123+
{
124+
counter.Value += tickData.GetData(fieldName);
125+
}
98126
}
99127
#endif
100128
}

com.unity.multiplayer.mlapi/Runtime/Profiling/ProfilingDataStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ public IReadOnlyDictionary<string, int> GetReadonly()
3636
return m_Dictionary;
3737
}
3838
}
39-
}
39+
}

0 commit comments

Comments
 (0)