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

     1  package bench
     2  
     3  import (
     4  	"testing"
     5  
     6  	"wa-lang.org/wazero"
     7  	"wa-lang.org/wazero/api"
     8  	"wa-lang.org/wazero/imports/wasi_snapshot_preview1"
     9  	"wa-lang.org/wazero/internal/testing/require"
    10  	"wa-lang.org/wazero/internal/wasm"
    11  	"wa-lang.org/wazero/internal/wasm/binary"
    12  )
    13  
    14  // example holds the latest supported features as described in the comments of exampleText
    15  var example = newExample()
    16  
    17  // exampleWasm is the exampleText encoded in the WebAssembly 1.0 binary format.
    18  var exampleWasm = binary.EncodeModule(example)
    19  
    20  func newExample() *wasm.Module {
    21  	three := wasm.Index(3)
    22  	f32, i32, i64 := wasm.ValueTypeF32, wasm.ValueTypeI32, wasm.ValueTypeI64
    23  	return &wasm.Module{
    24  		TypeSection: []*wasm.FunctionType{
    25  			{Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32}},
    26  			{},
    27  			{Params: []wasm.ValueType{i32, i32, i32, i32}, Results: []wasm.ValueType{i32}},
    28  			{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i64}},
    29  			{Params: []wasm.ValueType{f32}, Results: []wasm.ValueType{i32}},
    30  			{Params: []wasm.ValueType{i32, i32}, Results: []wasm.ValueType{i32, i32}},
    31  		},
    32  		ImportSection: []*wasm.Import{
    33  			{
    34  				Module: "wasi_snapshot_preview1", Name: "args_sizes_get",
    35  				Type:     wasm.ExternTypeFunc,
    36  				DescFunc: 0,
    37  			}, {
    38  				Module: "wasi_snapshot_preview1", Name: "fd_write",
    39  				Type:     wasm.ExternTypeFunc,
    40  				DescFunc: 2,
    41  			},
    42  		},
    43  		FunctionSection: []wasm.Index{wasm.Index(1), wasm.Index(1), wasm.Index(0), wasm.Index(3), wasm.Index(4), wasm.Index(5)},
    44  		CodeSection: []*wasm.Code{
    45  			{Body: []byte{wasm.OpcodeCall, 3, wasm.OpcodeEnd}},
    46  			{Body: []byte{wasm.OpcodeEnd}},
    47  			{Body: []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 1, wasm.OpcodeI32Add, wasm.OpcodeEnd}},
    48  			{Body: []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeI64Extend16S, wasm.OpcodeEnd}},
    49  			{Body: []byte{
    50  				wasm.OpcodeLocalGet, 0x00,
    51  				wasm.OpcodeMiscPrefix, wasm.OpcodeMiscI32TruncSatF32S,
    52  				wasm.OpcodeEnd,
    53  			}},
    54  			{Body: []byte{wasm.OpcodeLocalGet, 1, wasm.OpcodeLocalGet, 0, wasm.OpcodeEnd}},
    55  		},
    56  		MemorySection: &wasm.Memory{Min: 1, Cap: 1, Max: three, IsMaxEncoded: true},
    57  		ExportSection: []*wasm.Export{
    58  			{Name: "AddInt", Type: wasm.ExternTypeFunc, Index: wasm.Index(4)},
    59  			{Name: "", Type: wasm.ExternTypeFunc, Index: wasm.Index(3)},
    60  			{Name: "mem", Type: wasm.ExternTypeMemory, Index: wasm.Index(0)},
    61  			{Name: "swap", Type: wasm.ExternTypeFunc, Index: wasm.Index(7)},
    62  		},
    63  		StartSection: &three,
    64  		NameSection: &wasm.NameSection{
    65  			ModuleName: "example",
    66  			FunctionNames: wasm.NameMap{
    67  				{Index: wasm.Index(0), Name: "wasi.args_sizes_get"},
    68  				{Index: wasm.Index(1), Name: "wasi.fd_write"},
    69  				{Index: wasm.Index(2), Name: "call_hello"},
    70  				{Index: wasm.Index(3), Name: "hello"},
    71  				{Index: wasm.Index(4), Name: "addInt"},
    72  				{Index: wasm.Index(7), Name: "swap"},
    73  			},
    74  			LocalNames: wasm.IndirectNameMap{
    75  				{Index: wasm.Index(1), NameMap: wasm.NameMap{
    76  					{Index: wasm.Index(0), Name: "fd"},
    77  					{Index: wasm.Index(1), Name: "iovs_ptr"},
    78  					{Index: wasm.Index(2), Name: "iovs_len"},
    79  					{Index: wasm.Index(3), Name: "nwritten_ptr"},
    80  				}},
    81  				{Index: wasm.Index(4), NameMap: wasm.NameMap{
    82  					{Index: wasm.Index(0), Name: "value_1"},
    83  					{Index: wasm.Index(1), Name: "value_2"},
    84  				}},
    85  			},
    86  		},
    87  	}
    88  }
    89  
    90  func TestExampleUpToDate(t *testing.T) {
    91  	t.Run("binary.DecodeModule", func(t *testing.T) {
    92  		m, err := binary.DecodeModule(exampleWasm, api.CoreFeaturesV2, wasm.MemoryLimitPages, false)
    93  		require.NoError(t, err)
    94  		require.Equal(t, example, m)
    95  	})
    96  
    97  	t.Run("Executable", func(t *testing.T) {
    98  		r := wazero.NewRuntimeWithConfig(testCtx, wazero.NewRuntimeConfig())
    99  		defer r.Close(testCtx)
   100  
   101  		// Add WASI to satisfy import tests
   102  		wasi_snapshot_preview1.MustInstantiate(testCtx, r)
   103  
   104  		// Decode and instantiate the module
   105  		module, err := r.InstantiateModuleFromBinary(testCtx, exampleWasm)
   106  		require.NoError(t, err)
   107  
   108  		// Call the swap function as a smoke test
   109  		results, err := module.ExportedFunction("swap").Call(testCtx, 1, 2)
   110  		require.NoError(t, err)
   111  		require.Equal(t, []uint64{2, 1}, results)
   112  	})
   113  }
   114  
   115  func BenchmarkCodec(b *testing.B) {
   116  	b.Run("binary.DecodeModule", func(b *testing.B) {
   117  		b.ReportAllocs()
   118  		for i := 0; i < b.N; i++ {
   119  			if _, err := binary.DecodeModule(exampleWasm, api.CoreFeaturesV2, wasm.MemoryLimitPages, false); err != nil {
   120  				b.Fatal(err)
   121  			}
   122  		}
   123  	})
   124  	b.Run("binary.EncodeModule", func(b *testing.B) {
   125  		b.ReportAllocs()
   126  		for i := 0; i < b.N; i++ {
   127  			_ = binary.EncodeModule(example)
   128  		}
   129  	})
   130  }