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.

110 lines
2.6 KiB

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