Skip to content

Commit

Permalink
Add custom profiler statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
jeickhoff committed Jul 31, 2023
1 parent db241d0 commit aa32de7
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 54 deletions.
4 changes: 2 additions & 2 deletions Assets/Prefabs/TerrainArea.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3194879136077857405}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &346453942096632660
MonoBehaviour:
Expand Down Expand Up @@ -74,7 +74,7 @@ MonoBehaviour:
DefaultGhostMode: 0
SupportedGhostModes: 1
OptimizationMode: 1
Importance: 1
Importance: 100
prefabId: fbe285af02efd794da108f614fa73983
HasOwner: 0
SupportAutoCommandTarget: 1
Expand Down
8 changes: 0 additions & 8 deletions Assets/Scripts/DebugStats.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Assets/Scripts/DebugStats.cs.meta

This file was deleted.

21 changes: 0 additions & 21 deletions Assets/Scripts/DebugStatsSystem.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/Scripts/DebugStatsSystem.cs.meta

This file was deleted.

20 changes: 18 additions & 2 deletions Assets/Scripts/Player/Multiplay/MultiplayPlayerController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using UnityEngine;
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using System.Linq;
using Opencraft.Statistics;
using Unity.Entities;
using Unity.Profiling;
using Unity.RenderStreaming;
using UnityEngine.UI;
using Random = UnityEngine.Random;

