Repository: dotnet/runtime
File: src/mono/wasm/Wasm.Build.Tests/Blazor/EventPipeDiagnosticsTests.cs (C#)

Code before:

        async Task CpuProfileTest(IPage page)
        {
            await SetupCounterPage(page);

            await page.EvaluateAsync(@"
                    globalThis.upload = globalThis.uploadTrace(`cpuprofile.nettrace`, globalThis.getDotnetRuntime(0).collectCpuSamples({ durationSeconds: 2.0, skipDownload: true }));
                    console.log(`CPU samples collected: ${new Date().toISOString()}`);
                ");

            await ClickAndCollect(page);
        }

        // Run the test using the custom handler
        await RunForBuildWithDotnetRun(new BlazorRunOptions(
            Configuration: config,
            Test: CpuProfileTest,
            TimeoutSeconds: 60,
            CheckCounter: false,
            ServerEnvironment: new Dictionary<string, string>
            {
                ["DEVSERVER_UPLOAD_PATH"] = info.LogPath,
                ["DEVSERVER_UPLOAD_PATTERN"] = "^[a-zA-Z0-9_]+\\.nettrace$"
            }
        ));

        var methodFound = false;
        using (var source = TraceLog.OpenOrConvert(ConvertTrace(info, "cpuprofile.nettrace")))
        {
            methodFound = source.CallStacks.Any(stack => stack.CodeAddress.FullMethodName == "BlazorBasicTestApp.Pages.Counter.IncrementCount()");
            if (!methodFound)
            {
                foreach (var stack in source.CallStacks)
                {
                    _testOutput.WriteLine($"Stack: {stack.CodeAddress.FullMethodName}");
                }
            }
        }

        Assert.True(methodFound, "The cpuprofile.nettrace should contain stack frames for the 'Counter.IncrementCount' method");
    }

    [Fact]
    public async Task BlazorEventPipeTestWithMetrics()
    {
        string extraProperties = @"
                <EnableDiagnostics>true</EnableDiagnostics>
                <EventSourceSupport>true</EventSourceSupport>
                <MetricsSupport>true</MetricsSupport>
                <WBTDevServer>true</WBTDevServer>
            ";

Review comment (refactor):
Repeated regex literal "^[a-zA-Z0-9_]+\.nettrace$" added in three places (lines 66-68, 122-124, 196-198). Consider extracting to a shared constant to reduce duplication and risk of divergence if pattern changes.

Code after:

        BuildProject(info, config, new BuildOptions(AssertAppBundle: false));

        async Task CpuProfileTest(IPage page)
        {
            await SetupCounterPage(page);

            await page.EvaluateAsync(@"
                    globalThis.upload = globalThis.uploadTrace(`cpuprofile.nettrace`, globalThis.getDotnetRuntime(0).collectCpuSamples({ durationSeconds: 2.0, skipDownload: true }));
                    console.log(`CPU samples collected: ${new Date().toISOString()}`);
                ");

            await ClickAndCollect(page);
        }

        // Run the test using the custom handler
        await RunForBuildWithDotnetRun(new BlazorRunOptions(
            Configuration: config,
            Test: CpuProfileTest,
            TimeoutSeconds: 60,
            CheckCounter: false,
            ServerEnvironment: new Dictionary<string, string>
            {
                ["DEVSERVER_UPLOAD_PATH"] = info.LogPath,
                ["DEVSERVER_UPLOAD_PATTERN"] = uploadPattern
            }
        ));

        var methodFound = false;
        using (var source = TraceLog.OpenOrConvert(ConvertTrace(info, "cpuprofile.nettrace")))
        {
            methodFound = source.CallStacks.Any(stack => stack.CodeAddress.FullMethodName == "BlazorBasicTestApp.Pages.Counter.IncrementCount()");
            if (!methodFound)
            {
                foreach (var stack in source.CallStacks)
                {
                    _testOutput.WriteLine($"Stack: {stack.CodeAddress.FullMethodName}");
                }
            }
        }

        Assert.True(methodFound, "The cpuprofile.nettrace should contain stack frames for the 'Counter.IncrementCount' method");
    }

    [Fact]
    public async Task BlazorEventPipeTestWithMetrics()
    {
        string extraProperties = @"
                <EnableDiagnostics>true</EnableDiagnostics>
                <EventSourceSupport>true</EventSourceSupport>
                <MetricsSupport>true</MetricsSupport>