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

Benchmark Project #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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