From a30d6c9dc413d9c60bed2d4daecba4552abd7fe9 Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Mon, 16 Oct 2023 18:38:00 +0200 Subject: [PATCH] [browser] bench - separate scripts for memory snapshot (#93548) --- src/mono/sample/mbr/browser/WasmDelta.csproj | 2 +- src/mono/sample/wasm/Directory.Build.targets | 2 +- .../sample/wasm/blazor-frame/wwwroot/frame.js | 12 ----- .../wasm/blazor-frame/wwwroot/index.html | 4 +- .../wasm/blazor-frame/wwwroot/start.html | 33 +++++++++++++ .../sample/wasm/blazor-frame/wwwroot/start.js | 49 +++++++++++++++++++ .../sample/wasm/browser-bench/AppStart.cs | 3 +- .../sample/wasm/browser-frame/wwwroot/main.js | 4 -- .../wasm/browser-frame/wwwroot/start.html | 18 +++++++ .../wasm/browser-frame/wwwroot/start.js | 29 +++++++++++ .../Wasm.Browser.WebPack.Sample.csproj | 2 +- src/mono/sample/wasm/wasm.mk | 2 +- 12 files changed, 137 insertions(+), 23 deletions(-) create mode 100644 src/mono/sample/wasm/blazor-frame/wwwroot/start.html create mode 100644 src/mono/sample/wasm/blazor-frame/wwwroot/start.js create mode 100644 src/mono/sample/wasm/browser-frame/wwwroot/start.html create mode 100644 src/mono/sample/wasm/browser-frame/wwwroot/start.js diff --git a/src/mono/sample/mbr/browser/WasmDelta.csproj b/src/mono/sample/mbr/browser/WasmDelta.csproj index f040a672401d9..5bc8d24aa5b38 100644 --- a/src/mono/sample/mbr/browser/WasmDelta.csproj +++ b/src/mono/sample/mbr/browser/WasmDelta.csproj @@ -54,6 +54,6 @@ - + diff --git a/src/mono/sample/wasm/Directory.Build.targets b/src/mono/sample/wasm/Directory.Build.targets index 7ee7a50412fae..e320d5fd29dbb 100644 --- a/src/mono/sample/wasm/Directory.Build.targets +++ b/src/mono/sample/wasm/Directory.Build.targets @@ -64,7 +64,7 @@ - + diff --git a/src/mono/sample/wasm/blazor-frame/wwwroot/frame.js b/src/mono/sample/wasm/blazor-frame/wwwroot/frame.js index 4e8403bdc7b8e..6fde46d2b32be 100644 --- a/src/mono/sample/wasm/blazor-frame/wwwroot/frame.js +++ b/src/mono/sample/wasm/blazor-frame/wwwroot/frame.js @@ -29,18 +29,6 @@ try { window.muteErrors = () => { mute = true; } - - const urlParams = new URLSearchParams(window.location.search); - const myParam = urlParams.get('memorySnapshot'); - - Blazor.start({ - configureRuntime: dotnet => { - if (myParam === "true") { - dotnet.withStartupMemoryCache(true); - } - } - }); - } catch (err) { if (!mute) { diff --git a/src/mono/sample/wasm/blazor-frame/wwwroot/index.html b/src/mono/sample/wasm/blazor-frame/wwwroot/index.html index 581f46b23a663..2ee38e5ecc530 100644 --- a/src/mono/sample/wasm/blazor-frame/wwwroot/index.html +++ b/src/mono/sample/wasm/blazor-frame/wwwroot/index.html @@ -26,8 +26,8 @@ Reload 🗙 - - + + diff --git a/src/mono/sample/wasm/blazor-frame/wwwroot/start.html b/src/mono/sample/wasm/blazor-frame/wwwroot/start.html new file mode 100644 index 0000000000000..4e61123976dba --- /dev/null +++ b/src/mono/sample/wasm/blazor-frame/wwwroot/start.html @@ -0,0 +1,33 @@ + + + + + + blazor + + + + + + + + + +
+ + + + +
+
+ +
+ An unhandled error has occurred. + Reload + 🗙 +
+ + + + + diff --git a/src/mono/sample/wasm/blazor-frame/wwwroot/start.js b/src/mono/sample/wasm/blazor-frame/wwwroot/start.js new file mode 100644 index 0000000000000..4e8403bdc7b8e --- /dev/null +++ b/src/mono/sample/wasm/blazor-frame/wwwroot/start.js @@ -0,0 +1,49 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +"use strict"; + +class FrameApp { + reachedCallback() { + if (window.parent != window) { + window.parent.resolveAppStartEvent("reached"); + } + } +} + +let mute = false; +try { + globalThis.frameApp = new FrameApp(); + globalThis.frameApp.ReachedCallback = globalThis.frameApp.reachedCallback.bind(globalThis.frameApp); + if (window.parent != window) { + window.addEventListener("pageshow", event => { window.parent.resolveAppStartEvent("pageshow"); }) + } + + // receive blazor benchmark event and forward it to resolveAppStartEvent + window.receiveBenchmarkEvent = function (name) { + if (window !== window.parent) { + window.parent.resolveAppStartEvent("blazor: " + name); + } + }; + + window.muteErrors = () => { + mute = true; + } + + const urlParams = new URLSearchParams(window.location.search); + const myParam = urlParams.get('memorySnapshot'); + + Blazor.start({ + configureRuntime: dotnet => { + if (myParam === "true") { + dotnet.withStartupMemoryCache(true); + } + } + }); + +} +catch (err) { + if (!mute) { + console.error(`WASM ERROR ${err}`); + } +} diff --git a/src/mono/sample/wasm/browser-bench/AppStart.cs b/src/mono/sample/wasm/browser-bench/AppStart.cs index 045449c2522a6..411c5cc8c925c 100644 --- a/src/mono/sample/wasm/browser-bench/AppStart.cs +++ b/src/mono/sample/wasm/browser-bench/AppStart.cs @@ -134,6 +134,7 @@ class BlazorReachManagedSnapshot : BlazorAppStartMeasurement public override string Name => "Blazor Reach managed snapshot"; public override int InitialSamples => 3; public override bool HasRunStepAsync => true; + protected override string FramePage => "start.html?memorySnapshot=true"; public override async Task RunStepAsync() { @@ -224,7 +225,7 @@ class BrowserReachManagedSnapshot : BrowserAppStartMeasurement public override string Name => "Browser Reach managed snapshot"; public override int InitialSamples => 3; public override bool HasRunStepAsync => true; - protected override string FramePage => "?memorySnapshot=true"; + protected override string FramePage => "start.html?memorySnapshot=true"; public override async Task RunStepAsync() { diff --git a/src/mono/sample/wasm/browser-frame/wwwroot/main.js b/src/mono/sample/wasm/browser-frame/wwwroot/main.js index 25d884bca3620..a073fc9cf7032 100644 --- a/src/mono/sample/wasm/browser-frame/wwwroot/main.js +++ b/src/mono/sample/wasm/browser-frame/wwwroot/main.js @@ -3,12 +3,8 @@ import { dotnet } from './_framework/dotnet.js' -const urlParams = new URLSearchParams(window.location.search); -const myParam = urlParams.get('memorySnapshot'); - const { setModuleImports, getAssemblyExports, getConfig } = await dotnet .withDiagnosticTracing(false) - .withStartupMemoryCache(myParam === "true") .withApplicationArgumentsFromQuery() .create(); diff --git a/src/mono/sample/wasm/browser-frame/wwwroot/start.html b/src/mono/sample/wasm/browser-frame/wwwroot/start.html new file mode 100644 index 0000000000000..9dad19ec0a1cd --- /dev/null +++ b/src/mono/sample/wasm/browser-frame/wwwroot/start.html @@ -0,0 +1,18 @@ + + + + + + + browser-frame + + + + + + + + + + + diff --git a/src/mono/sample/wasm/browser-frame/wwwroot/start.js b/src/mono/sample/wasm/browser-frame/wwwroot/start.js new file mode 100644 index 0000000000000..25d884bca3620 --- /dev/null +++ b/src/mono/sample/wasm/browser-frame/wwwroot/start.js @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +import { dotnet } from './_framework/dotnet.js' + +const urlParams = new URLSearchParams(window.location.search); +const myParam = urlParams.get('memorySnapshot'); + +const { setModuleImports, getAssemblyExports, getConfig } = await dotnet + .withDiagnosticTracing(false) + .withStartupMemoryCache(myParam === "true") + .withApplicationArgumentsFromQuery() + .create(); + +setModuleImports('main.js', { + window: { + location: { + href: () => globalThis.window.location.href + } + } +}); + +const config = getConfig(); +const exports = await getAssemblyExports(config.mainAssemblyName); +const text = exports.MyClass.Greeting(); +console.log(text); + +document.getElementById('out').innerHTML = text; +await dotnet.run(); \ No newline at end of file diff --git a/src/mono/sample/wasm/browser-webpack/Wasm.Browser.WebPack.Sample.csproj b/src/mono/sample/wasm/browser-webpack/Wasm.Browser.WebPack.Sample.csproj index 10a01beae6eda..8c59adb0a7b9e 100644 --- a/src/mono/sample/wasm/browser-webpack/Wasm.Browser.WebPack.Sample.csproj +++ b/src/mono/sample/wasm/browser-webpack/Wasm.Browser.WebPack.Sample.csproj @@ -36,7 +36,7 @@
- + diff --git a/src/mono/sample/wasm/wasm.mk b/src/mono/sample/wasm/wasm.mk index 95f3eb8143536..1594d8d58993b 100644 --- a/src/mono/sample/wasm/wasm.mk +++ b/src/mono/sample/wasm/wasm.mk @@ -38,7 +38,7 @@ run-browser: echo "The tool dotnet-serve could not be found. Install with: $(DOTNET) tool install --global dotnet-serve"; \ exit 1; \ else \ - $(DOTNET) serve -d:bin/$(CONFIG)/AppBundle $(CORS_HEADERS) $(OPEN_BROWSER) -p:8000; \ + $(DOTNET) serve -S -d:bin/$(CONFIG)/AppBundle $(CORS_HEADERS) $(OPEN_BROWSER) -p:8000; \ fi run-console: