-
Notifications
You must be signed in to change notification settings - Fork 236
/
bench_test.go
232 lines (203 loc) · 6.21 KB
/
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package bench
import (
"context"
_ "embed"
"fmt"
"math/rand"
"runtime"
"testing"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/platform"
)
// testCtx is an arbitrary, non-default context. Non-nil also prevents linter errors.
var testCtx = context.WithValue(context.Background(), struct{}{}, "arbitrary")
// caseWasm was compiled from TinyGo testdata/case.go
//
//go:embed testdata/case.wasm
var caseWasm []byte
func BenchmarkInvocation(b *testing.B) {
b.Run("interpreter", func(b *testing.B) {
m := instantiateHostFunctionModuleWithEngine(b, wazero.NewRuntimeConfigInterpreter())
defer m.Close(testCtx)
runAllInvocationBenches(b, m)
})
if runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64" {
b.Run("compiler", func(b *testing.B) {
m := instantiateHostFunctionModuleWithEngine(b, wazero.NewRuntimeConfigCompiler())
defer m.Close(testCtx)
runAllInvocationBenches(b, m)
})
}
}
func BenchmarkInitialization(b *testing.B) {
b.Run("interpreter", func(b *testing.B) {
r := createRuntime(b, wazero.NewRuntimeConfigInterpreter())
runInitializationBench(b, r)
})
if runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64" {
b.Run("compiler", func(b *testing.B) {
r := createRuntime(b, wazero.NewRuntimeConfigCompiler())
runInitializationBench(b, r)
})
}
}
func BenchmarkCompilation(b *testing.B) {
if !platform.CompilerSupported() {
b.Skip()
}
// Note: recreate runtime each time in the loop to ensure that
// recompilation happens if the extern cache is not used.
b.Run("with extern cache", func(b *testing.B) {
ctx, err := experimental.WithCompilationCacheDirName(context.Background(), b.TempDir())
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigCompiler())
runCompilation(b, r)
}
})
b.Run("without extern cache", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := wazero.NewRuntimeWithConfig(context.Background(), wazero.NewRuntimeConfigCompiler())
runCompilation(b, r)
}
})
}
func runCompilation(b *testing.B, r wazero.Runtime) wazero.CompiledModule {
compiled, err := r.CompileModule(testCtx, caseWasm, wazero.NewCompileConfig())
if err != nil {
b.Fatal(err)
}
return compiled
}
func runInitializationBench(b *testing.B, r wazero.Runtime) {
compiled := runCompilation(b, r)
defer compiled.Close(testCtx)
b.ResetTimer()
for i := 0; i < b.N; i++ {
mod, err := r.InstantiateModule(testCtx, compiled, wazero.NewModuleConfig())
if err != nil {
b.Fatal(err)
}
mod.Close(testCtx)
}
}
func runAllInvocationBenches(b *testing.B, m api.Module) {
runBase64Benches(b, m)
runFibBenches(b, m)
runStringManipulationBenches(b, m)
runReverseArrayBenches(b, m)
runRandomMatMul(b, m)
}
func runBase64Benches(b *testing.B, m api.Module) {
base64 := m.ExportedFunction("base64")
for _, numPerExec := range []int{5, 100, 10000} {
numPerExec := uint64(numPerExec)
b.ResetTimer()
b.Run(fmt.Sprintf("base64_%d_per_exec", numPerExec), func(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := base64.Call(testCtx, numPerExec); err != nil {
b.Fatal(err)
}
}
})
}
}
func runFibBenches(b *testing.B, m api.Module) {
fibonacci := m.ExportedFunction("fibonacci")
for _, num := range []int{5, 10, 20, 30} {
num := uint64(num)
b.ResetTimer()
b.Run(fmt.Sprintf("fib_for_%d", num), func(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := fibonacci.Call(testCtx, num); err != nil {
b.Fatal(err)
}
}
})
}
}
func runStringManipulationBenches(b *testing.B, m api.Module) {
stringManipulation := m.ExportedFunction("string_manipulation")
for _, initialSize := range []int{50, 100, 1000} {
initialSize := uint64(initialSize)
b.ResetTimer()
b.Run(fmt.Sprintf("string_manipulation_size_%d", initialSize), func(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := stringManipulation.Call(testCtx, initialSize); err != nil {
b.Fatal(err)
}
}
})
}
}
func runReverseArrayBenches(b *testing.B, m api.Module) {
reverseArray := m.ExportedFunction("reverse_array")
for _, arraySize := range []int{500, 1000, 10000} {
arraySize := uint64(arraySize)
b.ResetTimer()
b.Run(fmt.Sprintf("reverse_array_size_%d", arraySize), func(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := reverseArray.Call(testCtx, arraySize); err != nil {
b.Fatal(err)
}
}
})
}
}
func runRandomMatMul(b *testing.B, m api.Module) {
randomMatMul := m.ExportedFunction("random_mat_mul")
for _, matrixSize := range []int{5, 10, 20} {
matrixSize := uint64(matrixSize)
b.ResetTimer()
b.Run(fmt.Sprintf("random_mat_mul_size_%d", matrixSize), func(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := randomMatMul.Call(testCtx, matrixSize); err != nil {
b.Fatal(err)
}
}
})
}
}
func instantiateHostFunctionModuleWithEngine(b *testing.B, config wazero.RuntimeConfig) api.Module {
r := createRuntime(b, config)
// InstantiateModuleFromBinary runs the "_start" function which is what TinyGo compiles "main" to.
m, err := r.InstantiateModuleFromBinary(testCtx, caseWasm)
if err != nil {
b.Fatal(err)
}
return m
}
func createRuntime(b *testing.B, config wazero.RuntimeConfig) wazero.Runtime {
getRandomString := func(ctx context.Context, m api.Module, retBufPtr uint32, retBufSize uint32) {
results, err := m.ExportedFunction("allocate_buffer").Call(ctx, 10)
if err != nil {
b.Fatal(err)
}
offset := uint32(results[0])
m.Memory().WriteUint32Le(ctx, retBufPtr, offset)
m.Memory().WriteUint32Le(ctx, retBufSize, 10)
b := make([]byte, 10)
_, _ = rand.Read(b)
m.Memory().Write(ctx, offset, b)
}
r := wazero.NewRuntimeWithConfig(testCtx, config)
_, err := r.NewModuleBuilder("env").
ExportFunction("get_random_string", getRandomString).
Instantiate(testCtx, r)
if err != nil {
b.Fatal(err)
}
// Note: host_func.go doesn't directly use WASI, but TinyGo needs to be initialized as a WASI Command.
// Add WASI to satisfy import tests
_, err = wasi_snapshot_preview1.Instantiate(testCtx, r)
if err != nil {
b.Fatal(err)
}
return r
}