namespace Opencraft.Player.Multiplay
{
Expand All @@ -25,13 +29,25 @@ public class MultiplayPlayerController : MonoBehaviour

public Text debugText;
public Text tooltipText;

private ProfilerRecorder _numAreasRecorder;

protected void Awake()
{
playerInput.onDeviceChange += OnDeviceChange;
username = Random.Range(0, 99999);
}

private void OnEnable()
{
_numAreasRecorder = ProfilerRecorder.StartNew(GameStatistics.GameStatisticsCategory, GameStatistics.NumTerrainAreasClientName);
}

private void OnDestroy()
{
_numAreasRecorder.Dispose();
}

void OnDeviceChange(InputDevice device, InputDeviceChange change)
{
switch (change)
Expand Down Expand Up @@ -63,7 +79,7 @@ public void CheckPairedDevices()

private void Update()
{
debugText.text = $"NumAreas: {DebugStats.numAreas}\n";
debugText.text = $"NumAreas: {_numAreasRecorder.LastValue}\n";
tooltipText.enabled = !playerEntityRequestSent && !playerEntityExists;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Unity.Profiling;
using Unity.Profiling.Editor;


// Defines profiler counters for collection and display by profiler module
namespace Opencraft.Player.Multiplay.MultiplayStats
{
public class MultiplayStatistics
{
public static readonly ProfilerCategory MultiplayCategory = ProfilerCategory.Scripts;

public const string RoundTripTimeName = "Round Trip Time (ms)";
public static readonly ProfilerCounterValue<double> MultiplayRoundTripTime =
new ProfilerCounterValue<double>(MultiplayCategory, RoundTripTimeName, ProfilerMarkerDataUnit.Count);

public const string BitRateName = "BitRate";
public static readonly ProfilerCounterValue<double> MultiplayBitRate =
new ProfilerCounterValue<double>(MultiplayCategory ,BitRateName , ProfilerMarkerDataUnit.Count);

public const string FPSName = "FPS";
public static readonly ProfilerCounterValue<double> MultiplayFPS =
new ProfilerCounterValue<double>(MultiplayCategory ,"FPS" , ProfilerMarkerDataUnit.Count);

public const string PacketLossName = "PacketLoss";
public static readonly ProfilerCounterValue<double> MultiplayPacketLoss =
new ProfilerCounterValue<double>(MultiplayCategory ,"PacketLoss" , ProfilerMarkerDataUnit.Percent);
}

[System.Serializable]
[ProfilerModuleMetadata("Multiplay Statistics")]
public class MultiplayProfilerModule : ProfilerModule
{
static readonly ProfilerCounterDescriptor[] k_Counters = new ProfilerCounterDescriptor[]
{
new ProfilerCounterDescriptor(MultiplayStatistics.RoundTripTimeName, MultiplayStatistics.MultiplayCategory),
new ProfilerCounterDescriptor(MultiplayStatistics.BitRateName, MultiplayStatistics.MultiplayCategory),
new ProfilerCounterDescriptor(MultiplayStatistics.FPSName, MultiplayStatistics.MultiplayCategory),
new ProfilerCounterDescriptor(MultiplayStatistics.PacketLossName, MultiplayStatistics.MultiplayCategory),
};

// Ensure that both ProfilerCategory.Scripts and ProfilerCategory.Memory categories are enabled when our module is active.
static readonly string[] k_AutoEnabledCategoryNames = new string[]
{
ProfilerCategory.Scripts.Name
};


// Pass the auto-enabled category names to the base constructor.
public MultiplayProfilerModule() : base(k_Counters, autoEnabledCategoryNames: k_AutoEnabledCategoryNames) { }
}
}

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

19 changes: 12 additions & 7 deletions Assets/Scripts/Player/Multiplay/MultiplayStats/StatsUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class StatsDisplay
private IEnumerator CollectStats()
{
// Sampling period
var waitSec = new WaitForSeconds(1);
var waitSec = new WaitForSeconds(0.5f);

while (true)
{
Expand Down Expand Up @@ -316,7 +316,7 @@ private static string CreateDisplayString(RTCStatsReport report, RTCStatsReport
builder.AppendLine($"Decoder: {inboundStats.decoderImplementation}");
builder.AppendLine($"Resolution: {inboundStats.frameWidth}x{inboundStats.frameHeight}");
builder.AppendLine($"Framerate: {inboundStats.framesPerSecond}");

MultiplayStatistics.MultiplayFPS.Value = inboundStats.framesPerSecond;
}

if (lastReport.TryGetValue(inboundStats.Id, out var lastStats) &&
Expand All @@ -325,6 +325,7 @@ private static string CreateDisplayString(RTCStatsReport report, RTCStatsReport
var duration = (double)(inboundStats.Timestamp - lastInboundStats.Timestamp) / 1000000;
var bitrate = (8 * (inboundStats.bytesReceived - lastInboundStats.bytesReceived) / duration) / 1000;
builder.AppendLine($"Bitrate: {bitrate:F2} kbit/sec");
MultiplayStatistics.MultiplayBitRate.Value = bitrate;
}
}
else if (stats is RTCOutboundRTPStreamStats outboundStats)
Expand Down Expand Up @@ -381,24 +382,28 @@ private static string CreateDisplayString(RTCStatsReport report, RTCStatsReport

if (rttm> 0)
{
double rtt = trtt / rttm;
builder.AppendLine(
$"Avg RTT: {trtt/rttm}");
$"Avg RTT: {rtt}");
MultiplayStatistics.MultiplayRoundTripTime.Value = rtt * 1000; // ms
}
builder.AppendLine(
$"Packet Loss: {fl}");
MultiplayStatistics.MultiplayPacketLoss.Value = fl;
}
else if (stats is RTCRemoteInboundRtpStreamStats remoteInboundStats)
{
builder.AppendLine($"{remoteInboundStats.kind} receiving remote stream stats");
if (remoteInboundStats.roundTripTimeMeasurements > 0)
{
builder.AppendLine(
$"Avg RTT: {remoteInboundStats.totalRoundTripTime / remoteInboundStats.roundTripTimeMeasurements}");
double rtt = remoteInboundStats.totalRoundTripTime /
remoteInboundStats.roundTripTimeMeasurements;
builder.AppendLine($"Avg RTT: {rtt}");
MultiplayStatistics.MultiplayRoundTripTime.Value = rtt * 1000; // ms
}
builder.AppendLine(
$"Packet Loss: {remoteInboundStats.fractionLost}");
//builder.AppendLine($"Resolution: {inboundStats.frameWidth}x{inboundStats.frameHeight}");
//builder.AppendLine($"Framerate: {inboundStats.framesPerSecond}");
MultiplayStatistics.MultiplayPacketLoss.Value = remoteInboundStats.fractionLost;
}
}

Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/Statistics.meta

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

39 changes: 39 additions & 0 deletions Assets/Scripts/Statistics/GameStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Unity.Profiling;
using Unity.Profiling.Editor;

namespace Opencraft.Statistics
{
public class GameStatistics
{
public static readonly ProfilerCategory GameStatisticsCategory = ProfilerCategory.Scripts;

public const string NumTerrainAreasClientName = "Number of Terrain Areas (Client)";
public static readonly ProfilerCounterValue<int> NumTerrainAreasClient =
new ProfilerCounterValue<int>(GameStatisticsCategory, NumTerrainAreasClientName, ProfilerMarkerDataUnit.Count);
public const string NumTerrainAreasServerName = "Number of Terrain Areas (Server)";
public static readonly ProfilerCounterValue<int> NumTerrainAreasServer =
new ProfilerCounterValue<int>(GameStatisticsCategory, NumTerrainAreasServerName, ProfilerMarkerDataUnit.Count);

}

[System.Serializable]
[ProfilerModuleMetadata("Game Statistics")]
public class GameProfilerModule : ProfilerModule
{
static readonly ProfilerCounterDescriptor[] k_Counters = new ProfilerCounterDescriptor[]
{
new ProfilerCounterDescriptor(GameStatistics.NumTerrainAreasClientName, GameStatistics.GameStatisticsCategory),
new ProfilerCounterDescriptor(GameStatistics.NumTerrainAreasServerName, GameStatistics.GameStatisticsCategory)
};

// Ensure that both ProfilerCategory.Scripts and ProfilerCategory.Memory categories are enabled when our module is active.
static readonly string[] k_AutoEnabledCategoryNames = new string[]
{
ProfilerCategory.Scripts.Name
};


// Pass the auto-enabled category names to the base constructor.
public GameProfilerModule() : base(k_Counters, autoEnabledCategoryNames: k_AutoEnabledCategoryNames) { }
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Statistics/GameStatistics.cs.meta

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

37 changes: 37 additions & 0 deletions Assets/Scripts/Statistics/StatisticsSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Opencraft.Terrain.Authoring;
using Unity.Entities;

namespace Opencraft.Statistics
{
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct StatisticsSystemServer : ISystem
{
private EntityQuery _terrainAreaQuery;

public void OnCreate(ref SystemState state)
{
_terrainAreaQuery = state.EntityManager.CreateEntityQuery(typeof(TerrainArea));
}

public void OnUpdate(ref SystemState state)
{
GameStatistics.NumTerrainAreasServer.Value = _terrainAreaQuery.CalculateEntityCount();
}
}

[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial struct StatisticsSystemClient : ISystem
{
private EntityQuery _terrainAreaQuery;

public void OnCreate(ref SystemState state)
{
_terrainAreaQuery = state.EntityManager.CreateEntityQuery(typeof(TerrainArea));
}

public void OnUpdate(ref SystemState state)
{
GameStatistics.NumTerrainAreasClient.Value = _terrainAreaQuery.CalculateEntityCount();
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Statistics/StatisticsSystem.cs.meta

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

0 comments on commit aa32de7

Please sign in to comment.