Skip to content

Commit

Permalink
[wasm] Cache files in simple server (#83744)
Browse files Browse the repository at this point in the history
* [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.

* Use StringComparer.OrdinalIgnoreCase for the cache
  • Loading branch information
radekdoulik authored Mar 22, 2023
1 parent 7500625 commit 2e8148c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<link rel="preload" href="./mono-config.json" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.wasm" as="fetch" crossorigin="anonymous">
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./managed/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
</head>

Expand Down
47 changes: 38 additions & 9 deletions src/mono/sample/wasm/simple-server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using System;

namespace HttpServer
{
Expand All @@ -19,6 +20,7 @@ public sealed class Program
{
private bool Verbose = false;
private ConcurrentDictionary<string, Session> Sessions = new ConcurrentDictionary<string, Session>();
private Dictionary<string, byte[]> cache = new Dictionary<string, byte[]>(StringComparer.OrdinalIgnoreCase);

public static int Main()
{
Expand Down Expand Up @@ -83,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;
Expand All @@ -102,6 +104,31 @@ private void HandleRequest(HttpListener listener)
ReceivePostAsync(context);
}

private async Task<byte[]?> 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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 _);
}
Expand Down

0 comments on commit 2e8148c

Please sign in to comment.