Skip to content

Commit

Permalink
Added a benchmark project. Currently only one benchmark which compare…
Browse files Browse the repository at this point in the history
…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
martindevans committed Jul 2, 2024
1 parent 69ecd4e commit 2f30e18
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Benchmark/Benchmark.csproj
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>
65 changes: 65 additions & 0 deletions Benchmark/CompareWasmtimeDotnet.cs
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();
}
}
18 changes: 18 additions & 0 deletions Benchmark/Program.cs
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 added Benchmark/wasm/inthrustwetrust.wasm
Binary file not shown.
11 changes: 11 additions & 0 deletions WebAssembly.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RunExisting", "Examples\Run
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GenerateClassFromWasm", "Examples\GenerateClassFromWasm\GenerateClassFromWasm.csproj", "{447571E7-CE40-4A4D-86E1-104C0141D13B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmark", "Benchmark\Benchmark.csproj", "{6007EA0B-C7A2-48DD-AE35-5FF66A4F388D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -72,6 +74,14 @@ Global
{447571E7-CE40-4A4D-86E1-104C0141D13B}.Release|Any CPU.Build.0 = Release|Any CPU
{447571E7-CE40-4A4D-86E1-104C0141D13B}.Release|x86.ActiveCfg = Release|Any CPU
{447571E7-CE40-4A4D-86E1-104C0141D13B}.Release|x86.Build.0 = Release|Any CPU
{6007EA0B-C7A2-48DD-AE35-5FF66A4F388D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6007EA0B-C7A2-48DD-AE35-5FF66A4F388D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6007EA0B-C7A2-48DD-AE35-5FF66A4F388D}.Debug|x86.ActiveCfg = Debug|Any CPU
{6007EA0B-C7A2-48DD-AE35-5FF66A4F388D}.Debug|x86.Build.0 = Debug|Any CPU
{6007EA0B-C7A2-48DD-AE35-5FF66A4F388D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6007EA0B-C7A2-48DD-AE35-5FF66A4F388D}.Release|Any CPU.Build.0 = Release|Any CPU
{6007EA0B-C7A2-48DD-AE35-5FF66A4F388D}.Release|x86.ActiveCfg = Release|Any CPU
{6007EA0B-C7A2-48DD-AE35-5FF66A4F388D}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -80,6 +90,7 @@ Global
{AAC685B0-BD34-4BDF-9283-E7712B83B2F3} = {8B170A86-42D9-4F76-9230-7E8FC5C242D9}
{E4A73D5E-5A53-4B6C-8034-E350ADC649EC} = {8B170A86-42D9-4F76-9230-7E8FC5C242D9}
{447571E7-CE40-4A4D-86E1-104C0141D13B} = {8B170A86-42D9-4F76-9230-7E8FC5C242D9}
{6007EA0B-C7A2-48DD-AE35-5FF66A4F388D} = {8B170A86-42D9-4F76-9230-7E8FC5C242D9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3B09C795-39DB-4388-9441-FA9401CFD4BE}
Expand Down

0 comments on commit 2f30e18

Please sign in to comment.