Skip to content

Commit

Permalink
Merge pull request #32 from noir-neo/performance-profiler
Browse files Browse the repository at this point in the history
Performance profiler
  • Loading branch information
noir-neo authored Mar 14, 2024
2 parents d8c4b94 + 0765ff7 commit 3b5edea
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"Hatbor.TextureStreaming.Spout",
"Hatbor.Rig.Fixed",
"Hatbor.HID",
"Hatbor.Light"
"Hatbor.Light",
"Hatbor.PerformanceProfiler"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
6 changes: 6 additions & 0 deletions Assets/Hatbor/Scripts/LifetimeScope/MainLifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Hatbor.Rig.VMC;
using Hatbor.Config;
using Hatbor.Rig.Fixed;
using Hatbor.PerformanceProfiler;
using Hatbor.VMC;
using VContainer;
using VContainer.Unity;
Expand All @@ -22,6 +23,11 @@ protected override void Configure(IContainerBuilder builder)
builder.Register<IConfigurable, LightConfig>(Lifetime.Singleton).AsSelf();
builder.Register<IConfigurable, MiscConfig>(Lifetime.Singleton).AsSelf();

// PerformanceProfiler
builder.RegisterEntryPoint<PerformanceProfilerTicker>();
builder.Register<IProfilerRecorder, FrameRateProfilerRecorder>(Lifetime.Singleton).AsSelf();
builder.Register<IProfilerRecorder, VmcServerProfilerRecorder>(Lifetime.Singleton).AsSelf();

// Rig/VMC
builder.RegisterEntryPoint<VmcServer>(Lifetime.Singleton).AsSelf();
builder.Register<IRootTransformRig, VmcRootTransformRig>(Lifetime.Singleton);
Expand Down
3 changes: 3 additions & 0 deletions Assets/Hatbor/Scripts/PerformanceProfiler.meta

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using UniRx;

namespace Hatbor.PerformanceProfiler
{
public sealed class FrameRateProfilerRecorder : IProfilerRecorder
{
readonly ReactiveProperty<string> text = new();
IReadOnlyReactiveProperty<string> IProfilerRecorder.Text => text;

int count;
float time;

void IProfilerRecorder.Tick(float t)
{
count++;
time += t;
if (!(time >= 1f)) return;
var frameRate = count / time;
text.Value = $"Frame Rate: {frameRate:F2}/s";
time = 0f;
count = 0;
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "Hatbor.PerformanceProfiler",
"rootNamespace": "",
"references": [
"GUID:560b04d1a97f54a4e82edc0cbbb69285",
"GUID:b0214a6008ed146ff8f122a6a9c2f6cc"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

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

11 changes: 11 additions & 0 deletions Assets/Hatbor/Scripts/PerformanceProfiler/IProfilerRecorder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using UniRx;

namespace Hatbor.PerformanceProfiler
{
public interface IProfilerRecorder
{
IReadOnlyReactiveProperty<string> Text { get; }

public void Tick(float t);
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using UnityEngine;
using VContainer;
using VContainer.Unity;

namespace Hatbor.PerformanceProfiler
{
public sealed class PerformanceProfilerTicker : ITickable
{
readonly IEnumerable<IProfilerRecorder> profilerRecorders;

[Inject]
public PerformanceProfilerTicker(IEnumerable<IProfilerRecorder> profilerRecorders)
{
this.profilerRecorders = profilerRecorders;
}

void ITickable.Tick()
{
foreach (var recorder in profilerRecorders)
{
recorder.Tick(Time.unscaledDeltaTime);
}
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using UniRx;

namespace Hatbor.PerformanceProfiler
{
public sealed class VmcServerProfilerRecorder : IProfilerRecorder
{
readonly ReactiveProperty<string> text = new();
IReadOnlyReactiveProperty<string> IProfilerRecorder.Text => text;

int count;
float time;

void IProfilerRecorder.Tick(float t)
{
time += t;
if (!(time >= 1f)) return;
var frameRate = count / time;
text.Value = $"VMC Received OK: {frameRate:F2}/s";
time = 0f;
count = 0;
}

public void IncrementCount()
{
count++;
}
}
}

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

12 changes: 12 additions & 0 deletions Assets/Hatbor/Scripts/UI/ConfigRoot.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Hatbor.Config;
using Hatbor.PerformanceProfiler;
using UniRx;
using UnityEngine.UIElements;
using VContainer;
Expand All @@ -12,24 +13,35 @@ public sealed class ConfigRoot : IStartable, IDisposable
{
readonly UIDocument uiDocument;
readonly IEnumerable<IConfigurable> configs;
readonly IEnumerable<IProfilerRecorder> profilerRecorders;
readonly IFileBrowser fileBrowser;

readonly CompositeDisposable disposables = new();

[Inject]
public ConfigRoot(UIDocument uiDocument,
IEnumerable<IConfigurable> configs,
IEnumerable<IProfilerRecorder> profilerRecorders,
IFileBrowser fileBrowser)
{
this.uiDocument = uiDocument;
this.configs = configs;
this.profilerRecorders = profilerRecorders;
this.fileBrowser = fileBrowser;
}

void IStartable.Start()
{
var root = uiDocument.rootVisualElement;
var container = root.Q<VisualElement>("unity-content-container");
var recorderFoldout = new Foldout();
container.Add(recorderFoldout);
foreach (var recorder in profilerRecorders)
{
var performanceGroup = new PerformanceGroup();
performanceGroup.Bind(recorder).AddTo(disposables);
recorderFoldout.Add(performanceGroup);
}
foreach (var config in configs)
{
var configGroup = new ConfigGroup(fileBrowser);
Expand Down
3 changes: 2 additions & 1 deletion Assets/Hatbor/Scripts/UI/Hatbor.UI.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"UniTask",
"UniRx",
"Hatbor.VMC",
"Hatbor.Config"
"Hatbor.Config",
"Hatbor.PerformanceProfiler"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
33 changes: 33 additions & 0 deletions Assets/Hatbor/Scripts/UI/PerformanceGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using Hatbor.PerformanceProfiler;
using UniRx;
using UnityEngine.UIElements;

namespace Hatbor.UI
{
public sealed class PerformanceGroup : VisualElement
{
readonly IFileBrowser fileBrowser;

readonly Label label;
readonly VisualElement container;

public PerformanceGroup()
{

label = new Label();
hierarchy.Add(label);
container = new VisualElement();
hierarchy.Add(container);
}

public IDisposable Bind(IProfilerRecorder recorder)
{
var disposables = new CompositeDisposable();
recorder.Text
.Subscribe(t => label.text = t)
.AddTo(disposables);
return disposables;
}
}
}
3 changes: 3 additions & 0 deletions Assets/Hatbor/Scripts/UI/PerformanceGroup.cs.meta

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

3 changes: 2 additions & 1 deletion Assets/Hatbor/Scripts/VMC/Hatbor.VMC.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"UniTask",
"UniRx",
"uOSC.Runtime",
"Hatbor.Config"
"Hatbor.Config",
"Hatbor.PerformanceProfiler"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
7 changes: 6 additions & 1 deletion Assets/Hatbor/Scripts/VMC/VmcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Hatbor.Config;
using Hatbor.PerformanceProfiler;
using UniRx;
using UnityEngine;
using uOSC;
Expand All @@ -14,6 +15,7 @@ namespace Hatbor.VMC
public sealed class VmcServer : IStartable, IDisposable
{
readonly VmcServerConfig config;
readonly VmcServerProfilerRecorder profilerRecorder;

readonly OscServer server = new();
readonly CompositeDisposable disposables = new();
Expand All @@ -34,9 +36,11 @@ public sealed class VmcServer : IStartable, IDisposable
public float CameraFov { get; private set; }

[Inject]
public VmcServer(VmcServerConfig config)
public VmcServer(VmcServerConfig config,
VmcServerProfilerRecorder profilerRecorder)
{
this.config = config;
this.profilerRecorder = profilerRecorder;
}

void IStartable.Start()
Expand Down Expand Up @@ -92,6 +96,7 @@ void OnRead(Message message)
if (IsAvailable)
{
LastAvailableReceivedTime = Time.time;
profilerRecorder.IncrementCount();
}
break;
}
Expand Down

0 comments on commit 3b5edea

Please sign in to comment.