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

     1  package bench
     2  
     3  import (
     4  	"testing"
     5  
     6  	"wa-lang.org/wazero/internal/wasm"
     7  )
     8  
     9  func BenchmarkMemory(b *testing.B) {
    10  	mem := &wasm.MemoryInstance{Buffer: make([]byte, wasm.MemoryPageSize), Min: 1}
    11  	if !mem.WriteByte(testCtx, 10, 16) {
    12  		b.Fail()
    13  	}
    14  
    15  	b.Run("ReadByte", func(b *testing.B) {
    16  		for i := 0; i < b.N; i++ {
    17  			if v, ok := mem.ReadByte(testCtx, 10); !ok || v != 16 {
    18  				b.Fail()
    19  			}
    20  		}
    21  	})
    22  
    23  	b.Run("ReadUint32Le", func(b *testing.B) {
    24  		for i := 0; i < b.N; i++ {
    25  			if v, ok := mem.ReadUint32Le(testCtx, 10); !ok || v != 16 {
    26  				b.Fail()
    27  			}
    28  		}
    29  	})
    30  
    31  	b.Run("WriteByte", func(b *testing.B) {
    32  		for i := 0; i < b.N; i++ {
    33  			if !mem.WriteByte(testCtx, 10, 16) {
    34  				b.Fail()
    35  			}
    36  		}
    37  	})
    38  
    39  	b.Run("WriteUint32Le", func(b *testing.B) {
    40  		for i := 0; i < b.N; i++ {
    41  			if !mem.WriteUint32Le(testCtx, 10, 16) {
    42  				b.Fail()
    43  			}
    44  		}
    45  	})
    46  }