From 74e3c4f0e03c5db5d32d051b7abcf186d266ea60 Mon Sep 17 00:00:00 2001 From: Nathan Ricci Date: Thu, 12 Aug 2021 16:24:37 -0400 Subject: [PATCH] Make wasm-interpreter work like wasm-aot (#1769) --- samples/BenchmarkDotNet.Samples/IntroWasm.cs | 3 +- .../ConsoleArguments/CommandLineOptions.cs | 3 -- .../ConsoleArguments/ConfigParser.cs | 11 +--- .../Environments/Runtimes/WasmRuntime.cs | 21 ++------ src/BenchmarkDotNet/Templates/WasmCsProj.txt | 25 ++++----- .../Toolchains/MonoWasm/WasmBuilder.cs | 53 ------------------- .../Toolchains/MonoWasm/WasmGenerator.cs | 8 --- 7 files changed, 15 insertions(+), 109 deletions(-) diff --git a/samples/BenchmarkDotNet.Samples/IntroWasm.cs b/samples/BenchmarkDotNet.Samples/IntroWasm.cs index 6d0ea6e27a..cf50d5671d 100644 --- a/samples/BenchmarkDotNet.Samples/IntroWasm.cs +++ b/samples/BenchmarkDotNet.Samples/IntroWasm.cs @@ -35,10 +35,9 @@ public class IntroWasmFluentConfig public static void Run() { // the Wasm Toolchain requires two mandatory arguments: - const string mainJsPath = @"/home/adam/projects/runtime/src/mono/wasm/runtime-test.js"; const string cliPath = @"/home/adam/projects/runtime/dotnet.sh"; - WasmRuntime runtime = new WasmRuntime(mainJs: new FileInfo(mainJsPath), msBuildMoniker: "net5.0"); + WasmRuntime runtime = new WasmRuntime(msBuildMoniker: "net5.0"); NetCoreAppSettings netCoreAppSettings = new NetCoreAppSettings( targetFrameworkMoniker: "net5.0", runtimeFrameworkVersion: null, name: "Wasm", customDotNetCliPath: cliPath); diff --git a/src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs b/src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs index 0295badacc..0be8d39310 100644 --- a/src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs +++ b/src/BenchmarkDotNet/ConsoleArguments/CommandLineOptions.cs @@ -175,9 +175,6 @@ public class CommandLineOptions [Option("wasmEngine", Required = false, HelpText = "Full path to a java script engine used to run the benchmarks, used by Wasm toolchain.")] public FileInfo WasmJavascriptEngine { get; set; } - [Option("wasmMainJS", Required = false, HelpText = "Path to the main.js file used by Wasm toolchain. Mandatory when using \"--runtimes wasm\"")] - public FileInfo WasmMainJs { get; set; } - [Option("wasmArgs", Required = false, Default = "--expose_wasm", HelpText = "Arguments for the javascript engine used by Wasm toolchain.")] public string WasmJavaScriptEngineArguments { get; set; } diff --git a/src/BenchmarkDotNet/ConsoleArguments/ConfigParser.cs b/src/BenchmarkDotNet/ConsoleArguments/ConfigParser.cs index cd2b3b0c07..d7fd9e34e6 100644 --- a/src/BenchmarkDotNet/ConsoleArguments/ConfigParser.cs +++ b/src/BenchmarkDotNet/ConsoleArguments/ConfigParser.cs @@ -112,19 +112,11 @@ private static bool Validate(CommandLineOptions options, ILogger logger) logger.WriteLineError($"The provided runtime \"{runtime}\" is invalid. Available options are: {string.Join(", ", Enum.GetNames(typeof(RuntimeMoniker)).Select(name => name.ToLower()))}."); return false; } - else if (runtimeMoniker == RuntimeMoniker.Wasm && options.WasmMainJs == null && options.RuntimeSrcDir == null) { - logger.WriteLine("Either runtimeSrcDir or WasmMainJS must be specified."); - } - else if (runtimeMoniker == RuntimeMoniker.Wasm && options.WasmMainJs.IsNotNullButDoesNotExist()) - { - logger.WriteLineError($"The provided {nameof(options.WasmMainJs)} \"{options.WasmMainJs}\" does NOT exist."); - return false; - } else if (runtimeMoniker == RuntimeMoniker.MonoAOTLLVM && (options.AOTCompilerPath == null || options.AOTCompilerPath.IsNotNullButDoesNotExist())) { logger.WriteLineError($"The provided {nameof(options.AOTCompilerPath)} \"{ options.AOTCompilerPath }\" does NOT exist. It MUST be provided."); } - else if (runtimeMoniker == RuntimeMoniker.Wasm && options.AOTCompilerMode == MonoAotCompilerMode.wasm && (options.RuntimeSrcDir == null || options.RuntimeSrcDir.IsNotNullButDoesNotExist())) + else if (runtimeMoniker == RuntimeMoniker.Wasm && (options.RuntimeSrcDir == null || options.RuntimeSrcDir.IsNotNullButDoesNotExist())) { logger.WriteLineError($"The provided {nameof(options.RuntimeSrcDir)} \"{options.RuntimeSrcDir}\" does NOT exist. It MUST be provided for wasm-aot."); return false; @@ -425,7 +417,6 @@ private static Job MakeWasmJob(Job baseJob, CommandLineOptions options, TimeSpan bool wasmAot = options.AOTCompilerMode == MonoAotCompilerMode.wasm; var wasmRuntime = new WasmRuntime( - mainJs: options.WasmMainJs ?? new FileInfo(Path.Combine(options.RuntimeSrcDir.FullName, "src", "mono", "wasm", "runtime-test.js")), msBuildMoniker: msBuildMoniker, javaScriptEngine: options.WasmJavascriptEngine?.FullName ?? "v8", javaScriptEngineArguments: options.WasmJavaScriptEngineArguments, diff --git a/src/BenchmarkDotNet/Environments/Runtimes/WasmRuntime.cs b/src/BenchmarkDotNet/Environments/Runtimes/WasmRuntime.cs index 3249f003ff..00ca68b182 100644 --- a/src/BenchmarkDotNet/Environments/Runtimes/WasmRuntime.cs +++ b/src/BenchmarkDotNet/Environments/Runtimes/WasmRuntime.cs @@ -12,8 +12,6 @@ public class WasmRuntime : Runtime, IEquatable [EditorBrowsable(EditorBrowsableState.Never)] internal static readonly WasmRuntime Default = new WasmRuntime(); - public FileInfo MainJs { get; } - public string JavaScriptEngine { get; } public string JavaScriptEngineArguments { get; } @@ -31,37 +29,24 @@ public class WasmRuntime : Runtime, IEquatable /// moniker, default: "net5.0" /// default: "Wasm" /// path to mainJs MUST be provided - public WasmRuntime(FileInfo mainJs, string msBuildMoniker = "net5.0", string displayName = "Wasm", string javaScriptEngine = "v8", string javaScriptEngineArguments = "--expose_wasm", bool aot = false, DirectoryInfo runtimeSrcDir = null) : base(RuntimeMoniker.Wasm, msBuildMoniker, displayName) + public WasmRuntime(string msBuildMoniker = "net5.0", string displayName = "Wasm", string javaScriptEngine = "v8", string javaScriptEngineArguments = "--expose_wasm", bool aot = false, DirectoryInfo runtimeSrcDir = null) : base(RuntimeMoniker.Wasm, msBuildMoniker, displayName) { - if (!aot && mainJs == null) - throw new ArgumentNullException(paramName: nameof(mainJs)); - if (!aot && mainJs.IsNotNullButDoesNotExist()) - throw new FileNotFoundException($"Provided {nameof(mainJs)} file: \"{mainJs.FullName}\" doest NOT exist"); if (!string.IsNullOrEmpty(javaScriptEngine) && javaScriptEngine != "v8" && !File.Exists(javaScriptEngine)) throw new FileNotFoundException($"Provided {nameof(javaScriptEngine)} file: \"{javaScriptEngine}\" doest NOT exist"); - MainJs = mainJs; JavaScriptEngine = javaScriptEngine; JavaScriptEngineArguments = javaScriptEngineArguments; Aot = aot; RuntimeSrcDir = runtimeSrcDir; } - // this ctor exists only for the purpose of having .Default property that returns something consumable by RuntimeInformation.GetCurrentRuntime() - private WasmRuntime(string msBuildMoniker = "net5.0", string displayName = "Wasm", string javaScriptEngine = "v8", string javaScriptEngineArguments = "--expose_wasm") : base(RuntimeMoniker.Wasm, msBuildMoniker, displayName) - { - MainJs = new FileInfo("fake"); - JavaScriptEngine = javaScriptEngine; - JavaScriptEngineArguments = javaScriptEngineArguments; - } - public override bool Equals(object obj) => obj is WasmRuntime other && Equals(other); public bool Equals(WasmRuntime other) - => other != null && base.Equals(other) && other.MainJs == MainJs && other.JavaScriptEngine == JavaScriptEngine && other.JavaScriptEngineArguments == JavaScriptEngineArguments && other.Aot == Aot && other.RuntimeSrcDir == RuntimeSrcDir; + => other != null && base.Equals(other) && other.JavaScriptEngine == JavaScriptEngine && other.JavaScriptEngineArguments == JavaScriptEngineArguments && other.Aot == Aot && other.RuntimeSrcDir == RuntimeSrcDir; public override int GetHashCode() - => base.GetHashCode() ^ MainJs.GetHashCode() ^ (JavaScriptEngine?.GetHashCode() ?? 0) ^ (JavaScriptEngineArguments?.GetHashCode() ?? 0 ^ Aot.GetHashCode() ^ (RuntimeSrcDir?.GetHashCode() ?? 0)); + => base.GetHashCode() ^ (JavaScriptEngine?.GetHashCode() ?? 0) ^ (JavaScriptEngineArguments?.GetHashCode() ?? 0 ^ Aot.GetHashCode() ^ (RuntimeSrcDir?.GetHashCode() ?? 0)); } } diff --git a/src/BenchmarkDotNet/Templates/WasmCsProj.txt b/src/BenchmarkDotNet/Templates/WasmCsProj.txt index 7f8f4b969a..cdeec4be27 100644 --- a/src/BenchmarkDotNet/Templates/WasmCsProj.txt +++ b/src/BenchmarkDotNet/Templates/WasmCsProj.txt @@ -1,37 +1,32 @@ - + Exe bin $RUNTIMESRCDIR$ + Release false $TFM$ true $(MSBuildThisFileDirectory)\bin\$TFM$\browser-wasm\publish $PROGRAMNAME$ browser-wasm - $MAINJS$ - $RUNTIMEPACK$ - false + false + false + $(RuntimeSrcDir)\src\mono\wasm\runtime-test.js + true false false + false $COPIEDSETTINGS$ + + + - - - - - - - - diff --git a/src/BenchmarkDotNet/Toolchains/MonoWasm/WasmBuilder.cs b/src/BenchmarkDotNet/Toolchains/MonoWasm/WasmBuilder.cs index 3883ee596d..cf389b005b 100644 --- a/src/BenchmarkDotNet/Toolchains/MonoWasm/WasmBuilder.cs +++ b/src/BenchmarkDotNet/Toolchains/MonoWasm/WasmBuilder.cs @@ -28,61 +28,8 @@ public BuildResult Build(GenerateResult generateResult, BuildPartition buildPart WasmRuntime runtime = (WasmRuntime)buildPartition.Runtime; - if (buildResult.IsBuildSuccess && !runtime.Aot) - { - BuildApp(buildPartition.ProgramName, generateResult.ArtifactsPaths.BuildArtifactsDirectoryPath, runtime); - } - return buildResult; } - private void BuildApp(string programName, string projectRoot, WasmRuntime runtime) - { - string appDir = Path.Combine(projectRoot, $"bin", targetFrameworkMoniker, "browser-wasm", "publish"); - string outputDir = Path.Combine(appDir, "output"); - - string mainAssemblyPath = Path.Combine(appDir, $"{programName}.dll"); - - if (!File.Exists(mainAssemblyPath)) - throw new ArgumentException($"File MainAssembly='{mainAssemblyPath}' doesn't exist."); - - var assemblies = Directory.GetFiles(appDir, "*.dll"); - - // Create app - Directory.CreateDirectory(outputDir); - Directory.CreateDirectory(Path.Combine(outputDir, "managed")); - foreach (var assembly in assemblies) - File.Copy(assembly, Path.Combine(outputDir, "managed", Path.GetFileName(assembly)), true); - - string timeZoneDat = "dotnet.timezones.blat"; - string icutDat = "icudt.dat"; - - foreach (var f in new string[] { "dotnet.wasm", "dotnet.js", timeZoneDat, icutDat }) - File.Copy(Path.Combine(appDir, f), Path.Combine(outputDir, f), true); - - File.Copy(runtime.MainJs.FullName, Path.Combine(outputDir, "runtime.js"), true); - - using (var sw = File.CreateText(Path.Combine(outputDir, "mono-config.js"))) - { - sw.WriteLine("config = {"); - sw.WriteLine("\t\"assembly_root\": \"managed\","); - sw.WriteLine("\t\"enable_debugging\": 0,"); - sw.WriteLine("\t\"assets\": ["); - - foreach (var assembly in assemblies) - { - sw.Write($"\t\t{{ \"behavior\": \"assembly\", \"name\": \"{ Path.GetFileName(assembly)}\" }}"); - sw.WriteLine(","); - } - - sw.WriteLine($"\t\t{{ \"behavior\": \"icu\", \"name\": \"icudt.dat\", \"load_remote\": false}},"); - sw.WriteLine($"\t\t{{ \"behavior\": \"vfs\", \"name\": \"{timeZoneDat}\", \"virtual_path\": \"/usr/share/zoneinfo/\" }}"); - - sw.WriteLine("\t],"); - sw.WriteLine("\t\"files_to_map\": []"); - - sw.WriteLine("};"); - } - } } } diff --git a/src/BenchmarkDotNet/Toolchains/MonoWasm/WasmGenerator.cs b/src/BenchmarkDotNet/Toolchains/MonoWasm/WasmGenerator.cs index b44d0a01c3..08c60ced37 100644 --- a/src/BenchmarkDotNet/Toolchains/MonoWasm/WasmGenerator.cs +++ b/src/BenchmarkDotNet/Toolchains/MonoWasm/WasmGenerator.cs @@ -85,7 +85,6 @@ protected void GenerateProjectInterpreter(BuildPartition buildPartition, Artifac .Replace("$SDKNAME$", sdkName) .Replace("$RUNTIMEPACK$", CustomRuntimePack ?? "") .Replace("$TARGET$", CustomRuntimePack != null ? "PublishWithCustomRuntimePack" : "Publish") - .Replace("$MAINJS$", runtime.MainJs.ToString()) .ToString(); File.WriteAllText(artifactsPaths.ProjectFilePath, content); @@ -96,14 +95,7 @@ protected void GenerateProjectInterpreter(BuildPartition buildPartition, Artifac protected override string GetBinariesDirectoryPath(string buildArtifactsDirectoryPath, string configuration) { - if (Aot) - { return Path.Combine(buildArtifactsDirectoryPath, "bin", TargetFrameworkMoniker, "browser-wasm", "AppBundle"); - } - else - { - return Path.Combine(buildArtifactsDirectoryPath, "bin", TargetFrameworkMoniker, "browser-wasm", "publish", "output"); - } } } }