|
| 1 | +using Cysharp.Threading.Tasks; |
| 2 | +using DCL.McpServer.Core; |
| 3 | +using DCL.McpServer.Tools; |
| 4 | +using DCL.Profiling; |
| 5 | +using DCL.Utilities; |
| 6 | +using ECS.SceneLifeCycle; |
| 7 | +using Newtonsoft.Json.Linq; |
| 8 | +using NSubstitute; |
| 9 | +using NUnit.Framework; |
| 10 | +using SceneRunner.Scene; |
| 11 | +using System.Threading; |
| 12 | + |
| 13 | +namespace DCL.McpServer.Tests |
| 14 | +{ |
| 15 | + public class GetPerformanceStatsToolShould |
| 16 | + { |
| 17 | + private IScenesCache scenesCache = null!; |
| 18 | + private ISceneFacade scene = null!; |
| 19 | + private SceneRuntimeMetrics runtimeMetrics = null!; |
| 20 | + private ReactiveProperty<ISceneFacade?> currentScene = null!; |
| 21 | + |
| 22 | + [SetUp] |
| 23 | + public void Setup() |
| 24 | + { |
| 25 | + runtimeMetrics = new SceneRuntimeMetrics { TargetFps = 30 }; |
| 26 | + |
| 27 | + scene = Substitute.For<ISceneFacade>(); |
| 28 | + scene.RuntimeMetrics.Returns(runtimeMetrics); |
| 29 | + |
| 30 | + currentScene = new ReactiveProperty<ISceneFacade?>(scene); |
| 31 | + scenesCache = Substitute.For<IScenesCache>(); |
| 32 | + scenesCache.CurrentScene.Returns(currentScene); |
| 33 | + } |
| 34 | + |
| 35 | + [Test] |
| 36 | + public void ReportRenderStatsFromTheSampledWindow() |
| 37 | + { |
| 38 | + // Arrange — 100 frames totalling 2 s: 20 ms average spanning 10–50 ms, 3 above the hiccup bar |
| 39 | + var tool = new GetPerformanceStatsTool(scenesCache, (_, _, _) => |
| 40 | + UniTask.FromResult(new GetPerformanceStatsTool.FrameWindow(100, 2000f, 10f, 50f, 3))); |
| 41 | + |
| 42 | + // Act |
| 43 | + var structured = (JObject)Execute(tool).Payload["structuredContent"]!; |
| 44 | + |
| 45 | + // Assert |
| 46 | + McpSchemaAssert.KeysMatch(tool.OutputSchema!, structured); |
| 47 | + Assert.That(structured["framesSampled"]!.Value<int>(), Is.EqualTo(100)); |
| 48 | + Assert.That(structured["averageFps"]!.Value<float>(), Is.EqualTo(50f)); |
| 49 | + Assert.That(structured["minFps"]!.Value<float>(), Is.EqualTo(20f)); |
| 50 | + Assert.That(structured["maxFps"]!.Value<float>(), Is.EqualTo(100f)); |
| 51 | + Assert.That(structured["averageFrameMs"]!.Value<float>(), Is.EqualTo(20f)); |
| 52 | + Assert.That(structured["maxFrameMs"]!.Value<float>(), Is.EqualTo(50f)); |
| 53 | + Assert.That(structured["hiccupFrames"]!.Value<int>(), Is.EqualTo(3)); |
| 54 | + } |
| 55 | + |
| 56 | + [Test] |
| 57 | + public void ReportSceneTickStatsOnlyFromTicksInsideTheWindow() |
| 58 | + { |
| 59 | + // Arrange — stale pre-window 100 ms ticks would read as 10 fps if they leaked into the window |
| 60 | + for (var i = 0; i < 10; i++) |
| 61 | + runtimeMetrics.TickTimesNs.Add(100_000_000); |
| 62 | + |
| 63 | + var tool = new GetPerformanceStatsTool(scenesCache, (_, _, _) => |
| 64 | + { |
| 65 | + for (var i = 0; i < 4; i++) |
| 66 | + runtimeMetrics.TickTimesNs.Add(25_000_000); // 40 fps during the window |
| 67 | + |
| 68 | + return UniTask.FromResult(SteadyWindow()); |
| 69 | + }); |
| 70 | + |
| 71 | + // Act |
| 72 | + var structured = (JObject)Execute(tool).Payload["structuredContent"]!; |
| 73 | + var sceneTick = (JObject)structured["sceneTick"]!; |
| 74 | + |
| 75 | + // Assert |
| 76 | + McpSchemaAssert.KeysMatch(tool.OutputSchema!, structured); |
| 77 | + Assert.That(sceneTick["averageFps"]!.Value<float>(), Is.EqualTo(40f)); |
| 78 | + Assert.That(sceneTick["minFps"]!.Value<float>(), Is.EqualTo(40f)); |
| 79 | + Assert.That(sceneTick["maxFps"]!.Value<float>(), Is.EqualTo(40f)); |
| 80 | + Assert.That(sceneTick["targetFps"]!.Value<int>(), Is.EqualTo(30)); |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public void ReportNullSceneTickWhenNoTicksLandDuringTheWindow() |
| 85 | + { |
| 86 | + // Arrange — the ring holds only stale pre-window ticks, as when the scene is paused |
| 87 | + for (var i = 0; i < 10; i++) |
| 88 | + runtimeMetrics.TickTimesNs.Add(100_000_000); |
| 89 | + |
| 90 | + var tool = new GetPerformanceStatsTool(scenesCache, (_, _, _) => UniTask.FromResult(SteadyWindow())); |
| 91 | + |
| 92 | + // Act |
| 93 | + var structured = (JObject)Execute(tool).Payload["structuredContent"]!; |
| 94 | + |
| 95 | + // Assert |
| 96 | + Assert.That(structured["sceneTick"]!.Type, Is.EqualTo(JTokenType.Null)); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public void ReportNullSceneTickWhenTheSceneChangesMidSample() |
| 101 | + { |
| 102 | + // Arrange — the current scene flips during the window, so its ticks describe a mixed window |
| 103 | + var tool = new GetPerformanceStatsTool(scenesCache, (_, _, _) => |
| 104 | + { |
| 105 | + runtimeMetrics.TickTimesNs.Add(25_000_000); |
| 106 | + currentScene.Value = null; |
| 107 | + return UniTask.FromResult(SteadyWindow()); |
| 108 | + }); |
| 109 | + |
| 110 | + // Act |
| 111 | + var structured = (JObject)Execute(tool).Payload["structuredContent"]!; |
| 112 | + |
| 113 | + // Assert |
| 114 | + Assert.That(structured["sceneTick"]!.Type, Is.EqualTo(JTokenType.Null)); |
| 115 | + } |
| 116 | + |
| 117 | + [Test] |
| 118 | + public void ClampSampleSecondsBeforeSampling() |
| 119 | + { |
| 120 | + // Arrange |
| 121 | + var receivedSeconds = 0f; |
| 122 | + |
| 123 | + var tool = new GetPerformanceStatsTool(scenesCache, (seconds, _, _) => |
| 124 | + { |
| 125 | + receivedSeconds = seconds; |
| 126 | + return UniTask.FromResult(SteadyWindow()); |
| 127 | + }); |
| 128 | + |
| 129 | + // Act |
| 130 | + var structured = (JObject)Execute(tool, new JObject { ["sampleSeconds"] = 100 }).Payload["structuredContent"]!; |
| 131 | + |
| 132 | + // Assert |
| 133 | + Assert.That(receivedSeconds, Is.EqualTo(10f)); |
| 134 | + Assert.That(structured["sampleSeconds"]!.Value<float>(), Is.EqualTo(10f)); |
| 135 | + } |
| 136 | + |
| 137 | + [Test] |
| 138 | + public void SampleHiccupsAgainstTheClientWideThreshold() |
| 139 | + { |
| 140 | + // Arrange |
| 141 | + var receivedThresholdMs = 0f; |
| 142 | + |
| 143 | + var tool = new GetPerformanceStatsTool(scenesCache, (_, thresholdMs, _) => |
| 144 | + { |
| 145 | + receivedThresholdMs = thresholdMs; |
| 146 | + return UniTask.FromResult(SteadyWindow()); |
| 147 | + }); |
| 148 | + |
| 149 | + // Act |
| 150 | + var structured = (JObject)Execute(tool).Payload["structuredContent"]!; |
| 151 | + |
| 152 | + // Assert |
| 153 | + float expected = Profiler.EffectiveHiccupThresholdNs() / 1_000_000f; |
| 154 | + Assert.That(receivedThresholdMs, Is.EqualTo(expected)); |
| 155 | + Assert.That(structured["hiccupThresholdMs"], Is.Not.Null); |
| 156 | + } |
| 157 | + |
| 158 | + [Test] |
| 159 | + public void ErrorWhenNoFramesWereSampled() |
| 160 | + { |
| 161 | + // Arrange |
| 162 | + var tool = new GetPerformanceStatsTool(scenesCache, (_, _, _) => |
| 163 | + UniTask.FromResult(new GetPerformanceStatsTool.FrameWindow(0, 0f, 0f, 0f, 0))); |
| 164 | + |
| 165 | + // Act |
| 166 | + McpToolResult result = Execute(tool); |
| 167 | + |
| 168 | + // Assert |
| 169 | + Assert.That(result.Payload["isError"]!.Value<bool>(), Is.True); |
| 170 | + } |
| 171 | + |
| 172 | + private static GetPerformanceStatsTool.FrameWindow SteadyWindow() => |
| 173 | + new (120, 1920f, 16f, 16f, 0); |
| 174 | + |
| 175 | + private static McpToolResult Execute(GetPerformanceStatsTool tool, JObject? arguments = null) => |
| 176 | + tool.ExecuteAsync(arguments ?? new JObject(), CancellationToken.None).GetAwaiter().GetResult(); |
| 177 | + } |
| 178 | +} |
0 commit comments