github.com/Finschia/finschia-sdk@v0.48.1/crypto/keys/internal/benchmarking/bench.go (about)

     1  package benchmarking
     2  
     3  import (
     4  	"crypto/rand"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/Finschia/finschia-sdk/crypto/types"
     9  )
    10  
    11  // The code in this file is adapted from agl/ed25519.
    12  // As such it is under the following license.
    13  // Copyright 2012 The Go Authors. All rights reserved.
    14  // Use of this source code is governed by a BSD-style
    15  // license that can be found at the bottom of this file.
    16  
    17  // BenchmarkKeyGeneration benchmarks the given key generation algorithm using
    18  // a dummy reader.
    19  func BenchmarkKeyGeneration(b *testing.B, generateKey func(reader io.Reader) types.PrivKey) {
    20  	b.ReportAllocs()
    21  	for i := 0; i < b.N; i++ {
    22  		generateKey(rand.Reader)
    23  	}
    24  }
    25  
    26  // BenchmarkSigning benchmarks the given signing algorithm using
    27  // the provided privkey.
    28  func BenchmarkSigning(b *testing.B, priv types.PrivKey) {
    29  	message := []byte("Hello, world!")
    30  	b.ResetTimer()
    31  	for i := 0; i < b.N; i++ {
    32  		_, err := priv.Sign(message)
    33  		if err != nil {
    34  			b.FailNow()
    35  		}
    36  	}
    37  }
    38  
    39  // BenchmarkVerification benchmarks the given verification algorithm using
    40  // the provided privkey on a constant message.
    41  func BenchmarkVerification(b *testing.B, priv types.PrivKey) {
    42  	pub := priv.PubKey()
    43  	// use a short message, so this time doesn't get dominated by hashing.
    44  	message := []byte("Hello, world!")
    45  	signature, err := priv.Sign(message)
    46  	if err != nil {
    47  		b.Fatal(err)
    48  	}
    49  	b.ResetTimer()
    50  	for i := 0; i < b.N; i++ {
    51  		pub.VerifySignature(message, signature)
    52  	}
    53  }
    54  
    55  // Below is the aforementioned license.
    56  
    57  // Copyright (c) 2012 The Go Authors. All rights reserved.
    58  
    59  // Redistribution and use in source and binary forms, with or without
    60  // modification, are permitted provided that the following conditions are
    61  // met:
    62  
    63  //    * Redistributions of source code must retain the above copyright
    64  // notice, this list of conditions and the following disclaimer.
    65  //    * Redistributions in binary form must reproduce the above
    66  // copyright notice, this list of conditions and the following disclaimer
    67  // in the documentation and/or other materials provided with the
    68  // distribution.
    69  //    * Neither the name of Google Inc. nor the names of its
    70  // contributors may be used to endorse or promote products derived from
    71  // this software without specific prior written permission.
    72  
    73  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    74  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    75  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    76  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    77  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    78  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    79  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    80  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    81  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    82  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    83  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.