github.com/project-88388/tendermint-v0.34.14-terra.2@v1.0.0/crypto/internal/benchmarking/bench.go (about)

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