Skip to content

Commit

Permalink
refactor: update NetworkMetrics to use strings instead of mirrored en…
Browse files Browse the repository at this point in the history
…um for scene event metrics (Unity-Technologies#1280)

* refactor: update to use strings instead of mirrored enum for scene event metrics

* update packages-lock.json

* Fixed typo when translating some of the event names

* revert burst change

Co-authored-by: Andrew Spiering <andrews@unity3d.com>
  • Loading branch information
becksebenius-unity and andrews-unity authored Oct 12, 2021
1 parent 3865de1 commit 3f298fd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 35 deletions.
27 changes: 24 additions & 3 deletions com.unity.netcode.gameobjects/Runtime/Metrics/NetworkMetrics.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
#if MULTIPLAYER_TOOLS
using System;
using System.Collections.Generic;
using Unity.Multiplayer.Tools;
using Unity.Multiplayer.Tools.MetricTypes;
using Unity.Multiplayer.Tools.NetStats;
using ToolsSceneEventType = Unity.Multiplayer.Tools.MetricTypes.SceneEventType;

namespace Unity.Netcode
{
internal class NetworkMetrics : INetworkMetrics
{
private static Dictionary<uint, string> s_SceneEventTypeNames;

static NetworkMetrics()
{
s_SceneEventTypeNames = new Dictionary<uint, string>();
foreach (SceneEventType type in Enum.GetValues(typeof(SceneEventType)))
{
s_SceneEventTypeNames[(uint)type] = type.ToString();
}
}

private static string GetSceneEventTypeName(uint typeCode)
{
if (!s_SceneEventTypeNames.TryGetValue(typeCode, out string name))
{
name = "Unknown";
}

return name;
}

private readonly Counter m_TransportBytesSent = new Counter(NetworkMetricTypes.TotalBytesSent.Id)
{
ShouldResetOnDispatch = true,
Expand Down Expand Up @@ -267,13 +288,13 @@ public void TrackSceneEventSent(IReadOnlyList<ulong> receiverClientIds, uint sce

public void TrackSceneEventSent(ulong receiverClientId, uint sceneEventType, string sceneName, long bytesCount)
{
m_SceneEventSentEvent.Mark(new SceneEventMetric(new ConnectionInfo(receiverClientId), (ToolsSceneEventType)sceneEventType, sceneName, bytesCount));
m_SceneEventSentEvent.Mark(new SceneEventMetric(new ConnectionInfo(receiverClientId), GetSceneEventTypeName(sceneEventType), sceneName, bytesCount));
MarkDirty();
}

public void TrackSceneEventReceived(ulong senderClientId, uint sceneEventType, string sceneName, long bytesCount)
{
m_SceneEventReceivedEvent.Mark(new SceneEventMetric(new ConnectionInfo(senderClientId), (ToolsSceneEventType)sceneEventType, sceneName, bytesCount));
m_SceneEventReceivedEvent.Mark(new SceneEventMetric(new ConnectionInfo(senderClientId), GetSceneEventTypeName(sceneEventType), sceneName, bytesCount));
MarkDirty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Unity.Netcode.RuntimeTests.Metrics.Utility;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
using ToolsSceneEventType = Unity.Multiplayer.Tools.MetricTypes.SceneEventType;
using SceneEventType = Unity.Netcode.SceneEventType;

namespace TestProject.ToolsIntegration.RuntimeTests
Expand Down Expand Up @@ -72,7 +71,7 @@ public IEnumerator TestS2CLoadSent()
Assert.AreEqual(1, sentMetrics.Count);

var sentMetric = sentMetrics.First();
Assert.AreEqual(ToolsSceneEventType.S2C_Load, sentMetric.SceneEventType);
Assert.AreEqual(SceneEventType.Load.ToString(), sentMetric.SceneEventType);
Assert.AreEqual(Client.LocalClientId, sentMetric.Connection.Id);
Assert.AreEqual(k_SimpleSceneName, sentMetric.SceneName);
}
Expand Down Expand Up @@ -108,7 +107,7 @@ public IEnumerator TestS2CLoadReceived()
Assert.AreEqual(1, receivedMetrics.Count);

var receivedMetric = receivedMetrics.First();
Assert.AreEqual(ToolsSceneEventType.S2C_Load, receivedMetric.SceneEventType);
Assert.AreEqual(SceneEventType.Load.ToString(), receivedMetric.SceneEventType);
Assert.AreEqual(Server.LocalClientId, receivedMetric.Connection.Id);
Assert.AreEqual(k_SimpleSceneName, receivedMetric.SceneName);
}
Expand Down Expand Up @@ -139,7 +138,7 @@ public IEnumerator TestC2SLoadCompleteSent()
Assert.AreEqual(1, sentMetrics.Count);

var sentMetric = sentMetrics.First();
Assert.AreEqual(ToolsSceneEventType.C2S_LoadComplete, sentMetric.SceneEventType);
Assert.AreEqual(SceneEventType.LoadComplete.ToString(), sentMetric.SceneEventType);
Assert.AreEqual(Server.LocalClientId, sentMetric.Connection.Id);
Assert.AreEqual(k_SimpleSceneName, sentMetric.SceneName);
}
Expand Down Expand Up @@ -170,7 +169,7 @@ public IEnumerator TestC2SLoadCompleteReceived()
Assert.AreEqual(1, receivedMetrics.Count);

var receivedMetric = receivedMetrics.First();
Assert.AreEqual(ToolsSceneEventType.C2S_LoadComplete, receivedMetric.SceneEventType);
Assert.AreEqual(SceneEventType.LoadComplete.ToString(), receivedMetric.SceneEventType);
Assert.AreEqual(Client.LocalClientId, receivedMetric.Connection.Id);
Assert.AreEqual(k_SimpleSceneName, receivedMetric.SceneName);
}
Expand All @@ -187,7 +186,7 @@ public IEnumerator TestS2CLoadCompleteSent()
var waitForSentMetric = new WaitForMetricValues<SceneEventMetric>(
ServerMetrics.Dispatcher,
NetworkMetricTypes.SceneEventSent,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.S2C_LoadComplete));
metric => metric.SceneEventType.Equals(SceneEventType.LoadEventCompleted.ToString()));

