-
Notifications
You must be signed in to change notification settings - Fork 17
feat: scene content stats — debug widget, creator Scene Stats panel & MCP tools #9457
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
Changes from 31 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
6255d94
feat: add scene content stats to the Current Scene debug widget
dalkia 5d6dd64
Merge branch 'dev' into feat/current-scene-content-stats-debug
dalkia 275428b
Merge branch 'dev' into feat/current-scene-content-stats-debug
dalkia 4f30841
feat: remove content size stat from Current Scene widget
dalkia 22e8c4b
feat: align content stat caps with documented scene limitations
dalkia 61d307f
feat: add scene metrics panel to the scene debug menu
dalkia 124d412
feat: expose scene content stats through an MCP tool
dalkia b4483cc
feat: add per-model content breakdown MCP tool (demo)
dalkia 92a8c1f
feat: add materials and draw-call estimate to the content breakdown
dalkia de98d33
feat: split content stats into their own Scene content debug widget
dalkia fdb47df
feat: per-POV visibility columns in the content breakdown
dalkia 1746336
feat: add get_performance_stats MCP tool
dalkia 448efed
fix: qualify UnityEngine.Time in GetPerformanceStatsTool
dalkia 2cd854a
Merge remote-tracking branch 'origin/dev' into feat/current-scene-con…
dalkia 8190dcb
feat: redesign Scene Stats creator panel
dalkia 051f251
feat: use a 2x2 grid glyph for the Scene Stats sidebar button
dalkia c711381
feat: use info.png as the Scene Stats sidebar button icon
dalkia c896ac6
feat: count shader variants in scene content stats
dalkia 9f53073
feat: use dedicated stats-chart icon for the Scene Stats sidebar button
dalkia 19e4831
Merge branch 'dev' into feat/current-scene-content-stats-debug
dalkia e230057
feat: restyle Scene Stats panel with banded rows and warning percentages
dalkia f876e62
feat: remove the Scene Stats panel close button
dalkia a74caf0
feat: make the Scene Stats panel background near-opaque for readability
dalkia 2232d38
chore: soften Scene Stats panel background to 90% opacity
dalkia da3e2f9
chore: rename External content widget row to Videos
dalkia 5ce04ae
feat: count video/media players instead of external content + NFTs
dalkia 5810e65
chore: label the media-player row External Videos/Audios
dalkia 59b4458
chore: drop the materials cap, show it as an informative count
dalkia f710d92
Merge branch 'dev' into feat/current-scene-content-stats-debug
dalkia d5b4aee
feat: per-metric hover tooltips in the Scene Stats panel
dalkia 534a6c1
chore: group capped stat rows above the uncapped ones
dalkia cf96039
fix: unsubscribe metrics button click handler in OnDisable
dalkia cb040fe
feat: use InfoIcn sprite for scene stats info marker
dalkia 637014f
chore: move SceneStats icon into the UI sprite atlas
dalkia b127293
chore: assign sprite id to icon-scene-stats on reimport
dalkia 13dfeb1
refactor: address MCP content-stats tool review feedback
dalkia bdda35d
fix: divide scene-tick average by contributing samples only
dalkia ae29b7f
fix: align MCP scene-tick stats with the render sampling window
dalkia 9272863
fix: use the client-wide hiccup definition in get_performance_stats
dalkia c6ae84e
fix: measure content-stats poll timeout against the wall clock
dalkia f6fe7a7
fix: refcount MCP content-stats demand so overlapping calls don't can…
dalkia 4dbe4ce
test: cover MCP performance and content-stats tools via injectable waits
dalkia 5f42d52
chore: resolve low-risk lint findings in branch-touched files
dalkia c6e1800
fix: declare an OutputSchema for get_scene_content_breakdown
dalkia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
Explorer/Assets/DCL/McpServer/Tests/GetSceneContentStatsToolShould.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| using DCL.McpServer.Core; | ||
| using DCL.McpServer.Tools; | ||
| using DCL.Profiling; | ||
| using DCL.Utilities; | ||
| using ECS.SceneLifeCycle; | ||
| using Newtonsoft.Json.Linq; | ||
| using NSubstitute; | ||
| using NUnit.Framework; | ||
| using SceneRunner.Scene; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using UnityEngine; | ||
|
|
||
| namespace DCL.McpServer.Tests | ||
| { | ||
| public class GetSceneContentStatsToolShould | ||
| { | ||
| private IScenesCache scenesCache = null!; | ||
| private ISceneFacade scene = null!; | ||
| private SceneRuntimeMetrics runtimeMetrics = null!; | ||
|
|
||
| [SetUp] | ||
| public void Setup() | ||
| { | ||
| runtimeMetrics = new SceneRuntimeMetrics(); | ||
|
|
||
| ISceneData sceneData = Substitute.For<ISceneData>(); | ||
| sceneData.Parcels.Returns(new List<Vector2Int> { new (0, 0), new (0, 1) }); | ||
|
|
||
| scene = Substitute.For<ISceneFacade>(); | ||
| scene.SceneData.Returns(sceneData); | ||
| scene.RuntimeMetrics.Returns(runtimeMetrics); | ||
|
|
||
| scenesCache = Substitute.For<IScenesCache>(); | ||
| scenesCache.CurrentScene.Returns(new ReactiveProperty<ISceneFacade?>(scene)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ErrorWhenNoSceneIsLoaded() | ||
| { | ||
| // Arrange | ||
| scenesCache.CurrentScene.Returns(new ReactiveProperty<ISceneFacade?>(null)); | ||
| var tool = new GetSceneContentStatsTool(scenesCache, collectionTimeoutMs: 0); | ||
|
|
||
| // Act | ||
| McpToolResult result = Execute(tool); | ||
|
|
||
| // Assert | ||
| Assert.That(result.Payload["isError"]!.Value<bool>(), Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ErrorWhenTheSceneNeverProducedStats() | ||
| { | ||
| // Arrange — HasData stays false and the zero timeout skips waiting for a pass | ||
| var tool = new GetSceneContentStatsTool(scenesCache, collectionTimeoutMs: 0); | ||
|
|
||
| // Act | ||
| McpToolResult result = Execute(tool); | ||
|
|
||
| // Assert | ||
| Assert.That(result.Payload["isError"]!.Value<bool>(), Is.True); | ||
| Assert.That(runtimeMetrics.ContentStats.RequestedByMcp, Is.False); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ReportStatsWithCapsMatchingTheOutputSchema() | ||
| { | ||
| // Arrange | ||
| SceneContentStats stats = runtimeMetrics.ContentStats; | ||
| stats.HasData = true; | ||
| stats.Entities = 10; | ||
| stats.Triangles = 5200; | ||
| stats.Bodies = 12; | ||
| stats.Geometries = 7; | ||
| stats.Materials = 5; | ||
| stats.Textures = 3; | ||
| stats.ShaderVariants = 2; | ||
| stats.Colliders = 2; | ||
| stats.Videos = 1; | ||
|
|
||
| var tool = new GetSceneContentStatsTool(scenesCache, collectionTimeoutMs: 0); | ||
|
|
||
| // Act | ||
| var structured = (JObject)Execute(tool).Payload["structuredContent"]!; | ||
|
|
||
| // Assert | ||
| McpSchemaAssert.KeysMatch(tool.OutputSchema, structured); | ||
| Assert.That(structured["parcelCount"]!.Value<int>(), Is.EqualTo(2)); | ||
| Assert.That(structured["fresh"]!.Value<bool>(), Is.False); | ||
| Assert.That(structured["entities"]!.Value<int>(), Is.EqualTo(10)); | ||
| Assert.That(structured["entitiesCap"]!.Value<int>(), Is.EqualTo(400)); | ||
| Assert.That(structured["triangles"]!.Value<long>(), Is.EqualTo(5200)); | ||
| Assert.That(structured["trianglesCap"]!.Value<long>(), Is.EqualTo(20000)); | ||
| Assert.That(structured["geometries"]!.Value<int>(), Is.EqualTo(7)); | ||
| Assert.That(structured["shaderVariants"]!.Value<int>(), Is.EqualTo(2)); | ||
| Assert.That(structured["videos"]!.Value<int>(), Is.EqualTo(1)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ClearTheDemandFlagAfterReporting() | ||
| { | ||
| // Arrange | ||
| runtimeMetrics.ContentStats.HasData = true; | ||
| var tool = new GetSceneContentStatsTool(scenesCache, collectionTimeoutMs: 0); | ||
|
|
||
| // Act | ||
| Execute(tool); | ||
|
|
||
| // Assert | ||
| Assert.That(runtimeMetrics.ContentStats.RequestedByMcp, Is.False); | ||
| } | ||
|
|
||
| private static McpToolResult Execute(GetSceneContentStatsTool tool) => | ||
| tool.ExecuteAsync(new JObject(), CancellationToken.None).GetAwaiter().GetResult(); | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
Explorer/Assets/DCL/McpServer/Tests/GetSceneContentStatsToolShould.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
161 changes: 161 additions & 0 deletions
161
Explorer/Assets/DCL/McpServer/Tools/GetPerformanceStatsTool.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| using Cysharp.Threading.Tasks; | ||
| using DCL.McpServer.Core; | ||
| using DCL.McpServer.Utils; | ||
| using DCL.Profiling; | ||
| using ECS.SceneLifeCycle; | ||
| using Newtonsoft.Json.Linq; | ||
| using SceneRunner.Scene; | ||
| using System.Globalization; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using UnityEngine; | ||
|
|
||
| namespace DCL.McpServer.Tools | ||
| { | ||
| /// <summary> | ||
| /// Samples the client's real frame rate over its own short window (no shared profiler state | ||
| /// is touched) and reports it together with the current scene's tick FPS, so an agent can | ||
| /// correlate a viewpoint's content cost with the frame rate it actually produces. | ||
| /// </summary> | ||
| public class GetPerformanceStatsTool : McpTool | ||
| { | ||
| private const float DEFAULT_SAMPLE_SECONDS = 2f; | ||
| private const float MIN_SAMPLE_SECONDS = 0.5f; | ||
| private const float MAX_SAMPLE_SECONDS = 10f; | ||
| private const float HICCUP_THRESHOLD_MS = 50f; | ||
|
|
||
| private readonly IScenesCache scenesCache; | ||
| private readonly long[] tickScratch = new long[SampledCounter.BUFFER_CAPACITY]; | ||
|
|
||
| public override string Name => "get_performance_stats"; | ||
|
|
||
| public override string Description => | ||
| "Sample the client's real frame rate over a short window and report render FPS (average, min, max, hiccup frames > 50 ms) plus " | ||
| + "the current scene's tick FPS vs its target. The call holds for sampleSeconds while it measures. Use together with " | ||
| + "get_scene_content_breakdown (sortBy=visibleTriangles) to correlate a viewpoint's content cost with the frame rate it actually " | ||
| + "produces — position the camera first, then sample."; | ||
|
|
||
| public override JObject OutputSchema => | ||
| McpJsonSchema.Object() | ||
| .Number("sampleSeconds") | ||
| .Integer("framesSampled") | ||
| .Number("averageFps") | ||
| .Number("minFps", "Lowest instantaneous FPS in the window (longest frame).") | ||
| .Number("maxFps") | ||
| .Number("averageFrameMs") | ||
| .Number("maxFrameMs") | ||
| .Integer("hiccupFrames", "Frames longer than 50 ms in the window.") | ||
| .Object("sceneTick", McpJsonSchema.Object() | ||
| .Number("averageFps") | ||
| .Number("minFps") | ||
| .Number("maxFps") | ||
| .Integer("targetFps"), | ||
| "The current scene's JS tick rate, or null when no scene is loaded or it has not ticked yet.", nullable: true) | ||
| .Build(); | ||
|
|
||
| public override McpToolAnnotations Annotations => McpToolAnnotations.ReadOnly(); | ||
|
|
||
| public GetPerformanceStatsTool(IScenesCache scenesCache) | ||
| { | ||
| this.scenesCache = scenesCache; | ||
| } | ||
|
|
||
| public override async UniTask<McpToolResult> ExecuteAsync(JObject arguments, CancellationToken ct) | ||
| { | ||
| float sampleSeconds = Mathf.Clamp(arguments.GetFloat("sampleSeconds", DEFAULT_SAMPLE_SECONDS), MIN_SAMPLE_SECONDS, MAX_SAMPLE_SECONDS); | ||
|
dalkia marked this conversation as resolved.
|
||
|
|
||
| var framesSampled = 0; | ||
| float totalMs = 0f; | ||
| float minFrameMs = float.MaxValue; | ||
| float maxFrameMs = 0f; | ||
| var hiccupFrames = 0; | ||
|
|
||
| float start = UnityEngine.Time.realtimeSinceStartup; | ||
|
|
||
| while (UnityEngine.Time.realtimeSinceStartup - start < sampleSeconds) | ||
| { | ||
| await UniTask.NextFrame(ct); | ||
|
|
||
| float frameMs = UnityEngine.Time.unscaledDeltaTime * 1000f; | ||
| framesSampled++; | ||
| totalMs += frameMs; | ||
| if (frameMs < minFrameMs) minFrameMs = frameMs; | ||
| if (frameMs > maxFrameMs) maxFrameMs = frameMs; | ||
| if (frameMs > HICCUP_THRESHOLD_MS) hiccupFrames++; | ||
| } | ||
|
|
||
| if (framesSampled == 0) | ||
| return McpToolResult.Error("No frames rendered during the sampling window."); | ||
|
|
||
| float averageFrameMs = totalMs / framesSampled; | ||
| float averageFps = 1000f / averageFrameMs; | ||
| float minFps = 1000f / maxFrameMs; | ||
| float maxFps = 1000f / minFrameMs; | ||
|
|
||
| JObject? sceneTick = null; | ||
| ISceneFacade? scene = scenesCache.CurrentScene.Value; | ||
|
|
||
| if (scene != null) | ||
| { | ||
| SceneRuntimeMetrics metrics = scene.RuntimeMetrics; | ||
| int tickSamples = metrics.TickTimesNs.CopySnapshot(tickScratch); | ||
|
|
||
| if (tickSamples > 0) | ||
| { | ||
| long totalNs = 0; | ||
| long minNs = long.MaxValue; | ||
| long maxNs = long.MinValue; | ||
|
|
||
| for (var i = 0; i < tickSamples; i++) | ||
| { | ||
| long ns = tickScratch[i]; | ||
| if (ns <= 0) continue; | ||
| totalNs += ns; | ||
| if (ns < minNs) minNs = ns; | ||
| if (ns > maxNs) maxNs = ns; | ||
| } | ||
|
|
||
| if (totalNs > 0) | ||
| sceneTick = new JObject | ||
| { | ||
| ["averageFps"] = Round1(1e9f / ((float)totalNs / tickSamples)), | ||
| ["minFps"] = Round1(1e9f / maxNs), | ||
| ["maxFps"] = Round1(1e9f / minNs), | ||
| ["targetFps"] = metrics.TargetFps, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| var structured = new JObject | ||
| { | ||
| ["sampleSeconds"] = Round1(sampleSeconds), | ||
| ["framesSampled"] = framesSampled, | ||
| ["averageFps"] = Round1(averageFps), | ||
| ["minFps"] = Round1(minFps), | ||
| ["maxFps"] = Round1(maxFps), | ||
| ["averageFrameMs"] = Round1(averageFrameMs), | ||
| ["maxFrameMs"] = Round1(maxFrameMs), | ||
| ["hiccupFrames"] = hiccupFrames, | ||
| ["sceneTick"] = sceneTick ?? (JToken)JValue.CreateNull(), | ||
| }; | ||
|
|
||
| var text = new StringBuilder(); | ||
| text.Append("Render: ").Append(averageFps.ToString("F1", CultureInfo.InvariantCulture)).Append(" fps avg (min ") | ||
| .Append(minFps.ToString("F1", CultureInfo.InvariantCulture)).Append(", max ") | ||
| .Append(maxFps.ToString("F1", CultureInfo.InvariantCulture)).Append(") over ") | ||
| .Append(framesSampled).Append(" frames / ").Append(sampleSeconds.ToString("F1", CultureInfo.InvariantCulture)).Append("s; ") | ||
| .Append(hiccupFrames).AppendLine(" hiccup frames (>50 ms)."); | ||
|
|
||
| if (sceneTick != null) | ||
| text.Append("Scene tick: ").Append(sceneTick["averageFps"]!.Value<float>().ToString("F1", CultureInfo.InvariantCulture)) | ||
| .Append(" fps avg (target ").Append(sceneTick["targetFps"]!.Value<int>()).Append(")."); | ||
| else | ||
| text.Append("Scene tick: no data (no scene loaded or it has not ticked yet)."); | ||
|
|
||
| return McpToolResult.TextWithStructured(text.ToString(), structured); | ||
| } | ||
|
|
||
| private static float Round1(float value) => | ||
| Mathf.Round(value * 10f) / 10f; | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
Explorer/Assets/DCL/McpServer/Tools/GetPerformanceStatsTool.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.