bitbucket.org/number571/tendermint@v0.8.14/crypto/internal/benchmarking/bench.go (about)

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