-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test memory usage of the wazero engine
Signed-off-by: Achille Roussel <[email protected]>
- Loading branch information
1 parent
3946e94
commit 129e49a
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package adhoc | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/tetratelabs/wazero" | ||
) | ||
|
||
func TestMemoryLeak(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping memory leak test in short mode.") | ||
} | ||
|
||
duration := 5 * time.Second | ||
t.Logf("running memory leak test for %s", duration) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), duration) | ||
defer cancel() | ||
|
||
for ctx.Err() == nil { | ||
if err := testMemoryLeakInstantiateRuntimeAndModule(); err != nil { | ||
log.Panicln(err) | ||
} | ||
} | ||
|
||
var stats runtime.MemStats | ||
runtime.GC() | ||
runtime.ReadMemStats(&stats) | ||
|
||
if stats.Alloc > (100 * 1024 * 1024) { | ||
t.Errorf("wazero used more than 100 MiB after running the test for %s (alloc=%d)", duration, stats.Alloc) | ||
} | ||
} | ||
|
||
func testMemoryLeakInstantiateRuntimeAndModule() error { | ||
ctx := context.Background() | ||
|
||
runtime := wazero.NewRuntime(ctx) | ||
defer runtime.Close(ctx) | ||
|
||
mod, err := runtime.InstantiateWithConfig(ctx, memoryWasm, | ||
wazero.NewModuleConfig().WithStartFunctions()) | ||
if err != nil { | ||
return err | ||
} | ||
return mod.Close(ctx) | ||
} |