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.

106 lines
2.4 KiB

  1. package light_test
  2. import (
  3. "testing"
  4. "time"
  5. dbm "github.com/tendermint/tm-db"
  6. "github.com/tendermint/tendermint/libs/log"
  7. "github.com/tendermint/tendermint/light"
  8. "github.com/tendermint/tendermint/light/provider"
  9. mockp "github.com/tendermint/tendermint/light/provider/mock"
  10. dbs "github.com/tendermint/tendermint/light/store/db"
  11. )
  12. // NOTE: block is produced every minute. Make sure the verification time
  13. // provided in the function call is correct for the size of the blockchain. The
  14. // benchmarking may take some time hence it can be more useful to set the time
  15. // or the amount of iterations use the flag -benchtime t -> i.e. -benchtime 5m
  16. // or -benchtime 100x.
  17. //
  18. // Remember that none of these benchmarks account for network latency.
  19. var (
  20. benchmarkFullNode = mockp.New(GenMockNode(chainID, 1000, 100, 1, bTime))
  21. genesisHeader, _ = benchmarkFullNode.SignedHeader(1)
  22. )
  23. func BenchmarkSequence(b *testing.B) {
  24. c, err := light.NewClient(
  25. chainID,
  26. light.TrustOptions{
  27. Period: 24 * time.Hour,
  28. Height: 1,
  29. Hash: genesisHeader.Hash(),
  30. },
  31. benchmarkFullNode,
  32. []provider.Provider{benchmarkFullNode},
  33. dbs.New(dbm.NewMemDB(), chainID),
  34. light.Logger(log.TestingLogger()),
  35. light.SequentialVerification(),
  36. )
  37. if err != nil {
  38. b.Fatal(err)
  39. }
  40. b.ResetTimer()
  41. for n := 0; n < b.N; n++ {
  42. _, err = c.VerifyHeaderAtHeight(1000, bTime.Add(1000*time.Minute))
  43. if err != nil {
  44. b.Fatal(err)
  45. }
  46. }
  47. }
  48. func BenchmarkBisection(b *testing.B) {
  49. c, err := light.NewClient(
  50. chainID,
  51. light.TrustOptions{
  52. Period: 24 * time.Hour,
  53. Height: 1,
  54. Hash: genesisHeader.Hash(),
  55. },
  56. benchmarkFullNode,
  57. []provider.Provider{benchmarkFullNode},
  58. dbs.New(dbm.NewMemDB(), chainID),
  59. light.Logger(log.TestingLogger()),
  60. )
  61. if err != nil {
  62. b.Fatal(err)
  63. }
  64. b.ResetTimer()
  65. for n := 0; n < b.N; n++ {
  66. _, err = c.VerifyHeaderAtHeight(1000, bTime.Add(1000*time.Minute))
  67. if err != nil {
  68. b.Fatal(err)
  69. }
  70. }
  71. }
  72. func BenchmarkBackwards(b *testing.B) {
  73. trustedHeader, _ := benchmarkFullNode.SignedHeader(0)
  74. c, err := light.NewClient(
  75. chainID,
  76. light.TrustOptions{
  77. Period: 24 * time.Hour,
  78. Height: trustedHeader.Height,
  79. Hash: trustedHeader.Hash(),
  80. },
  81. benchmarkFullNode,
  82. []provider.Provider{benchmarkFullNode},
  83. dbs.New(dbm.NewMemDB(), chainID),
  84. light.Logger(log.TestingLogger()),
  85. )
  86. if err != nil {
  87. b.Fatal(err)
  88. }
  89. b.ResetTimer()
  90. for n := 0; n < b.N; n++ {
  91. _, err = c.VerifyHeaderAtHeight(1, bTime)
  92. if err != nil {
  93. b.Fatal(err)
  94. }
  95. }
  96. }