// Load a scene to trigger the messages
StartServerLoadScene();
Expand All @@ -203,7 +202,7 @@ public IEnumerator TestS2CLoadCompleteSent()
Assert.AreEqual(Server.ConnectedClients.Count, sentMetrics.Count);

var filteredSentMetrics = sentMetrics
.Where(metric => metric.SceneEventType == ToolsSceneEventType.S2C_LoadComplete)
.Where(metric => metric.SceneEventType == SceneEventType.LoadEventCompleted.ToString())
.Where(metric => metric.SceneName == k_SimpleSceneName);
CollectionAssert.AreEquivalent(filteredSentMetrics.Select(x => x.Connection.Id), Server.ConnectedClients.Select(x => x.Key));
}
Expand All @@ -220,7 +219,7 @@ public IEnumerator TestS2CLoadCompleteReceived()
var waitForReceivedMetric = new WaitForMetricValues<SceneEventMetric>(
ClientMetrics.Dispatcher,
NetworkMetricTypes.SceneEventReceived,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.S2C_LoadComplete));
metric => metric.SceneEventType.Equals(SceneEventType.LoadEventCompleted.ToString()));

// Load a scene to trigger the messages
StartServerLoadScene();
Expand All @@ -236,7 +235,7 @@ public IEnumerator TestS2CLoadCompleteReceived()
Assert.AreEqual(1, receivedMetrics.Count);
var receivedMetric = receivedMetrics.First();

Assert.AreEqual(ToolsSceneEventType.S2C_LoadComplete, receivedMetric.SceneEventType);
Assert.AreEqual(SceneEventType.LoadEventCompleted.ToString(), receivedMetric.SceneEventType);
Assert.AreEqual(Server.LocalClientId, receivedMetric.Connection.Id);
Assert.AreEqual(k_SimpleSceneName, receivedMetric.SceneName);
}
Expand All @@ -262,7 +261,7 @@ public IEnumerator TestS2CUnloadSent()
var waitForSentMetric = new WaitForMetricValues<SceneEventMetric>(
ServerMetrics.Dispatcher,
NetworkMetricTypes.SceneEventSent,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.S2C_Unload));
metric => metric.SceneEventType.Equals(SceneEventType.Unload.ToString()));

