Skip to content

feat: Add metrics for transport bytes sent and received #1104

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 8 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Code review fixes
  • Loading branch information
Benoit Doyon committed Aug 31, 2021
commit 33fd19069405985b09d52e2cb8fefa10de831b62
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,8 @@ private IEnumerator ApprovalTimeout(ulong clientId)
private void HandleRawTransportPoll(NetworkEvent networkEvent, ulong clientId, NetworkChannel networkChannel,
ArraySegment<byte> payload, float receiveTime)
{
NetworkMetrics.TrackTransportBytesReceived(payload.Count);

switch (networkEvent)
{
case NetworkEvent.Connect:
Expand Down Expand Up @@ -1220,7 +1222,6 @@ internal void HandleIncomingData(ulong clientId, NetworkChannel networkChannel,
#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_HandleIncomingData.Begin();
#endif
NetworkMetrics.TrackTransportBytesReceived(data.Count);

if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#if MULTIPLAYER_TOOLS
using System;
using System.Collections;
using System.IO;
using NUnit.Framework;
using Unity.Multiplayer.Tools.MetricTypes;
using Unity.Multiplayer.Tools.NetStats;
using Unity.Netcode.RuntimeTests.Metrics.Utlity;
using UnityEngine.TestTools;

namespace Unity.Netcode.RuntimeTests.Metrics
{
public class TransportBytesMetricsTests : SingleClientMetricTestBase
{
const long MessageOverhead = 9;

[UnityTest]
public IEnumerator TrackTotalNumberOfBytesSent()
{
var messageName = Guid.NewGuid().ToString();
using var memoryStream = new MemoryStream();
using var binaryWriter = new BinaryWriter(memoryStream);
binaryWriter.Write(messageName);

var observer = new TotalBytesObserver(ServerMetrics.Dispatcher, NetworkMetricTypes.TotalBytesSent);

Server.CustomMessagingManager.SendNamedMessage(messageName, Client.LocalClientId, memoryStream);

var nbFrames = 0;
while (!observer.Found || nbFrames < 10)
{
yield return null;
nbFrames++;
}

Assert.True(observer.Found);
Assert.AreEqual(messageName.Length + MessageOverhead, observer.Value);
}

[UnityTest]
public IEnumerator TrackTotalNumberOfBytesReceived()
{
var messageName = Guid.NewGuid().ToString();
using var memoryStream = new MemoryStream();
using var binaryWriter = new BinaryWriter(memoryStream);
binaryWriter.Write(messageName);

var observer = new TotalBytesObserver(ClientMetrics.Dispatcher, NetworkMetricTypes.TotalBytesReceived);

Server.CustomMessagingManager.SendNamedMessage(messageName, Client.LocalClientId, memoryStream);

var nbFrames = 0;
while (!observer.Found || nbFrames < 10)
{
yield return null;
nbFrames++;
}

Assert.True(observer.Found);
Assert.AreEqual(messageName.Length + MessageOverhead, observer.Value);
}

private class TotalBytesObserver : IMetricObserver
{
private readonly DirectionalMetricInfo m_MetricInfo;

public TotalBytesObserver(IMetricDispatcher dispatcher, DirectionalMetricInfo metricInfo)
{
m_MetricInfo = metricInfo;

dispatcher.RegisterObserver(this);
}

public bool Found { get; private set; }

public long Value { get; private set; }

public void Observe(MetricCollection collection)
{
if (collection.TryGetCounter(m_MetricInfo.Id, out var counter) && counter.Value > 0)
{
Found = true;
Value = counter.Value;
}
}
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.6",
"com.unity.multiplayer.tools": "0.0.1-preview.7",
"com.unity.multiplayer.transport.utp": "file:../../com.unity.multiplayer.transport.utp",
"com.unity.test-framework": "1.1.26",
"com.unity.modules.ai": "1.0.0",
Expand Down