Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Cache files in simple server #83744

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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">
radekdoulik marked this conversation as resolved.
Show resolved Hide resolved
<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