wa-lang.org/wazero@v1.0.2/internal/integration_test/vs/bench_memory.go (about)

     1  package vs
     2  
     3  import (
     4  	_ "embed"
     5  	"encoding/binary"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"wa-lang.org/wazero/internal/testing/require"
    10  )
    11  
    12  const (
    13  	i32                  = "i32"
    14  	i32ValueMemoryOffset = 32
    15  	i64                  = "i64"
    16  	i64ValueMemoryOffset = 64
    17  	inWasmIteration      = 100
    18  )
    19  
    20  var (
    21  	//go:embed testdata/memory.wasm
    22  	memoryWasm      []byte
    23  	memoryConfig    *RuntimeConfig
    24  	memoryFunctions = []string{i32, i64}
    25  )
    26  
    27  func init() {
    28  	memoryConfig = &RuntimeConfig{
    29  		ModuleName:        "memory",
    30  		ModuleWasm:        memoryWasm,
    31  		FuncNames:         memoryFunctions,
    32  		NeedsMemoryExport: true,
    33  	}
    34  }
    35  
    36  func RunTestMemory(t *testing.T, runtime func() Runtime) {
    37  	t.Run(i32, func(t *testing.T) {
    38  		testCall(t, runtime, memoryConfig, func(t *testing.T, m Module, instantiation int, iteration int) {
    39  			buf := m.Memory()
    40  			binary.LittleEndian.PutUint32(buf[i32ValueMemoryOffset:], inWasmIteration)
    41  			err := m.CallV_V(testCtx, i32)
    42  			require.NoError(t, err)
    43  			if 0 != binary.LittleEndian.Uint32(buf[i32ValueMemoryOffset:]) {
    44  				panic(fmt.Sprintf("BUG at iteration %d: %d", iteration, binary.LittleEndian.Uint32(buf[i32ValueMemoryOffset:])))
    45  			}
    46  		})
    47  	})
    48  
    49  	t.Run(i64, func(t *testing.T) {
    50  		testCall(t, runtime, memoryConfig, func(t *testing.T, m Module, instantiation int, iteration int) {
    51  			buf := m.Memory()
    52  			binary.LittleEndian.PutUint64(buf[i64ValueMemoryOffset:], inWasmIteration)
    53  			err := m.CallV_V(testCtx, i64)
    54  			require.NoError(t, err)
    55  			if 0 != binary.LittleEndian.Uint64(buf[i64ValueMemoryOffset:]) {
    56  				panic(fmt.Sprintf("BUG at iteration %d: %d", iteration, binary.LittleEndian.Uint64(buf[i32ValueMemoryOffset:])))
    57  			}
    58  		})
    59  	})
    60  }
    61  
    62  func RunTestBenchmarkMemory_CompilerFastest(t *testing.T, vsRuntime Runtime) {
    63  	t.Run(i32, func(t *testing.T) {
    64  		runTestBenchmark_Call_CompilerFastest(t, memoryConfig, "/memory.i32", memoryI32, vsRuntime)
    65  	})
    66  	t.Run(i64, func(t *testing.T) {
    67  		runTestBenchmark_Call_CompilerFastest(t, memoryConfig, "/memory.i64", memoryI64, vsRuntime)
    68  	})
    69  }
    70  
    71  func RunBenchmarkMemory(b *testing.B, runtime func() Runtime) {
    72  	b.Run(i32, func(b *testing.B) {
    73  		benchmark(b, runtime, memoryConfig, memoryI32)
    74  	})
    75  	b.Run(i64, func(b *testing.B) {
    76  		benchmark(b, runtime, memoryConfig, memoryI64)
    77  	})
    78  }
    79  
    80  func memoryI32(m Module, iteration int) (err error) {
    81  	buf := m.Memory()
    82  	binary.LittleEndian.PutUint32(buf[i32ValueMemoryOffset:], inWasmIteration)
    83  	err = m.CallV_V(testCtx, i32)
    84  	if 0 != binary.LittleEndian.Uint32(buf[i32ValueMemoryOffset:]) {
    85  		panic(fmt.Sprintf("BUG at iteration %d", iteration))
    86  	}
    87  	return
    88  }
    89  
    90  func memoryI64(m Module, iteration int) (err error) {
    91  	buf := m.Memory()
    92  	binary.LittleEndian.PutUint64(buf[i64ValueMemoryOffset:], inWasmIteration)
    93  	err = m.CallV_V(testCtx, i64)
    94  	if 0 != binary.LittleEndian.Uint64(buf[i64ValueMemoryOffset:]) {
    95  		panic(fmt.Sprintf("BUG at iteration %d", iteration))
    96  	}
    97  	return
    98  }