-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a benchmark project. Currently only one benchmark which compare…
…s performance to wasmtime. This project actually beats wasmtime! The benchmark runs a function called `bench`, which is adapted from a random Rust project I had lying around (calculating various rocket nozzle parameters). I'm happy to share the code for this if you want, but I'm not sure what the best way to do it is - would you just want the entire Rust project included in the wasm folder?
- Loading branch information
1 parent
69ecd4e
commit 2f30e18
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" /> | ||
<PackageReference Include="Wasmtime" Version="19.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WebAssembly\WebAssembly.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="wasm\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using Wasmtime; | ||
using WebAssembly; | ||
using WebAssembly.Runtime; | ||
|
||
namespace Benchmark; | ||
|
||
public class CompareWasmtimeDotnet | ||
{ | ||
private Instance<Exports> _dotnet = null!; | ||
private Instance<dynamic> _dotnetDynamic = null!; | ||
private Func<int> _wasmtime = null!; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
const string path = "wasm/inthrustwetrust.wasm"; | ||
|
||
LoadDotnetWebassembly(path); | ||
LoadWasmtimeDotnet(path); | ||
} | ||
|
||
private void LoadDotnetWebassembly(string path) | ||
{ | ||
using var stream = File.OpenRead(path); | ||
_dotnet = Compile.FromBinary<Exports>(stream)(new ImportDictionary()); | ||
|
||
stream.Position = 0; | ||
_dotnetDynamic = Compile.FromBinary<dynamic>(stream)(new ImportDictionary()); | ||
} | ||
|
||
private void LoadWasmtimeDotnet(string path) | ||
{ | ||
Engine engine = new(new Config()); | ||
|
||
var module = Wasmtime.Module.FromFile(engine, path); | ||
var store = new Store(engine); | ||
|
||
var instance = new Instance(store, module); | ||
_wasmtime = instance.GetFunction<int>("bench") ?? throw new InvalidOperationException("Missing 'bench' function"); | ||
} | ||
|
||
[Benchmark] | ||
public void DotnetWebassembly() | ||
{ | ||
_dotnet.Exports.bench(); | ||
} | ||
|
||
[Benchmark] | ||
public void DotnetWebassemblyDynamic() | ||
{ | ||
_dotnetDynamic.Exports.bench(); | ||
} | ||
|
||
[Benchmark] | ||
public void WasmtimeDotnet() | ||
{ | ||
_wasmtime(); | ||
} | ||
|
||
public abstract class Exports | ||
{ | ||
public abstract int bench(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Benchmark; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Running; | ||
|
||
BenchmarkSwitcher benchmark = BenchmarkSwitcher.FromTypes([ | ||
typeof(CompareWasmtimeDotnet), | ||
]); | ||
|
||
IConfig configuration = DefaultConfig.Instance; | ||
|
||
if (args.Length > 0) | ||
{ | ||
benchmark.Run(args, configuration); | ||
} | ||
else | ||
{ | ||
benchmark.RunAll(configuration); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters