Skip to content

Commit 2e8148c

Browse files
authoredMar 22, 2023
[wasm] Cache files in simple server (#83744)
* [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
1 parent 7500625 commit 2e8148c

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed
 

‎src/mono/sample/wasm/browser-bench/appstart-frame.html

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<link rel="preload" href="./mono-config.json" as="fetch" crossorigin="anonymous">
1313
<link rel="prefetch" href="./dotnet.wasm" as="fetch" crossorigin="anonymous">
1414
<!-- users should consider if they optimize for the first load or subsequent load from memory snapshot -->
15-
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
1615
<link rel="prefetch" href="./managed/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
1716
</head>
1817

‎src/mono/sample/wasm/simple-server/Program.cs

+38-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Collections.Concurrent;
77
using System.Runtime.InteropServices;
8+
using System;
89

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

2325
public static int Main()
2426
{
@@ -83,7 +85,7 @@ private void OpenUrl(string url)
8385
}
8486
else
8587
{
86-
System.Console.WriteLine("Don't know how to open url on this OS platform");
88+
Console.WriteLine("Don't know how to open url on this OS platform");
8789
}
8890

8991
proc.StartInfo = si;
@@ -102,6 +104,31 @@ private void HandleRequest(HttpListener listener)
102104
ReceivePostAsync(context);
103105
}
104106

107+
private async Task<byte[]?> GetFileContent(string path)
108+
{
109+
if (Verbose)
110+
await Console.Out.WriteLineAsync($"get content for: {path}");
111+
112+
if (cache.ContainsKey(path))
113+
{
114+
if (Verbose)
115+
await Console.Out.WriteLineAsync($"returning cached content for: {path}");
116+
117+
return cache[path];
118+
}
119+
120+
var content = await File.ReadAllBytesAsync(path).ConfigureAwait(false);
121+
if (content == null)
122+
return null;
123+
124+
if (Verbose)
125+
await Console.Out.WriteLineAsync($"adding content to cache for: {path}");
126+
127+
cache[path] = content;
128+
129+
return content;
130+
}
131+
105132
private async void ReceivePostAsync(HttpListenerContext context)
106133
{
107134
if (Verbose)
@@ -165,15 +192,17 @@ private async void ServeAsync(HttpListenerContext context)
165192
byte[]? buffer;
166193
try
167194
{
168-
buffer = await File.ReadAllBytesAsync(path).ConfigureAwait(false);
169-
if (throttleMbps > 0) {
195+
buffer = await GetFileContent(path);
196+
197+
if (buffer != null && throttleMbps > 0)
198+
{
170199
double delaySeconds = (buffer.Length * 8) / (throttleMbps * 1024 * 1024);
171200
int delayMs = (int)(delaySeconds * 1000);
172-
if(session != null)
201+
if (session != null)
173202
{
174203
Task currentDelay;
175204
int myIndex;
176-
lock(session)
205+
lock (session)
177206
{
178207
currentDelay = session.CurrentDelay;
179208
myIndex = session.Started;
@@ -185,10 +214,10 @@ private async void ServeAsync(HttpListenerContext context)
185214
// wait for everybody else to finish in this while loop
186215
await currentDelay;
187216

188-
lock(session)
217+
lock (session)
189218
{
190219
// it's my turn to insert delay for others
191-
if(session.Finished == myIndex)
220+
if (session.Finished == myIndex)
192221
{
193222
session.CurrentDelay = Task.Delay(delayMs);
194223
break;
@@ -202,10 +231,10 @@ private async void ServeAsync(HttpListenerContext context)
202231
// wait my own delay
203232
await Task.Delay(delayMs + latencyMs);
204233

205-
lock(session)
234+
lock (session)
206235
{
207236
session.Finished++;
208-
if(session.Finished == session.Started)
237+
if (session.Finished == session.Started)
209238
{
210239
Sessions.TryRemove(sessionId!, out _);
211240
}

0 commit comments

Comments
 (0)