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.

92 lines
3.1 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. _, err := priv.Sign(message)
  34. if err != nil {
  35. b.FailNow()
  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 crypto.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.VerifyBytes(message, signature)
  52. }
  53. }
  54. // Below is the aforementioned license.
  55. // Copyright (c) 2012 The Go Authors. All rights reserved.
  56. // Redistribution and use in source and binary forms, with or without
  57. // modification, are permitted provided that the following conditions are
  58. // met:
  59. // * Redistributions of source code must retain the above copyright
  60. // notice, this list of conditions and the following disclaimer.
  61. // * Redistributions in binary form must reproduce the above
  62. // copyright notice, this list of conditions and the following disclaimer
  63. // in the documentation and/or other materials provided with the
  64. // distribution.
  65. // * Neither the name of Google Inc. nor the names of its
  66. // contributors may be used to endorse or promote products derived from
  67. // this software without specific prior written permission.
  68. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  69. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  70. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  71. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  72. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  73. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  74. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  75. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  76. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  77. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  78. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.