From 8e4bc944f59837d086a5070f640b7409a80f4321 Mon Sep 17 00:00:00 2001 From: Ilona Tomkowicz Date: Wed, 11 Sep 2024 11:20:40 +0200 Subject: [PATCH] Shift the responsibility of checking profile's size to the browser. --- .../TestAppScenarios/MemoryTests.cs | 13 ++++++++++-- .../WasmBasicTestApp/App/wwwroot/profiler.js | 21 ++++++++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/MemoryTests.cs b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/MemoryTests.cs index d1d25e04160c1..4f51f00f54484 100644 --- a/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/MemoryTests.cs +++ b/src/mono/wasm/Wasm.Build.Tests/TestAppScenarios/MemoryTests.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using System.Text.RegularExpressions; using Xunit.Abstractions; using Xunit; @@ -47,7 +48,15 @@ public async Task RunSimpleAppWithProfiler() BuildProject(config, assertAppBundle: false, extraArgs: extraArgs); var result = await RunSdkStyleAppForBuild(new (Configuration: config, TestScenario: "ProfilerTest")); - Console.WriteLine($"Test output: {result}"); - // ToDo: check if the file is created AND its size it bigger than 10MB - where do we look for it? + Regex regex = new Regex(@"Profile data of size (\d+) bytes"); + var match = result.TestOutput + .Select(line => regex.Match(line)) + .FirstOrDefault(m => m.Success); + Assert.True(match != null, $"TestOuptup did not contain log matching {regex}"); + if (!int.TryParse(match.Groups[1].Value, out int fileSize)) + { + Assert.Fail($"Failed to parse profile size from {match.Groups[1].Value} to int"); + } + Assert.True(fileSize >= 10 * 1024, $"Profile file size is less than 10KB. Actual size: {fileSize} bytes."); } } diff --git a/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/profiler.js b/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/profiler.js index 9ae2e2bc8070f..53d3841e316cf 100644 --- a/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/profiler.js +++ b/src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/profiler.js @@ -1,28 +1,35 @@ export function saveProfile(Module) { - let profileData = readProfileFile(Module); + const fileName = "output.mlpd"; + let profileData = readProfileFile(Module, fileName); + + if (!profileData) { + console.error("Profile data is empty or could not be read."); + return; + } const a = document.createElement('a'); const blob = new Blob([profileData]); a.href = URL.createObjectURL(blob); - a.download = "output.mlpd"; + a.download = fileName; // Append anchor to body. document.body.appendChild(a); a.click(); + console.log(`TestOutput -> Profile data of size ${profileData.length} bytes started downloading.`); + // Remove anchor from body document.body.removeChild(a); } -function readProfileFile(Module) { - let profileFilePath="output.mlpd"; +function readProfileFile(Module, fileName) { - var stat = Module.FS.stat(profileFilePath); + var stat = Module.FS.stat(fileName); if (stat && stat.size > 0) { - return Module.FS.readFile(profileFilePath); + return Module.FS.readFile(fileName); } else { - console.debug(`Unable to fetch the profile file ${profileFilePath} as it is empty`); + console.debug(`Unable to fetch the profile file ${fileName} as it is empty`); return null; } } \ No newline at end of file