From c5ddedb2416bbc38b606289779987557a0598e04 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Tue, 21 Mar 2023 21:26:43 +0100 Subject: [PATCH 1/2] [wasm] Cache files in simple server This improves measurements comparison between hosts with different storage speeds. In our case local storage vs NFS storage on the otherwise equal hardware. The cache is filled during initial runs of browser-bench. Also do not prefetch `icudt.dat` as it is not used anymore. --- .../wasm/browser-bench/appstart-frame.html | 1 - src/mono/sample/wasm/simple-server/Program.cs | 45 +++++++++++++++---- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/mono/sample/wasm/browser-bench/appstart-frame.html b/src/mono/sample/wasm/browser-bench/appstart-frame.html index b54590f423895..4a1945c9aad6d 100644 --- a/src/mono/sample/wasm/browser-bench/appstart-frame.html +++ b/src/mono/sample/wasm/browser-bench/appstart-frame.html @@ -12,7 +12,6 @@ - diff --git a/src/mono/sample/wasm/simple-server/Program.cs b/src/mono/sample/wasm/simple-server/Program.cs index f02f8cdd77000..10407c58c10fc 100644 --- a/src/mono/sample/wasm/simple-server/Program.cs +++ b/src/mono/sample/wasm/simple-server/Program.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Collections.Concurrent; using System.Runtime.InteropServices; +using System; namespace HttpServer { @@ -19,6 +20,7 @@ public sealed class Program { private bool Verbose = false; private ConcurrentDictionary Sessions = new ConcurrentDictionary(); + private Dictionary cache = new Dictionary(); public static int Main() { @@ -102,6 +104,31 @@ private void HandleRequest(HttpListener listener) ReceivePostAsync(context); } + private async Task GetFileContent(string path) + { + if (Verbose) + await Console.Out.WriteLineAsync($"get content for: {path}"); + + if (cache.ContainsKey(path)) + { + if (Verbose) + await Console.Out.WriteLineAsync($"returning cached content for: {path}"); + + return cache[path]; + } + + var content = await File.ReadAllBytesAsync(path).ConfigureAwait(false); + if (content == null) + return null; + + if (Verbose) + await Console.Out.WriteLineAsync($"adding content to cache for: {path}"); + + cache[path] = content; + + return content; + } + private async void ReceivePostAsync(HttpListenerContext context) { if (Verbose) @@ -165,15 +192,17 @@ private async void ServeAsync(HttpListenerContext context) byte[]? buffer; try { - buffer = await File.ReadAllBytesAsync(path).ConfigureAwait(false); - if (throttleMbps > 0) { + buffer = await GetFileContent(path); + + if (buffer != null && throttleMbps > 0) + { double delaySeconds = (buffer.Length * 8) / (throttleMbps * 1024 * 1024); int delayMs = (int)(delaySeconds * 1000); - if(session != null) + if (session != null) { Task currentDelay; int myIndex; - lock(session) + lock (session) { currentDelay = session.CurrentDelay; myIndex = session.Started; @@ -185,10 +214,10 @@ private async void ServeAsync(HttpListenerContext context) // wait for everybody else to finish in this while loop await currentDelay; - lock(session) + lock (session) { // it's my turn to insert delay for others - if(session.Finished == myIndex) + if (session.Finished == myIndex) { session.CurrentDelay = Task.Delay(delayMs); break; @@ -202,10 +231,10 @@ private async void ServeAsync(HttpListenerContext context) // wait my own delay await Task.Delay(delayMs + latencyMs); - lock(session) + lock (session) { session.Finished++; - if(session.Finished == session.Started) + if (session.Finished == session.Started) { Sessions.TryRemove(sessionId!, out _); } From 47a99bd27ca71e499e7a6d2c2d86d700bbf40ce8 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Tue, 21 Mar 2023 22:40:31 +0100 Subject: [PATCH 2/2] Use StringComparer.OrdinalIgnoreCase for the cache --- src/mono/sample/wasm/simple-server/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mono/sample/wasm/simple-server/Program.cs b/src/mono/sample/wasm/simple-server/Program.cs index 10407c58c10fc..6d1148f9d2251 100644 --- a/src/mono/sample/wasm/simple-server/Program.cs +++ b/src/mono/sample/wasm/simple-server/Program.cs @@ -20,7 +20,7 @@ public sealed class Program { private bool Verbose = false; private ConcurrentDictionary Sessions = new ConcurrentDictionary(); - private Dictionary cache = new Dictionary(); + private Dictionary cache = new Dictionary(StringComparer.OrdinalIgnoreCase); public static int Main() { @@ -85,7 +85,7 @@ private void OpenUrl(string url) } else { - System.Console.WriteLine("Don't know how to open url on this OS platform"); + Console.WriteLine("Don't know how to open url on this OS platform"); } proc.StartInfo = si;