// Unload the scene to trigger the messages
StartServerUnloadScene();
Expand All @@ -278,7 +277,7 @@ public IEnumerator TestS2CUnloadSent()
Assert.AreEqual(1, sentMetrics.Count);

var sentMetric = sentMetrics.First();
Assert.AreEqual(ToolsSceneEventType.S2C_Unload, sentMetric.SceneEventType);
Assert.AreEqual(SceneEventType.Unload.ToString(), sentMetric.SceneEventType);
Assert.AreEqual(Client.LocalClientId, sentMetric.Connection.Id);
Assert.AreEqual(k_SimpleSceneName, sentMetric.SceneName);
}
Expand All @@ -305,7 +304,7 @@ public IEnumerator TestS2CUnloadReceived()
var waitForReceivedMetric = new WaitForMetricValues<SceneEventMetric>(
ClientMetrics.Dispatcher,
NetworkMetricTypes.SceneEventReceived,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.S2C_Unload));
metric => metric.SceneEventType.Equals(SceneEventType.Unload.ToString()));

// Unload the scene to trigger the messages
StartServerUnloadScene();
Expand All @@ -321,7 +320,7 @@ public IEnumerator TestS2CUnloadReceived()
Assert.AreEqual(1, receivedMetrics.Count);

var receivedMetric = receivedMetrics.First();
Assert.AreEqual(ToolsSceneEventType.S2C_Unload, receivedMetric.SceneEventType);
Assert.AreEqual(SceneEventType.Unload.ToString(), receivedMetric.SceneEventType);
Assert.AreEqual(Server.LocalClientId, receivedMetric.Connection.Id);
Assert.AreEqual(k_SimpleSceneName, receivedMetric.SceneName);
}
Expand All @@ -341,7 +340,7 @@ public IEnumerator TestC2SUnloadCompleteSent()
var waitForSentMetric = new WaitForMetricValues<SceneEventMetric>(
ClientMetrics.Dispatcher,
NetworkMetricTypes.SceneEventSent,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.C2S_UnloadComplete));
metric => metric.SceneEventType.Equals(SceneEventType.UnloadComplete.ToString()));

// Unload a scene to trigger the messages
StartServerUnloadScene();
Expand All @@ -357,7 +356,7 @@ public IEnumerator TestC2SUnloadCompleteSent()
Assert.AreEqual(1, sentMetrics.Count);

var sentMetric = sentMetrics.First();
Assert.AreEqual(ToolsSceneEventType.C2S_UnloadComplete, sentMetric.SceneEventType);
Assert.AreEqual(SceneEventType.UnloadComplete.ToString(), sentMetric.SceneEventType);
Assert.AreEqual(Server.LocalClientId, sentMetric.Connection.Id);
Assert.AreEqual(k_SimpleSceneName, sentMetric.SceneName);
}
Expand All @@ -377,7 +376,7 @@ public IEnumerator TestC2SUnloadCompleteReceived()
var waitForReceivedMetric = new WaitForMetricValues<SceneEventMetric>(
ServerMetrics.Dispatcher,
NetworkMetricTypes.SceneEventReceived,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.C2S_UnloadComplete));
metric => metric.SceneEventType.Equals(SceneEventType.UnloadComplete.ToString()));

// Unload a scene to trigger the messages
StartServerUnloadScene();
Expand All @@ -393,7 +392,7 @@ public IEnumerator TestC2SUnloadCompleteReceived()
Assert.AreEqual(1, receivedMetrics.Count);

