-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from peterhuene/use-different-store
Use a different store for the host.
- Loading branch information
Showing
26 changed files
with
250 additions
and
88 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
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 |
---|---|---|
|
@@ -7,3 +7,5 @@ | |
|
||
bin/ | ||
obj/ | ||
|
||
BenchmarkDotNet.Artifacts/ |
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
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,79 @@ | ||
using System; | ||
using System.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Jobs; | ||
using BenchmarkDotNet.Running; | ||
using BenchmarkDotNet.Toolchains.InProcess.Emit; | ||
using Wasmtime; | ||
|
||
namespace Simple | ||
{ | ||
[Config(typeof(Config))] | ||
public class Benchmark | ||
{ | ||
private class Config : ManualConfig | ||
{ | ||
public Config() | ||
{ | ||
AddJob(Job.MediumRun | ||
.WithLaunchCount(1) | ||
.WithToolchain(InProcessEmitToolchain.Instance) | ||
.WithId("InProcess")); | ||
} | ||
} | ||
|
||
public Benchmark() | ||
{ | ||
_engine = new Engine(); | ||
_store = new Store(_engine); | ||
_module = _store.LoadModuleText("hello", @" | ||
(module | ||
(type $t0 (func)) | ||
(import """" ""hello"" (func $.hello (type $t0))) | ||
(func $run | ||
call $.hello | ||
) | ||
(export ""run"" (func $run)) | ||
)"); | ||
} | ||
|
||
[Benchmark] | ||
public void SayHello() | ||
{ | ||
using var host = new Host(_engine); | ||
|
||
host.DefineFunction("", "hello", () => { }); | ||
|
||
// Define a memory to add memory pressure if not disposed in the benchmark test | ||
using var memory = host.DefineMemory("", "memory", 3); | ||
|
||
using dynamic instance = host.Instantiate(_module); | ||
instance.run(); | ||
} | ||
|
||
private Engine _engine; | ||
private Store _store; | ||
private Module _module; | ||
} | ||
|
||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var summary = BenchmarkRunner.Run<Benchmark>(); | ||
|
||
var report = summary[summary.BenchmarksCases.Where(c => c.Descriptor.Type == typeof(Benchmark)).Single()]; | ||
if (!(report is null)) | ||
{ | ||
if (report.ExecuteResults.All(r => r.ExitCode == 0)) | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
Console.Error.WriteLine("Benchmark failed."); | ||
Environment.Exit(1); | ||
} | ||
} | ||
} |
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" /> | ||
<ProjectReference Include="..\..\src\Wasmtime.csproj" /> | ||
</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
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
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
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
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,57 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Collections.Generic; | ||
|
||
namespace Wasmtime | ||
{ | ||
/// <summary> | ||
/// Represents a WebAssembly engine. | ||
/// </summary> | ||
public class Engine : IDisposable | ||
{ | ||
/// <summary> | ||
/// Constructs a new default engine. | ||
/// </summary> | ||
public Engine() | ||
{ | ||
_handle = Interop.wasm_engine_new(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
if (!_handle.IsInvalid) | ||
{ | ||
_handle.Dispose(); | ||
_handle.SetHandleAsInvalid(); | ||
} | ||
} | ||
|
||
internal Engine(Interop.WasmConfigHandle config) | ||
{ | ||
if (config.IsInvalid) | ||
{ | ||
throw new WasmtimeException("Failed to create Wasmtime engine."); | ||
} | ||
|
||
_handle = Interop.wasm_engine_new_with_config(config); | ||
config.SetHandleAsInvalid(); | ||
} | ||
|
||
internal Interop.EngineHandle Handle | ||
{ | ||
get | ||
{ | ||
if (_handle.IsInvalid) | ||
{ | ||
throw new ObjectDisposedException(typeof(Engine).FullName); | ||
} | ||
|
||
return _handle; | ||
} | ||
} | ||
|
||
private Interop.EngineHandle _handle; | ||
} | ||
} |
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
Oops, something went wrong.