|
| 1 | +// Command wago_host runs a generated json-as runtime benchmark on WAGO. |
| 2 | +// |
| 3 | +// The benchmark keeps timing inside WebAssembly. This host only supplies the |
| 4 | +// small env ABI used by the AssemblyScript bench library and forwards result |
| 5 | +// records to stdout for scripts/run-bench.runtimes.sh. |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "encoding/binary" |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | + "time" |
| 13 | + "unicode/utf16" |
| 14 | + |
| 15 | + wago "github.com/wago-org/wago" |
| 16 | +) |
| 17 | + |
| 18 | +var epoch = time.Now() |
| 19 | + |
| 20 | +func liftString(m wago.HostModule, ptr uint32) string { |
| 21 | + mem := m.Memory() |
| 22 | + if ptr == 0 || ptr < 4 || uint64(ptr) > uint64(len(mem)) { |
| 23 | + return "" |
| 24 | + } |
| 25 | + |
| 26 | + byteLen := binary.LittleEndian.Uint32(mem[ptr-4 : ptr]) |
| 27 | + end := uint64(ptr) + uint64(byteLen) |
| 28 | + if byteLen%2 != 0 || end > uint64(len(mem)) { |
| 29 | + return "" |
| 30 | + } |
| 31 | + |
| 32 | + units := make([]uint16, byteLen/2) |
| 33 | + for i := range units { |
| 34 | + off := int(ptr) + i*2 |
| 35 | + units[i] = binary.LittleEndian.Uint16(mem[off : off+2]) |
| 36 | + } |
| 37 | + return string(utf16.Decode(units)) |
| 38 | +} |
| 39 | + |
| 40 | +func fail(format string, args ...any) { |
| 41 | + fmt.Fprintf(os.Stderr, "wago_host: "+format+"\n", args...) |
| 42 | + os.Exit(1) |
| 43 | +} |
| 44 | + |
| 45 | +func trace(message string) { |
| 46 | + if os.Getenv("WAGO_HOST_TRACE") != "" { |
| 47 | + fmt.Fprintln(os.Stderr, "wago_host:", message) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func main() { |
| 52 | + if len(os.Args) != 2 { |
| 53 | + fail("usage: wago_host <module.wasm>") |
| 54 | + } |
| 55 | + |
| 56 | + wasm, err := os.ReadFile(os.Args[1]) |
| 57 | + if err != nil { |
| 58 | + fail("read module: %v", err) |
| 59 | + } |
| 60 | + if !wago.GuardPageSupported() { |
| 61 | + fail("guard-page bounds are unavailable; build with -tags wago_guardpage") |
| 62 | + } |
| 63 | + |
| 64 | + trace("compiling module") |
| 65 | + config := wago.NewRuntimeConfig().WithBoundsChecks(wago.BoundsChecksSignalsBased) |
| 66 | + compiled, err := wago.Compile(config, wasm) |
| 67 | + if err != nil { |
| 68 | + fail("compile module: %v", err) |
| 69 | + } |
| 70 | + defer compiled.Close() |
| 71 | + trace("module compiled") |
| 72 | + |
| 73 | + imports := wago.Imports{ |
| 74 | + "env.performance.now": wago.HostFunc(func(_ wago.HostModule, _ []uint64, results []uint64) { |
| 75 | + results[0] = wago.F64(float64(time.Since(epoch).Nanoseconds()) / 1e6) |
| 76 | + }), |
| 77 | + "env.Date.now": wago.HostFunc(func(_ wago.HostModule, _ []uint64, results []uint64) { |
| 78 | + results[0] = wago.F64(float64(time.Now().UnixNano()) / 1e6) |
| 79 | + }), |
| 80 | + "env.console.log": wago.HostFunc(func(m wago.HostModule, params, _ []uint64) { |
| 81 | + fmt.Println(liftString(m, uint32(params[0]))) |
| 82 | + }), |
| 83 | + "env.writeFile": wago.HostFunc(func(m wago.HostModule, params, _ []uint64) { |
| 84 | + name := liftString(m, uint32(params[0])) |
| 85 | + data := liftString(m, uint32(params[1])) |
| 86 | + fmt.Printf("__AS_BENCH_JSON__%s\t%s\n", name, data) |
| 87 | + }), |
| 88 | + "env.abort": wago.HostFunc(func(m wago.HostModule, params, _ []uint64) { |
| 89 | + fmt.Fprintf( |
| 90 | + os.Stderr, |
| 91 | + "abort: %s in %s:%d:%d\n", |
| 92 | + liftString(m, uint32(params[0])), |
| 93 | + liftString(m, uint32(params[1])), |
| 94 | + uint32(params[2]), |
| 95 | + uint32(params[3]), |
| 96 | + ) |
| 97 | + panic(wago.HostExit{Code: 1}) |
| 98 | + }), |
| 99 | + } |
| 100 | + |
| 101 | + trace("instantiating module") |
| 102 | + instance, err := wago.Instantiate(compiled, wago.InstantiateOptions{Imports: imports}) |
| 103 | + if err != nil { |
| 104 | + fail("instantiate module: %v", err) |
| 105 | + } |
| 106 | + defer instance.Close() |
| 107 | + trace("module instantiated") |
| 108 | + |
| 109 | + // The module is built with --exportStart so initialization and the full |
| 110 | + // benchmark run through WAGO's normal exported-function invocation path. |
| 111 | + trace("running benchmark") |
| 112 | + if _, err := instance.Invoke("start"); err != nil { |
| 113 | + fail("run benchmark: %v", err) |
| 114 | + } |
| 115 | + trace("benchmark finished") |
| 116 | +} |
0 commit comments