Skip to content

Commit 5984fc3

Browse files
simon-lemay-unityjakobbbb
authored andcommitted
fix: Rework packet loss metric tests to work with UTP 2.0 [MTT-4535] (Unity-Technologies#2174)
* Allow fixing the simulator RNG seed * Fix packet loss test for UTP 2.0 * Add comment about random seed in packet loss test
1 parent 354e373 commit 5984fc3

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ public struct SimulatorParameters
337337
PacketDropRate = 0
338338
};
339339

340+
internal uint DebugSimulatorRandomSeed { get; set; } = 0;
341+
340342
private struct PacketLossCache
341343
{
342344
public int PacketsReceived;
@@ -1322,7 +1324,8 @@ private void ConfigureSimulator()
13221324
maxPacketSize: NetworkParameterConstants.MTU,
13231325
packetDelayMs: DebugSimulator.PacketDelayMS,
13241326
packetJitterMs: DebugSimulator.PacketJitterMS,
1325-
packetDropPercentage: DebugSimulator.PacketDropRate
1327+
packetDropPercentage: DebugSimulator.PacketDropRate,
1328+
randomSeed: DebugSimulatorRandomSeed
13261329
#if UTP_TRANSPORT_2_0_ABOVE
13271330
, mode: ApplyMode.AllPackets
13281331
#endif

com.unity.netcode.gameobjects/Tests/Runtime/Metrics/PacketLossMetricsTests.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public class PacketLossMetricsTests : NetcodeIntegrationTest
1616
{
1717
protected override int NumberOfClients => 1;
1818
private readonly int m_PacketLossRate = 25;
19-
private readonly int m_PacketLossRangeDelta = 5;
19+
private readonly int m_PacketLossRangeDelta = 3;
20+
private readonly int m_MessageSize = 200;
2021

2122
public PacketLossMetricsTests()
2223
: base(HostOrServer.Server)
@@ -27,6 +28,12 @@ protected override void OnServerAndClientsCreated()
2728
var clientTransport = (UnityTransport)m_ClientNetworkManagers[0].NetworkConfig.NetworkTransport;
2829
clientTransport.SetDebugSimulatorParameters(0, 0, m_PacketLossRate);
2930

31+
// Determined through trial and error. With both UTP 1.2 and 2.0, this random seed
32+
// results in an effective packet loss percentage between 22% and 28%. Future UTP
33+
// updates may change the RNG call patterns and cause this test to fail, in which
34+
// case the value should be modified again.
35+
clientTransport.DebugSimulatorRandomSeed = 4;
36+
3037
base.OnServerAndClientsCreated();
3138
}
3239

@@ -39,8 +46,8 @@ public IEnumerator TrackPacketLossAsServer()
3946

4047
for (int i = 0; i < 1000; ++i)
4148
{
42-
using var writer = new FastBufferWriter(sizeof(byte), Allocator.Persistent);
43-
writer.WriteByteSafe(42);
49+
using var writer = new FastBufferWriter(m_MessageSize, Allocator.Persistent);
50+
writer.WriteBytesSafe(new byte[m_MessageSize]);
4451
m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage("Test", m_ServerNetworkManager.ConnectedClientsIds, writer);
4552
}
4653

@@ -51,9 +58,6 @@ public IEnumerator TrackPacketLossAsServer()
5158
}
5259

5360
[UnityTest]
54-
#if UTP_TRANSPORT_2_0_ABOVE
55-
[Ignore("Pending adjustment for UTP 2.0")]
56-
#endif
5761
public IEnumerator TrackPacketLossAsClient()
5862
{
5963
double packetLossRateMinRange = (m_PacketLossRate - m_PacketLossRangeDelta) / 100d;
@@ -65,8 +69,8 @@ public IEnumerator TrackPacketLossAsClient()
6569

6670
for (int i = 0; i < 1000; ++i)
6771
{
68-
using var writer = new FastBufferWriter(sizeof(byte), Allocator.Persistent);
69-
writer.WriteByteSafe(42);
72+
using var writer = new FastBufferWriter(m_MessageSize, Allocator.Persistent);
73+
writer.WriteBytesSafe(new byte[m_MessageSize]);
7074
m_ServerNetworkManager.CustomMessagingManager.SendNamedMessage("Test", m_ServerNetworkManager.ConnectedClientsIds, writer);
7175
}
7276

0 commit comments

Comments
 (0)