var receivedMetric = receivedMetrics.First();
Assert.AreEqual(ToolsSceneEventType.C2S_UnloadComplete, receivedMetric.SceneEventType);
Assert.AreEqual(SceneEventType.UnloadComplete.ToString(), receivedMetric.SceneEventType);
Assert.AreEqual(Client.LocalClientId, receivedMetric.Connection.Id);
Assert.AreEqual(k_SimpleSceneName, receivedMetric.SceneName);
}
Expand All @@ -413,7 +412,7 @@ public IEnumerator TestS2CUnloadCompleteSent()
var waitForSentMetric = new WaitForMetricValues<SceneEventMetric>(
ServerMetrics.Dispatcher,
NetworkMetricTypes.SceneEventSent,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.S2C_UnLoadComplete));
metric => metric.SceneEventType.Equals(SceneEventType.UnloadEventCompleted.ToString()));

// Unload a scene to trigger the messages
StartServerUnloadScene();
Expand All @@ -432,7 +431,7 @@ public IEnumerator TestS2CUnloadCompleteSent()
// so iterate over the connected client list on the server to ensure that we have a 1-1 match of connected
// clients to sent metrics.
var filteredSentMetrics = sentMetrics
.Where(metric => metric.SceneEventType == ToolsSceneEventType.S2C_UnLoadComplete)
.Where(metric => metric.SceneEventType == SceneEventType.UnloadEventCompleted.ToString())
.Where(metric => metric.SceneName == k_SimpleSceneName);
CollectionAssert.AreEquivalent(filteredSentMetrics.Select(x => x.Connection.Id), Server.ConnectedClients.Select(x => x.Key));
}
Expand All @@ -452,7 +451,7 @@ public IEnumerator TestS2CUnloadCompleteReceived()
var waitForReceivedMetric = new WaitForMetricValues<SceneEventMetric>(
ClientMetrics.Dispatcher,
NetworkMetricTypes.SceneEventReceived,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.S2C_UnLoadComplete));
metric => metric.SceneEventType.Equals(SceneEventType.UnloadEventCompleted.ToString()));

// Unload the scene to trigger the messages
StartServerUnloadScene();
Expand All @@ -468,7 +467,7 @@ public IEnumerator TestS2CUnloadCompleteReceived()
Assert.AreEqual(1, receivedMetrics.Count);
var receivedMetric = receivedMetrics.First();

Assert.AreEqual(ToolsSceneEventType.S2C_UnLoadComplete, receivedMetric.SceneEventType);
Assert.AreEqual(SceneEventType.UnloadEventCompleted.ToString(), receivedMetric.SceneEventType);
Assert.AreEqual(Server.LocalClientId, receivedMetric.Connection.Id);
Assert.AreEqual(k_SimpleSceneName, receivedMetric.SceneName);
}
Expand All @@ -484,7 +483,7 @@ public IEnumerator TestS2CSyncSent()
var waitForSentMetric = new WaitForMetricValues<SceneEventMetric>(
ServerMetrics.Dispatcher,
NetworkMetricTypes.SceneEventSent,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.S2C_Sync));
metric => metric.SceneEventType.Equals(SceneEventType.Synchronize.ToString()));

// To trigger a sync, we need to connect a new client to an already started server, so create a client
var newClient = CreateAndStartClient();
Expand All @@ -502,7 +501,7 @@ public IEnumerator TestS2CSyncSent()

var sentMetric = sentMetrics.First();

Assert.AreEqual(ToolsSceneEventType.S2C_Sync, sentMetric.SceneEventType);
Assert.AreEqual(SceneEventType.Synchronize.ToString(), sentMetric.SceneEventType);
Assert.AreEqual(newClient.LocalClientId, sentMetric.Connection.Id);

MultiInstanceHelpers.StopOneClient(newClient);
Expand All @@ -520,7 +519,7 @@ public IEnumerator TestS2CSyncReceived()
var waitForReceivedMetric = new WaitForMetricValues<SceneEventMetric>(
newClientMetrics.Dispatcher,
NetworkMetricTypes.SceneEventReceived,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.S2C_Sync));
metric => metric.SceneEventType.Equals(SceneEventType.Synchronize.ToString()));

