Add a go test driver.

Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2025-04-28 18:33:38 -07:00
parent d6826160c2
commit d9c36b94f8
5 changed files with 52 additions and 1 deletions

2
go.mod
View file

@ -1,3 +1,5 @@
module code.jmug.me/jmug/sqlite-wasm module code.jmug.me/jmug/sqlite-wasm
go 1.23.6 go 1.23.6
require github.com/tetratelabs/wazero v1.9.0

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM=

47
main.go Normal file
View file

@ -0,0 +1,47 @@
package main
import (
"context"
_ "embed"
"fmt"
"log"
"time"
"github.com/tetratelabs/wazero"
)
//go:embed testdata/zig-out/bin/addUpTo.wasm
var addUpToWasm []byte
func main() {
ctx := context.Background()
// Create a new WebAssembly Runtime.
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().WithCloseOnContextDone(true))
defer r.Close(ctx)
// Compile the Wasm binary once so that we can skip the entire compilation time during instantiation.
compiledWasm, err := r.CompileModule(ctx, addUpToWasm)
if err != nil {
log.Panicf("failed to compile Wasm binary: %v", err)
}
start := time.Now()
for range 1000 {
// Instantiate a new Wasm module from the already compiled `compiledWasm`.
instance, err := r.InstantiateModule(ctx, compiledWasm, wazero.NewModuleConfig().WithName(""))
if err != nil {
log.Panicf("failed to instantiate %v", err)
}
f := instance.ExportedFunction("addUpTo")
res, err := f.Call(ctx, 1000)
if err != nil {
log.Panicf("failed to call function: %v", err)
} else {
log.Printf("Got %d from wasm\n", res[0])
}
}
end := time.Now()
fmt.Printf("Took %v to run functions\n", end.Sub(start))
}

2
testdata/build.zig vendored
View file

@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void {
const add_up_to_mod = b.createModule(.{ const add_up_to_mod = b.createModule(.{
.root_source_file = b.path("addupto.zig"), .root_source_file = b.path("addupto.zig"),
.target = wasmTarget, .target = wasmTarget,
.optimize = .ReleaseFast, .optimize = .ReleaseSmall,
}); });
const add_up_to_native_mod = b.createModule(.{ const add_up_to_native_mod = b.createModule(.{
.root_source_file = b.path("addupto.zig"), .root_source_file = b.path("addupto.zig"),

Binary file not shown.