github.com/aakash4dev/cometbft@v0.38.2/crypto/sr25519/bench_test.go (about)

     1  package sr25519
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/aakash4dev/cometbft/crypto"
    11  	"github.com/aakash4dev/cometbft/crypto/internal/benchmarking"
    12  )
    13  
    14  func BenchmarkKeyGeneration(b *testing.B) {
    15  	benchmarkKeygenWrapper := func(reader io.Reader) crypto.PrivKey {
    16  		return genPrivKey(reader)
    17  	}
    18  	benchmarking.BenchmarkKeyGeneration(b, benchmarkKeygenWrapper)
    19  }
    20  
    21  func BenchmarkSigning(b *testing.B) {
    22  	priv := GenPrivKey()
    23  	benchmarking.BenchmarkSigning(b, priv)
    24  }
    25  
    26  func BenchmarkVerification(b *testing.B) {
    27  	priv := GenPrivKey()
    28  	benchmarking.BenchmarkVerification(b, priv)
    29  }
    30  
    31  func BenchmarkVerifyBatch(b *testing.B) {
    32  	msg := []byte("BatchVerifyTest")
    33  
    34  	for _, sigsCount := range []int{1, 8, 64, 1024} {
    35  		sigsCount := sigsCount
    36  		b.Run(fmt.Sprintf("sig-count-%d", sigsCount), func(b *testing.B) {
    37  			// Pre-generate all of the keys, and signatures, but do not
    38  			// benchmark key-generation and signing.
    39  			pubs := make([]crypto.PubKey, 0, sigsCount)
    40  			sigs := make([][]byte, 0, sigsCount)
    41  			for i := 0; i < sigsCount; i++ {
    42  				priv := GenPrivKey()
    43  				sig, _ := priv.Sign(msg)
    44  				pubs = append(pubs, priv.PubKey().(PubKey))
    45  				sigs = append(sigs, sig)
    46  			}
    47  			b.ResetTimer()
    48  
    49  			b.ReportAllocs()
    50  			// NOTE: dividing by n so that metrics are per-signature
    51  			for i := 0; i < b.N/sigsCount; i++ {
    52  				// The benchmark could just benchmark the Verify()
    53  				// routine, but there is non-trivial overhead associated
    54  				// with BatchVerifier.Add(), which should be included
    55  				// in the benchmark.
    56  				v := NewBatchVerifier()
    57  				for i := 0; i < sigsCount; i++ {
    58  					err := v.Add(pubs[i], msg, sigs[i])
    59  					require.NoError(b, err)
    60  				}
    61  
    62  				if ok, _ := v.Verify(); !ok {
    63  					b.Fatal("signature set failed batch verification")
    64  				}
    65  			}
    66  		})
    67  	}
    68  }