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)) }