wa-lang.org/wazero@v1.0.2/imports/wasi_snapshot_preview1/wasi_bench_test.go (about)

     1  package wasi_snapshot_preview1
     2  
     3  import (
     4  	"testing"
     5  
     6  	"wa-lang.org/wazero"
     7  	"wa-lang.org/wazero/api"
     8  	"wa-lang.org/wazero/internal/testing/proxy"
     9  	"wa-lang.org/wazero/internal/testing/require"
    10  )
    11  
    12  var testMem = []byte{
    13  	0,                // environBuf is after this
    14  	'a', '=', 'b', 0, // null terminated "a=b",
    15  	'b', '=', 'c', 'd', 0, // null terminated "b=cd"
    16  	0,          // environ is after this
    17  	1, 0, 0, 0, // little endian-encoded offset of "a=b"
    18  	5, 0, 0, 0, // little endian-encoded offset of "b=cd"
    19  	0,
    20  }
    21  
    22  func Test_Benchmark_EnvironGet(t *testing.T) {
    23  	mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().
    24  		WithEnv("a", "b").WithEnv("b", "cd"))
    25  	defer r.Close(testCtx)
    26  
    27  	// Invoke environGet and check the memory side effects.
    28  	requireErrno(t, ErrnoSuccess, mod, functionEnvironGet, uint64(11), uint64(1))
    29  	require.Equal(t, `
    30  --> proxy.environ_get(environ=11,environ_buf=1)
    31  	==> wasi_snapshot_preview1.environ_get(environ=11,environ_buf=1)
    32  	<== ESUCCESS
    33  <-- (0)
    34  `, "\n"+log.String())
    35  
    36  	mem, ok := mod.Memory().Read(testCtx, 0, uint32(len(testMem)))
    37  	require.True(t, ok)
    38  	require.Equal(t, testMem, mem)
    39  }
    40  
    41  func Benchmark_EnvironGet(b *testing.B) {
    42  	r := wazero.NewRuntime(testCtx)
    43  	defer r.Close(testCtx)
    44  
    45  	mod, err := instantiateProxyModule(r, wazero.NewModuleConfig().
    46  		WithEnv("a", "b").WithEnv("b", "cd"))
    47  	if err != nil {
    48  		b.Fatal(err)
    49  	}
    50  
    51  	b.Run("environGet", func(b *testing.B) {
    52  		for i := 0; i < b.N; i++ {
    53  			results, err := mod.ExportedFunction(functionEnvironGet).Call(testCtx, uint64(0), uint64(4))
    54  			if err != nil {
    55  				b.Fatal(err)
    56  			}
    57  			errno := Errno(results[0])
    58  			if errno != ErrnoSuccess {
    59  				b.Fatal(ErrnoName(errno))
    60  			}
    61  		}
    62  	})
    63  }
    64  
    65  // instantiateProxyModule instantiates a guest that re-exports WASI functions.
    66  func instantiateProxyModule(r wazero.Runtime, config wazero.ModuleConfig) (api.Module, error) {
    67  	wasiModuleCompiled, err := (&builder{r}).hostModuleBuilder().Compile(testCtx)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	if _, err = r.InstantiateModule(testCtx, wasiModuleCompiled, wazero.NewModuleConfig()); err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	proxyBin := proxy.GetProxyModuleBinary(ModuleName, wasiModuleCompiled)
    77  
    78  	proxyCompiled, err := r.CompileModule(testCtx, proxyBin)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	return r.InstantiateModule(testCtx, proxyCompiled, config)
    84  }