You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.0 KiB

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