Skip to content

Commit 6fdb893

Browse files
authored
Fixes std lib cases actually benchmarkable (#1885)
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent 8c71d4d commit 6fdb893

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

.github/workflows/integration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ jobs:
391391
path:
392392
${{ env.STDLIB_TESTS }}/testdata/go
393393
# Use precise Go version from setup-go as patch version differences can effect tests.
394-
key: go-wasip1-binaries-${{ matrix.os }}-${{ steps.setup-go.outputs.go-version }}
394+
key: go-wasip1-binaries-${{ matrix.os }}-${{ steps.setup-go.outputs.go-version }}-${{ matrix.arch }}
395395

396396
- if: ${{ matrix.compiler == 'optimizing' }}
397397
name: Build wazero

internal/integration_test/stdlibs/bench_test.go

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,38 @@ func BenchmarkZig(b *testing.B) {
2020
if runtime.GOARCH == "arm64" {
2121
b.Run("optimizing", func(b *testing.B) {
2222
c := opt.NewRuntimeConfigOptimizingCompiler()
23-
runtBench(b, context.Background(), c, zigTestCase)
23+
runtBenches(b, context.Background(), c, zigTestCase)
2424
})
2525
}
2626
b.Run("baseline", func(b *testing.B) {
2727
c := wazero.NewRuntimeConfigCompiler()
28-
runtBench(b, context.Background(), c, zigTestCase)
28+
runtBenches(b, context.Background(), c, zigTestCase)
2929
})
3030
}
3131

3232
func BenchmarkTinyGo(b *testing.B) {
3333
if runtime.GOARCH == "arm64" {
3434
b.Run("optimizing", func(b *testing.B) {
3535
c := opt.NewRuntimeConfigOptimizingCompiler()
36-
runtBench(b, context.Background(), c, tinyGoTestCase)
36+
runtBenches(b, context.Background(), c, tinyGoTestCase)
3737
})
3838
}
3939
b.Run("baseline", func(b *testing.B) {
4040
c := wazero.NewRuntimeConfigCompiler()
41-
runtBench(b, context.Background(), c, tinyGoTestCase)
41+
runtBenches(b, context.Background(), c, tinyGoTestCase)
4242
})
4343
}
4444

4545
func BenchmarkWasip1(b *testing.B) {
4646
if runtime.GOARCH == "arm64" {
4747
b.Run("optimizing", func(b *testing.B) {
4848
c := opt.NewRuntimeConfigOptimizingCompiler()
49-
runtBench(b, context.Background(), c, wasip1TestCase)
49+
runtBenches(b, context.Background(), c, wasip1TestCase)
5050
})
5151
}
5252
b.Run("baseline", func(b *testing.B) {
5353
c := wazero.NewRuntimeConfigCompiler()
54-
runtBench(b, context.Background(), c, wasip1TestCase)
54+
runtBenches(b, context.Background(), c, wasip1TestCase)
5555
})
5656
}
5757

@@ -136,7 +136,7 @@ var (
136136
}
137137
)
138138

139-
func runtBench(b *testing.B, ctx context.Context, rc wazero.RuntimeConfig, tc testCase) {
139+
func runtBenches(b *testing.B, ctx context.Context, rc wazero.RuntimeConfig, tc testCase) {
140140
cwd, _ := os.Getwd()
141141
files, err := os.ReadDir(tc.dir)
142142
require.NoError(b, err)
@@ -152,27 +152,36 @@ func runtBench(b *testing.B, ctx context.Context, rc wazero.RuntimeConfig, tc te
152152
if bin == nil {
153153
continue
154154
}
155-
b.Run(fname, func(b *testing.B) {
156-
r := wazero.NewRuntimeWithConfig(ctx, rc)
157-
wasi_snapshot_preview1.MustInstantiate(ctx, r)
158-
b.Cleanup(func() { r.Close(ctx) })
159-
160-
var cm wazero.CompiledModule
161-
b.Run("Compile", func(b *testing.B) {
162-
b.ResetTimer()
163-
var err error
164-
cm, err = r.CompileModule(ctx, bin)
165-
require.NoError(b, err)
166-
})
167155

168-
im, err := r.InstantiateModule(ctx, cm, modCfg)
169-
require.NoError(b, err)
170-
b.Run("Run", func(b *testing.B) {
171-
b.ResetTimer()
172-
_, err := im.ExportedFunction("_start").Call(ctx)
173-
requireZeroExitCode(b, err)
174-
})
175-
})
156+
for _, compile := range []bool{false, true} {
157+
if compile {
158+
b.Run("Compile/"+fname, func(b *testing.B) {
159+
b.ResetTimer()
160+
for i := 0; i < b.N; i++ {
161+
r := wazero.NewRuntimeWithConfig(ctx, rc)
162+
_, err := r.CompileModule(ctx, bin)
163+
require.NoError(b, err)
164+
require.NoError(b, r.Close(ctx))
165+
}
166+
})
167+
} else {
168+
r := wazero.NewRuntimeWithConfig(ctx, rc)
169+
wasi_snapshot_preview1.MustInstantiate(ctx, r)
170+
b.Cleanup(func() { r.Close(ctx) })
171+
172+
cm, err := r.CompileModule(ctx, bin)
173+
require.NoError(b, err)
174+
b.Run("Run/"+fname, func(b *testing.B) {
175+
b.ResetTimer()
176+
for i := 0; i < b.N; i++ {
177+
// Instantiate in the loop as _start cannot be called multiple times.
178+
m, err := r.InstantiateModule(ctx, cm, modCfg)
179+
requireZeroExitCode(b, err)
180+
require.NoError(b, m.Close(ctx))
181+
}
182+
})
183+
}
184+
}
176185
}
177186
}
178187

@@ -194,8 +203,7 @@ func defaultModuleConfig() wazero.ModuleConfig {
194203
WithRandSource(rand.Reader).
195204
// Some tests require Stdout and Stderr to be present.
196205
WithStdout(os.Stdout).
197-
WithStderr(os.Stderr).
198-
WithStartFunctions()
206+
WithStderr(os.Stderr)
199207
}
200208

201209
func requireZeroExitCode(b *testing.B, err error) {

0 commit comments

Comments
 (0)