// Wait for the metric to be emitted when the message is received on the client from the server
yield return waitForReceivedMetric.WaitForMetricsReceived();
Expand All @@ -529,7 +528,7 @@ public IEnumerator TestS2CSyncReceived()
Assert.AreEqual(1, receivedMetrics.Count);
var receivedMetric = receivedMetrics.First();

Assert.AreEqual(ToolsSceneEventType.S2C_Sync, receivedMetric.SceneEventType);
Assert.AreEqual(SceneEventType.Synchronize.ToString(), receivedMetric.SceneEventType);
Assert.AreEqual(Server.LocalClientId, receivedMetric.Connection.Id);

MultiInstanceHelpers.StopOneClient(newClient);
Expand All @@ -547,7 +546,7 @@ public IEnumerator TestC2SSyncCompleteSent()
var waitForSentMetric = new WaitForMetricValues<SceneEventMetric>(
newClientMetrics.Dispatcher,
NetworkMetricTypes.SceneEventSent,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.C2S_SyncComplete));
metric => metric.SceneEventType.Equals(SceneEventType.SynchronizeComplete.ToString()));

// Wait for the metric to be emitted when the client has completed the sync locally and sends the message
// to the server
Expand All @@ -557,7 +556,7 @@ public IEnumerator TestC2SSyncCompleteSent()
Assert.AreEqual(1, sentMetrics.Count);
var sentMetric = sentMetrics.First();

Assert.AreEqual(ToolsSceneEventType.C2S_SyncComplete, sentMetric.SceneEventType);
Assert.AreEqual(SceneEventType.SynchronizeComplete.ToString(), sentMetric.SceneEventType);
Assert.AreEqual(Server.LocalClientId, sentMetric.Connection.Id);

MultiInstanceHelpers.StopOneClient(newClient);
Expand All @@ -569,7 +568,7 @@ public IEnumerator TestC2SSyncCompleteReceived()
var waitForReceivedMetric = new WaitForMetricValues<SceneEventMetric>(
ServerMetrics.Dispatcher,
NetworkMetricTypes.SceneEventReceived,
metric => metric.SceneEventType.Equals(ToolsSceneEventType.C2S_SyncComplete));
metric => metric.SceneEventType.Equals(SceneEventType.SynchronizeComplete.ToString()));

// To trigger a sync, we need to connect a new client to an already started server, so create a client
var newClient = CreateAndStartClient();
Expand All @@ -583,7 +582,7 @@ public IEnumerator TestC2SSyncCompleteReceived()

var receivedMetric = receivedMetrics.First();

Assert.AreEqual(ToolsSceneEventType.C2S_SyncComplete, receivedMetric.SceneEventType);
Assert.AreEqual(SceneEventType.SynchronizeComplete.ToString(), receivedMetric.SceneEventType);
Assert.AreEqual(newClient.LocalClientId, receivedMetric.Connection.Id);

MultiInstanceHelpers.StopOneClient(newClient);
Expand Down
2 changes: 1 addition & 1 deletion testproject-tools-integration/Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"dependencies": {
"com.unity.ide.rider": "3.0.7",
"com.unity.netcode.gameobjects": "file:../../com.unity.netcode.gameobjects",
"com.unity.multiplayer.tools": "0.0.1-preview.10",
"com.unity.multiplayer.tools": "0.0.1-preview.11",
"com.unity.netcode.adapter.utp": "file:../../com.unity.netcode.adapter.utp",
"com.unity.test-framework": "1.1.29",
"com.unity.modules.ai": "1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions testproject-tools-integration/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"url": "https://artifactory.prd.cds.internal.unity3d.com/artifactory/api/npm/upm-candidates"
},
"com.unity.multiplayer.tools": {
"version": "0.0.1-preview.9",
"version": "0.0.1-preview.11",
"depth": 0,
"source": "registry",
"dependencies": {
Expand All @@ -56,7 +56,7 @@
"depth": 0,
"source": "local",
"dependencies": {
"com.unity.netcode.gameobjects": "0.0.1-preview.1",
"com.unity.netcode.gameobjects": "0.2.0-preview.1",
"com.unity.transport": "1.0.0-pre.5"
}
},
Expand Down

0 comments on commit 3f298fd

Please sign in to comment.