Skip to content

fix: fixing the number of connections counter to be correctly named and function properly #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions com.unity.multiplayer.mlapi/Editor/MLAPIProfilerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal static class MLAPIProfilerModule
[Serializable]
private class MLAPIProfilerCounter
{
// Note: These fields are named this way for internal serialization
public string m_Name;
public string m_Category;
}
Expand All @@ -31,6 +32,7 @@ private class MLAPIProfilerCounter
[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;
Expand All @@ -39,6 +41,7 @@ private class MLAPIProfilerModuleData
[Serializable]
private class MLAPIModules
{
// Note: These fields are named this way for internal serialization
public List<MLAPIProfilerModuleData> m_Modules;
}

Expand All @@ -55,7 +58,7 @@ private class MLAPIModules

private static List<MLAPIProfilerCounter> CreateOperationsCounters() => new List<MLAPIProfilerCounter>()
{
new MLAPIProfilerCounter { m_Name = ProfilerConstants.NumberOfConnections, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.Connections, m_Category = ProfilerCategory.Network.Name },
new MLAPIProfilerCounter { m_Name = ProfilerConstants.ReceiveTickRate, m_Category = ProfilerCategory.Network.Name },
};

Expand All @@ -75,10 +78,10 @@ private static bool CreateMLAPIDynamicModule(ref MLAPIModules mlapiModules, stri
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();
var newModule = new MLAPIProfilerModuleData
{
m_Name = moduleName, m_ChartCounters = counterListFactoryDelegate(), m_DetailCounters = counterListFactoryDelegate(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit but I'd suggest the setters should be on their own lines

};
mlapiModules.m_Modules.Add(newModule);
return true;
}
Expand Down Expand Up @@ -107,4 +110,4 @@ static MLAPIProfilerModule()
#endif
}
}
}
}
6 changes: 3 additions & 3 deletions com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ public void DisconnectClient(ulong clientId)
if (ConnectedClientsList[i].ClientId == clientId)
{
ConnectedClientsList.RemoveAt(i);
PerformanceDataManager.Increment(ProfilerConstants.NumberOfConnections, -1);
PerformanceDataManager.Increment(ProfilerConstants.Connections, -1);
ProfilerStatManager.Connections.Record(-1);
}
}
Expand Down Expand Up @@ -1263,7 +1263,7 @@ internal void OnClientDisconnectFromServer(ulong clientId)
if (ConnectedClientsList[i].ClientId == clientId)
{
ConnectedClientsList.RemoveAt(i);
PerformanceDataManager.Increment(ProfilerConstants.NumberOfConnections, -1);
PerformanceDataManager.Increment(ProfilerConstants.Connections, -1);
ProfilerStatManager.Connections.Record(-1);
break;
}
Expand Down Expand Up @@ -1307,7 +1307,7 @@ internal void HandleApproval(ulong clientId, bool createPlayerObject, ulong? pla
ConnectedClients.Add(clientId, client);
ConnectedClientsList.Add(client);

PerformanceDataManager.Increment(ProfilerConstants.NumberOfConnections);
PerformanceDataManager.Increment(ProfilerConstants.Connections);
ProfilerStatManager.Connections.Record();

// This packet is unreliable, but if it gets through it should provide a much better sync than the potentially huge approval message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@ public int GetData(string fieldName)
{
return m_TickData.GetData(fieldName);
}

public bool HasData(string fieldName)
{
return m_TickData.HasData(fieldName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace MLAPI.Profiling
{
public static class ProfilerConstants
{
public const string NumberOfConnections = nameof(NumberOfConnections);
public const string Connections = nameof(Connections);
public const string ReceiveTickRate = nameof(ReceiveTickRate);

public const string NumberOfNamedMessages = nameof(NumberOfNamedMessages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,91 +10,119 @@ internal static class ProfilerCountersInfo
#if UNITY_2020_2_OR_NEWER && ENABLE_PROFILER
// Operations
private static readonly ProfilerCounterValue<int> k_ConnectionsCounterValue =
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfConnections,
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.Connections,
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);

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

// Messages
private static readonly ProfilerCounterValue<int> k_NamedMessagesCounterValue =
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfNamedMessages,
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);

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

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

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

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

// RPCs
private static readonly ProfilerCounterValue<int> k_RPCsSentCounterValue =
new ProfilerCounterValue<int>(ProfilerCategory.Network, ProfilerConstants.NumberOfRPCsSent,
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame);
ProfilerMarkerDataUnit.Count, ProfilerCounterOptions.FlushOnEndOfFrame | ProfilerCounterOptions.ResetToZeroOnFlush);

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

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

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

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

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

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

[RuntimeInitializeOnLoadMethod]
private static void RegisterMLAPIPerformanceEvent()
{
InitializeCounters();
NetworkManager.OnPerformanceDataEvent += OnPerformanceTickData;
}

private static void InitializeCounters()
{
k_ConnectionsCounterValue.Value = 0;
k_TickRateCounterValue.Value = 0;

k_NamedMessagesCounterValue.Value = 0;
k_UnnamedMessagesCounterValue.Value = 0;
k_BytesSentCounterValue.Value = 0;
k_BytesReceivedCounterValue.Value = 0;
k_NetworkVarsCounterValue.Value = 0;

k_RPCsSentCounterValue.Value = 0;
k_RPCsReceivedCounterValue.Value = 0;
k_RPCBatchesSentCounterValue.Value = 0;
k_RPCBatchesReceivedCounterValue.Value = 0;
k_RPCQueueProcessedCounterValue.Value = 0;
k_RPCsInQueueSizeCounterValue.Value = 0;
k_RPCsOutQueueSizeCounterValue.Value = 0;
}

private static void OnPerformanceTickData(PerformanceTickData tickData)
{
// Operations
k_ConnectionsCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfConnections);
k_TickRateCounterValue.Value = tickData.GetData(ProfilerConstants.ReceiveTickRate);
UpdateIntCounter(tickData, k_ConnectionsCounterValue, ProfilerConstants.Connections);
UpdateIntCounter(tickData, k_TickRateCounterValue, ProfilerConstants.ReceiveTickRate);

// Messages
k_NamedMessagesCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfNamedMessages);
k_UnnamedMessagesCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfUnnamedMessages);
k_BytesSentCounterValue.Value = tickData.GetData(ProfilerConstants.NumberBytesSent);
k_BytesReceivedCounterValue.Value = tickData.GetData(ProfilerConstants.NumberBytesReceived);
k_NetworkVarsCounterValue.Value = tickData.GetData(ProfilerConstants.NumberNetworkVarsReceived);
UpdateIntCounter(tickData, k_NamedMessagesCounterValue, ProfilerConstants.NumberOfNamedMessages);
UpdateIntCounter(tickData, k_UnnamedMessagesCounterValue, ProfilerConstants.NumberOfUnnamedMessages);
UpdateIntCounter(tickData, k_BytesSentCounterValue, ProfilerConstants.NumberBytesSent);
UpdateIntCounter(tickData, k_BytesReceivedCounterValue, ProfilerConstants.NumberBytesReceived);
UpdateIntCounter(tickData, k_NetworkVarsCounterValue, ProfilerConstants.NumberNetworkVarsReceived);

// RPCs
k_RPCsSentCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCsSent);
k_RPCsReceivedCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCsReceived);
k_RPCBatchesSentCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCBatchesSent);
k_RPCBatchesReceivedCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCBatchesReceived);
k_RPCBatchesReceivedCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCBatchesReceived);
k_RPCQueueProcessedCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCQueueProcessed);
k_RPCsInQueueSizeCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCsInQueueSize);
k_RPCsOutQueueSizeCounterValue.Value = tickData.GetData(ProfilerConstants.NumberOfRPCsOutQueueSize);
UpdateIntCounter(tickData, k_RPCsSentCounterValue, ProfilerConstants.NumberOfRPCsSent);
UpdateIntCounter(tickData, k_RPCsReceivedCounterValue, ProfilerConstants.NumberOfRPCsReceived);
UpdateIntCounter(tickData, k_RPCBatchesSentCounterValue, ProfilerConstants.NumberOfRPCBatchesSent);
UpdateIntCounter(tickData, k_RPCBatchesReceivedCounterValue, ProfilerConstants.NumberOfRPCBatchesReceived);
UpdateIntCounter(tickData, k_RPCBatchesReceivedCounterValue, ProfilerConstants.NumberOfRPCQueueProcessed);
UpdateIntCounter(tickData, k_RPCQueueProcessedCounterValue, ProfilerConstants.NumberOfRPCsInQueueSize);
UpdateIntCounter(tickData, k_RPCsInQueueSizeCounterValue, ProfilerConstants.NumberOfRPCsOutQueueSize);
}

private static void UpdateIntCounter(PerformanceTickData tickData, ProfilerCounterValue<int> counter, string fieldName)
{
if (tickData.HasData(fieldName))
{
counter.Value += tickData.GetData(fieldName);
}
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ public IReadOnlyDictionary<string, int> GetReadonly()
return m_Dictionary;
}
}
}
}