-
Notifications
You must be signed in to change notification settings - Fork 243
/
wasi_bench_test.go
68 lines (58 loc) · 1.82 KB
/
wasi_bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package wasi_snapshot_preview1
import (
"testing"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/internal/testing/require"
)
var testMem = []byte{
0, // environBuf is after this
'a', '=', 'b', 0, // null terminated "a=b",
'b', '=', 'c', 'd', 0, // null terminated "b=cd"
0, // environ is after this
1, 0, 0, 0, // little endian-encoded offset of "a=b"
5, 0, 0, 0, // little endian-encoded offset of "b=cd"
0,
}
func Test_Benchmark_EnvironGet(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().
WithEnv("a", "b").WithEnv("b", "cd"))
defer r.Close(testCtx)
// Invoke environGet and check the memory side effects.
requireErrno(t, ErrnoSuccess, mod, functionEnvironGet, uint64(11), uint64(1))
require.Equal(t, `
--> proxy.environ_get(environ=11,environ_buf=1)
==> wasi_snapshot_preview1.environ_get(environ=11,environ_buf=1)
<== ESUCCESS
<-- (0)
`, "\n"+log.String())
mem, ok := mod.Memory().Read(testCtx, 0, uint32(len(testMem)))
require.True(t, ok)
require.Equal(t, testMem, mem)
}
func Benchmark_EnvironGet(b *testing.B) {
r := wazero.NewRuntimeWithConfig(testCtx, wazero.NewRuntimeConfigInterpreter())
compiled, err := r.NewModuleBuilder(b.Name()).
ExportMemoryWithMax("memory", 1, 1).
Compile(testCtx, wazero.NewCompileConfig())
if err != nil {
b.Fatal(err)
}
mod, err := r.InstantiateModule(testCtx, compiled, wazero.NewModuleConfig().
WithEnv("a", "bc").WithEnv("b", "cd"))
if err != nil {
b.Fatal(err)
}
defer r.Close(testCtx)
b.Run("environGet", func(b *testing.B) {
for i := 0; i < b.N; i++ {
results, err := mod.ExportedFunction(functionEnvironGet).Call(testCtx, uint64(0), uint64(4))
if err != nil {
b.Fatal(err)
}
errno := Errno(results[0])
if errno != ErrnoSuccess {
b.Fatal(ErrnoName(errno))
}
}
})
}