github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/integration_test/libsodium/bench_test.go (about)

     1  package libsodium
     2  
     3  import (
     4  	"context"
     5  	"crypto/rand"
     6  	"embed"
     7  	"io"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/tetratelabs/wazero"
    12  	"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
    13  	"github.com/tetratelabs/wazero/internal/platform"
    14  	"github.com/tetratelabs/wazero/internal/testing/require"
    15  )
    16  
    17  //go:embed testdata/*
    18  var tests embed.FS
    19  
    20  func BenchmarkLibsodium(b *testing.B) {
    21  	if !platform.CompilerSupported() {
    22  		b.Skip()
    23  	}
    24  
    25  	cases, err := tests.ReadDir("testdata")
    26  	require.NoError(b, err)
    27  	if len(cases) < 10 {
    28  		b.Skip("skipping libsodium bench because wasm files not found. `make libsodium` to download the binaries.")
    29  	}
    30  
    31  	ctx := context.Background()
    32  	r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigCompiler())
    33  	defer r.Close(ctx)
    34  	wasi_snapshot_preview1.MustInstantiate(ctx, r)
    35  
    36  	// Some tests are skipped because they are taking too long to run, but sure it is possible to run them.
    37  	for _, c := range []struct {
    38  		name string
    39  	}{
    40  		//{name: "box7"},
    41  		{name: "box_easy2"},
    42  		{name: "kdf_hkdf"},
    43  		{name: "auth5"},
    44  		{name: "stream2"},
    45  		{name: "aead_xchacha20poly1305"},
    46  		{name: "hash3"},
    47  		{name: "aead_chacha20poly1305"},
    48  		{name: "auth"},
    49  		//{name: "core_ed25519_h2c"},
    50  		{name: "onetimeauth"},
    51  		{name: "aead_aegis256"},
    52  		{name: "scalarmult_ristretto255"},
    53  		//{name: "core_ristretto255"},
    54  		{name: "stream3"},
    55  		//{name: "pwhash_scrypt"},
    56  		{name: "shorthash"},
    57  		{name: "scalarmult"},
    58  		{name: "chacha20"},
    59  		//{name: "pwhash_argon2id"},
    60  		{name: "onetimeauth7"},
    61  		{name: "scalarmult7"},
    62  		{name: "auth3"},
    63  		{name: "stream4"},
    64  		{name: "hash"},
    65  		//{name: "sign"},
    66  		{name: "auth2"},
    67  		{name: "scalarmult6"},
    68  		{name: "ed25519_convert"},
    69  		{name: "box_seal"},
    70  		{name: "secretbox7"},
    71  		{name: "pwhash_argon2i"},
    72  		{name: "secretstream_xchacha20poly1305"},
    73  		{name: "codecs"},
    74  		{name: "scalarmult_ed25519"},
    75  		{name: "sodium_utils"},
    76  		{name: "scalarmult5"},
    77  		{name: "xchacha20"},
    78  		{name: "secretbox8"},
    79  		{name: "box2"},
    80  		{name: "core3"},
    81  		{name: "siphashx24"},
    82  		{name: "generichash"},
    83  		{name: "aead_chacha20poly13052"},
    84  		{name: "randombytes"},
    85  		{name: "scalarmult8"},
    86  		//{name: "pwhash_scrypt_ll"},
    87  		{name: "kx"},
    88  		{name: "stream"},
    89  		{name: "auth7"},
    90  		{name: "generichash2"},
    91  		{name: "box_seed"},
    92  		{name: "keygen"},
    93  		{name: "metamorphic"},
    94  		{name: "secretbox_easy2"},
    95  		{name: "sign2"},
    96  		//{name: "core_ed25519"},
    97  		{name: "box_easy"},
    98  		{name: "secretbox2"},
    99  		//{name: "box8"},
   100  		{name: "box"},
   101  		{name: "kdf"},
   102  		{name: "secretbox_easy"},
   103  		{name: "onetimeauth2"},
   104  		{name: "generichash3"},
   105  		{name: "scalarmult2"},
   106  		{name: "aead_aegis128l"},
   107  		{name: "auth6"},
   108  		{name: "secretbox"},
   109  		{name: "verify1"},
   110  	} {
   111  		b.Run(c.name, func(b *testing.B) {
   112  			path := "testdata/" + c.name + ".wasm"
   113  			wasm, err := tests.ReadFile(path)
   114  			require.NoError(b, err)
   115  
   116  			cfg := wazero.NewModuleConfig().
   117  				WithStdout(io.Discard).
   118  				WithStderr(io.Discard).
   119  				WithStdin(os.Stdin).
   120  				WithRandSource(rand.Reader).
   121  				WithFSConfig(wazero.NewFSConfig()).
   122  				WithSysNanosleep().
   123  				WithSysNanotime().
   124  				WithSysWalltime().
   125  				WithArgs(c.name + ".wasm")
   126  
   127  			compiled, err := r.CompileModule(ctx, wasm)
   128  			require.NoError(b, err)
   129  
   130  			b.ResetTimer()
   131  			for i := 0; i < b.N; i++ {
   132  				mod, err := r.InstantiateModule(ctx, compiled, cfg.WithName(""))
   133  				require.NoError(b, err)
   134  				err = mod.Close(ctx)
   135  				require.NoError(b, err)
   136  			}
   137  		})
   138  	}
   139  }