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.

237 lines
6.2 KiB

lite: memStoreProvider GetHeightBinarySearch method + fix ValKeys.signHeaders Updates #1021 * Implement a GetHeightBinarySearch method that looks for the height using the binary search algorithm guaranteeing worst case iteration time of O(log2(n)) whereas worst case iteration time of O(n) for the current linear search So if n we had 500 commits stored by height and sorted, to trigger the worst case scenario for each, pass in the most negative height you can find e.g. -1 Linear search: 500 iterations Binary search: 9 iterations with n=1000, qHeight = -1 Linear search: 1000 iterations Binary search: 10 iterations with n=1e6, qHeight = -1 Linear search: 1e6 iterations Binary search: 20 iterations Of course there are realistic expectations e.g. a max of commits that may be saved so linear search might be useful for very small size set because it has less preparing overhead and only ~2 types of comparisons, but nonetheless binary search shines as soon as we start to hit say 50 commits to search from as you can see below: ```shell $ go test -v -run=^$ -bench=MemStore goos: darwin goarch: amd64 pkg: github.com/tendermint/tendermint/lite BenchmarkMemStoreProviderGetByHeightLinearSearch5-4 300000 6491 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch50-4 200000 12064 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch100-4 50000 32987 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch500-4 5000 395521 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch1000-4 500 2940724 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch5-4 300000 6281 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch50-4 200000 10117 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch100-4 100000 18447 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch500-4 20000 89029 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch1000-4 5000 265719 ns/op 1600 B/op 15 allocs/op PASS ok github.com/tendermint/tendermint/lite 86.614s $ go test -v -run=^$ -bench=MemStore goos: darwin goarch: amd64 pkg: github.com/tendermint/tendermint/lite BenchmarkMemStoreProviderGetByHeightLinearSearch5-4 300000 6779 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch50-4 100000 12980 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch100-4 30000 43598 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch500-4 5000 377462 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch1000-4 500 3278122 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch5-4 300000 7084 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch50-4 200000 9852 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch100-4 100000 19020 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch500-4 20000 99463 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch1000-4 5000 259293 ns/op 1600 B/op 15 allocs/op PASS ok github.com/tendermint/tendermint/lite 86.204s ``` which gives ```shell $ benchstat old.txt new.txt name old time/op new time/op delta MemStoreProviderGetByHeight5-4 6.63µs ± 2% 6.68µs ± 6% ~ (p=1.000 n=2+2) MemStoreProviderGetByHeight50-4 12.5µs ± 4% 10.0µs ± 1% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight100-4 38.3µs ±14% 18.7µs ± 2% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight500-4 386µs ± 2% 94µs ± 6% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight1000-4 3.11ms ± 5% 0.26ms ± 1% ~ (p=0.333 n=2+2) ``` If need be we can make a hybrid algorithm that switches between the linear and binary search depending on the number of items. This is reminiscent of Python's TimSort algorithm.
7 years ago
lite: memStoreProvider GetHeightBinarySearch method + fix ValKeys.signHeaders Updates #1021 * Implement a GetHeightBinarySearch method that looks for the height using the binary search algorithm guaranteeing worst case iteration time of O(log2(n)) whereas worst case iteration time of O(n) for the current linear search So if n we had 500 commits stored by height and sorted, to trigger the worst case scenario for each, pass in the most negative height you can find e.g. -1 Linear search: 500 iterations Binary search: 9 iterations with n=1000, qHeight = -1 Linear search: 1000 iterations Binary search: 10 iterations with n=1e6, qHeight = -1 Linear search: 1e6 iterations Binary search: 20 iterations Of course there are realistic expectations e.g. a max of commits that may be saved so linear search might be useful for very small size set because it has less preparing overhead and only ~2 types of comparisons, but nonetheless binary search shines as soon as we start to hit say 50 commits to search from as you can see below: ```shell $ go test -v -run=^$ -bench=MemStore goos: darwin goarch: amd64 pkg: github.com/tendermint/tendermint/lite BenchmarkMemStoreProviderGetByHeightLinearSearch5-4 300000 6491 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch50-4 200000 12064 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch100-4 50000 32987 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch500-4 5000 395521 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch1000-4 500 2940724 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch5-4 300000 6281 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch50-4 200000 10117 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch100-4 100000 18447 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch500-4 20000 89029 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch1000-4 5000 265719 ns/op 1600 B/op 15 allocs/op PASS ok github.com/tendermint/tendermint/lite 86.614s $ go test -v -run=^$ -bench=MemStore goos: darwin goarch: amd64 pkg: github.com/tendermint/tendermint/lite BenchmarkMemStoreProviderGetByHeightLinearSearch5-4 300000 6779 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch50-4 100000 12980 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch100-4 30000 43598 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch500-4 5000 377462 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch1000-4 500 3278122 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch5-4 300000 7084 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch50-4 200000 9852 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch100-4 100000 19020 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch500-4 20000 99463 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch1000-4 5000 259293 ns/op 1600 B/op 15 allocs/op PASS ok github.com/tendermint/tendermint/lite 86.204s ``` which gives ```shell $ benchstat old.txt new.txt name old time/op new time/op delta MemStoreProviderGetByHeight5-4 6.63µs ± 2% 6.68µs ± 6% ~ (p=1.000 n=2+2) MemStoreProviderGetByHeight50-4 12.5µs ± 4% 10.0µs ± 1% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight100-4 38.3µs ±14% 18.7µs ± 2% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight500-4 386µs ± 2% 94µs ± 6% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight1000-4 3.11ms ± 5% 0.26ms ± 1% ~ (p=0.333 n=2+2) ``` If need be we can make a hybrid algorithm that switches between the linear and binary search depending on the number of items. This is reminiscent of Python's TimSort algorithm.
7 years ago
lite: memStoreProvider GetHeightBinarySearch method + fix ValKeys.signHeaders Updates #1021 * Implement a GetHeightBinarySearch method that looks for the height using the binary search algorithm guaranteeing worst case iteration time of O(log2(n)) whereas worst case iteration time of O(n) for the current linear search So if n we had 500 commits stored by height and sorted, to trigger the worst case scenario for each, pass in the most negative height you can find e.g. -1 Linear search: 500 iterations Binary search: 9 iterations with n=1000, qHeight = -1 Linear search: 1000 iterations Binary search: 10 iterations with n=1e6, qHeight = -1 Linear search: 1e6 iterations Binary search: 20 iterations Of course there are realistic expectations e.g. a max of commits that may be saved so linear search might be useful for very small size set because it has less preparing overhead and only ~2 types of comparisons, but nonetheless binary search shines as soon as we start to hit say 50 commits to search from as you can see below: ```shell $ go test -v -run=^$ -bench=MemStore goos: darwin goarch: amd64 pkg: github.com/tendermint/tendermint/lite BenchmarkMemStoreProviderGetByHeightLinearSearch5-4 300000 6491 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch50-4 200000 12064 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch100-4 50000 32987 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch500-4 5000 395521 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch1000-4 500 2940724 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch5-4 300000 6281 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch50-4 200000 10117 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch100-4 100000 18447 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch500-4 20000 89029 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch1000-4 5000 265719 ns/op 1600 B/op 15 allocs/op PASS ok github.com/tendermint/tendermint/lite 86.614s $ go test -v -run=^$ -bench=MemStore goos: darwin goarch: amd64 pkg: github.com/tendermint/tendermint/lite BenchmarkMemStoreProviderGetByHeightLinearSearch5-4 300000 6779 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch50-4 100000 12980 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch100-4 30000 43598 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch500-4 5000 377462 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch1000-4 500 3278122 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch5-4 300000 7084 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch50-4 200000 9852 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch100-4 100000 19020 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch500-4 20000 99463 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch1000-4 5000 259293 ns/op 1600 B/op 15 allocs/op PASS ok github.com/tendermint/tendermint/lite 86.204s ``` which gives ```shell $ benchstat old.txt new.txt name old time/op new time/op delta MemStoreProviderGetByHeight5-4 6.63µs ± 2% 6.68µs ± 6% ~ (p=1.000 n=2+2) MemStoreProviderGetByHeight50-4 12.5µs ± 4% 10.0µs ± 1% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight100-4 38.3µs ±14% 18.7µs ± 2% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight500-4 386µs ± 2% 94µs ± 6% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight1000-4 3.11ms ± 5% 0.26ms ± 1% ~ (p=0.333 n=2+2) ``` If need be we can make a hybrid algorithm that switches between the linear and binary search depending on the number of items. This is reminiscent of Python's TimSort algorithm.
7 years ago
lite: memStoreProvider GetHeightBinarySearch method + fix ValKeys.signHeaders Updates #1021 * Implement a GetHeightBinarySearch method that looks for the height using the binary search algorithm guaranteeing worst case iteration time of O(log2(n)) whereas worst case iteration time of O(n) for the current linear search So if n we had 500 commits stored by height and sorted, to trigger the worst case scenario for each, pass in the most negative height you can find e.g. -1 Linear search: 500 iterations Binary search: 9 iterations with n=1000, qHeight = -1 Linear search: 1000 iterations Binary search: 10 iterations with n=1e6, qHeight = -1 Linear search: 1e6 iterations Binary search: 20 iterations Of course there are realistic expectations e.g. a max of commits that may be saved so linear search might be useful for very small size set because it has less preparing overhead and only ~2 types of comparisons, but nonetheless binary search shines as soon as we start to hit say 50 commits to search from as you can see below: ```shell $ go test -v -run=^$ -bench=MemStore goos: darwin goarch: amd64 pkg: github.com/tendermint/tendermint/lite BenchmarkMemStoreProviderGetByHeightLinearSearch5-4 300000 6491 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch50-4 200000 12064 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch100-4 50000 32987 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch500-4 5000 395521 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch1000-4 500 2940724 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch5-4 300000 6281 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch50-4 200000 10117 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch100-4 100000 18447 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch500-4 20000 89029 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch1000-4 5000 265719 ns/op 1600 B/op 15 allocs/op PASS ok github.com/tendermint/tendermint/lite 86.614s $ go test -v -run=^$ -bench=MemStore goos: darwin goarch: amd64 pkg: github.com/tendermint/tendermint/lite BenchmarkMemStoreProviderGetByHeightLinearSearch5-4 300000 6779 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch50-4 100000 12980 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch100-4 30000 43598 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch500-4 5000 377462 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightLinearSearch1000-4 500 3278122 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch5-4 300000 7084 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch50-4 200000 9852 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch100-4 100000 19020 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch500-4 20000 99463 ns/op 1600 B/op 15 allocs/op BenchmarkMemStoreProviderGetByHeightBinarySearch1000-4 5000 259293 ns/op 1600 B/op 15 allocs/op PASS ok github.com/tendermint/tendermint/lite 86.204s ``` which gives ```shell $ benchstat old.txt new.txt name old time/op new time/op delta MemStoreProviderGetByHeight5-4 6.63µs ± 2% 6.68µs ± 6% ~ (p=1.000 n=2+2) MemStoreProviderGetByHeight50-4 12.5µs ± 4% 10.0µs ± 1% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight100-4 38.3µs ±14% 18.7µs ± 2% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight500-4 386µs ± 2% 94µs ± 6% ~ (p=0.333 n=2+2) MemStoreProviderGetByHeight1000-4 3.11ms ± 5% 0.26ms ± 1% ~ (p=0.333 n=2+2) ``` If need be we can make a hybrid algorithm that switches between the linear and binary search depending on the number of items. This is reminiscent of Python's TimSort algorithm.
7 years ago
  1. package lite_test
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "sync"
  6. "testing"
  7. "github.com/tendermint/tendermint/lite"
  8. )
  9. func BenchmarkGenCommit20(b *testing.B) {
  10. keys := lite.GenValKeys(20)
  11. benchmarkGenCommit(b, keys)
  12. }
  13. func BenchmarkGenCommit100(b *testing.B) {
  14. keys := lite.GenValKeys(100)
  15. benchmarkGenCommit(b, keys)
  16. }
  17. func BenchmarkGenCommitSec20(b *testing.B) {
  18. keys := lite.GenSecpValKeys(20)
  19. benchmarkGenCommit(b, keys)
  20. }
  21. func BenchmarkGenCommitSec100(b *testing.B) {
  22. keys := lite.GenSecpValKeys(100)
  23. benchmarkGenCommit(b, keys)
  24. }
  25. func benchmarkGenCommit(b *testing.B, keys lite.ValKeys) {
  26. chainID := fmt.Sprintf("bench-%d", len(keys))
  27. vals := keys.ToValidators(20, 10)
  28. for i := 0; i < b.N; i++ {
  29. h := int64(1 + i)
  30. appHash := []byte(fmt.Sprintf("h=%d", h))
  31. resHash := []byte(fmt.Sprintf("res=%d", h))
  32. keys.GenCommit(chainID, h, nil, vals, appHash, []byte("params"), resHash, 0, len(keys))
  33. }
  34. }
  35. // this benchmarks generating one key
  36. func BenchmarkGenValKeys(b *testing.B) {
  37. keys := lite.GenValKeys(20)
  38. for i := 0; i < b.N; i++ {
  39. keys = keys.Extend(1)
  40. }
  41. }
  42. // this benchmarks generating one key
  43. func BenchmarkGenSecpValKeys(b *testing.B) {
  44. keys := lite.GenSecpValKeys(20)
  45. for i := 0; i < b.N; i++ {
  46. keys = keys.Extend(1)
  47. }
  48. }
  49. func BenchmarkToValidators20(b *testing.B) {
  50. benchmarkToValidators(b, 20)
  51. }
  52. func BenchmarkToValidators100(b *testing.B) {
  53. benchmarkToValidators(b, 100)
  54. }
  55. // this benchmarks constructing the validator set (.PubKey() * nodes)
  56. func benchmarkToValidators(b *testing.B, nodes int) {
  57. keys := lite.GenValKeys(nodes)
  58. for i := 1; i <= b.N; i++ {
  59. keys.ToValidators(int64(2*i), int64(i))
  60. }
  61. }
  62. func BenchmarkToValidatorsSec100(b *testing.B) {
  63. benchmarkToValidatorsSec(b, 100)
  64. }
  65. // this benchmarks constructing the validator set (.PubKey() * nodes)
  66. func benchmarkToValidatorsSec(b *testing.B, nodes int) {
  67. keys := lite.GenSecpValKeys(nodes)
  68. for i := 1; i <= b.N; i++ {
  69. keys.ToValidators(int64(2*i), int64(i))
  70. }
  71. }
  72. func BenchmarkCertifyCommit20(b *testing.B) {
  73. keys := lite.GenValKeys(20)
  74. benchmarkCertifyCommit(b, keys)
  75. }
  76. func BenchmarkCertifyCommit100(b *testing.B) {
  77. keys := lite.GenValKeys(100)
  78. benchmarkCertifyCommit(b, keys)
  79. }
  80. func BenchmarkCertifyCommitSec20(b *testing.B) {
  81. keys := lite.GenSecpValKeys(20)
  82. benchmarkCertifyCommit(b, keys)
  83. }
  84. func BenchmarkCertifyCommitSec100(b *testing.B) {
  85. keys := lite.GenSecpValKeys(100)
  86. benchmarkCertifyCommit(b, keys)
  87. }
  88. func benchmarkCertifyCommit(b *testing.B, keys lite.ValKeys) {
  89. chainID := "bench-certify"
  90. vals := keys.ToValidators(20, 10)
  91. cert := lite.NewStaticCertifier(chainID, vals)
  92. check := keys.GenCommit(chainID, 123, nil, vals, []byte("foo"), []byte("params"), []byte("res"), 0, len(keys))
  93. for i := 0; i < b.N; i++ {
  94. err := cert.Certify(check)
  95. if err != nil {
  96. panic(err)
  97. }
  98. }
  99. }
  100. type algo bool
  101. const (
  102. linearSearch = true
  103. binarySearch = false
  104. )
  105. // Lazy load the commits
  106. var fcs5, fcs50, fcs100, fcs500, fcs1000 []lite.FullCommit
  107. var h5, h50, h100, h500, h1000 []int64
  108. var commitsOnce sync.Once
  109. func lazyGenerateFullCommits() {
  110. commitsOnce.Do(func() {
  111. fcs5, h5 = genFullCommits(nil, nil, 5)
  112. fcs50, h50 = genFullCommits(fcs5, h5, 50)
  113. fcs100, h100 = genFullCommits(fcs50, h50, 100)
  114. fcs500, h500 = genFullCommits(fcs100, h100, 500)
  115. fcs1000, h1000 = genFullCommits(fcs500, h500, 1000)
  116. })
  117. }
  118. func BenchmarkMemStoreProviderGetByHeightLinearSearch5(b *testing.B) {
  119. benchmarkMemStoreProviderGetByHeight(b, fcs5, h5, linearSearch)
  120. }
  121. func BenchmarkMemStoreProviderGetByHeightLinearSearch50(b *testing.B) {
  122. benchmarkMemStoreProviderGetByHeight(b, fcs50, h50, linearSearch)
  123. }
  124. func BenchmarkMemStoreProviderGetByHeightLinearSearch100(b *testing.B) {
  125. benchmarkMemStoreProviderGetByHeight(b, fcs100, h100, linearSearch)
  126. }
  127. func BenchmarkMemStoreProviderGetByHeightLinearSearch500(b *testing.B) {
  128. benchmarkMemStoreProviderGetByHeight(b, fcs500, h500, linearSearch)
  129. }
  130. func BenchmarkMemStoreProviderGetByHeightLinearSearch1000(b *testing.B) {
  131. benchmarkMemStoreProviderGetByHeight(b, fcs1000, h1000, linearSearch)
  132. }
  133. func BenchmarkMemStoreProviderGetByHeightBinarySearch5(b *testing.B) {
  134. benchmarkMemStoreProviderGetByHeight(b, fcs5, h5, binarySearch)
  135. }
  136. func BenchmarkMemStoreProviderGetByHeightBinarySearch50(b *testing.B) {
  137. benchmarkMemStoreProviderGetByHeight(b, fcs50, h50, binarySearch)
  138. }
  139. func BenchmarkMemStoreProviderGetByHeightBinarySearch100(b *testing.B) {
  140. benchmarkMemStoreProviderGetByHeight(b, fcs100, h100, binarySearch)
  141. }
  142. func BenchmarkMemStoreProviderGetByHeightBinarySearch500(b *testing.B) {
  143. benchmarkMemStoreProviderGetByHeight(b, fcs500, h500, binarySearch)
  144. }
  145. func BenchmarkMemStoreProviderGetByHeightBinarySearch1000(b *testing.B) {
  146. benchmarkMemStoreProviderGetByHeight(b, fcs1000, h1000, binarySearch)
  147. }
  148. var rng = rand.New(rand.NewSource(10))
  149. func benchmarkMemStoreProviderGetByHeight(b *testing.B, fcs []lite.FullCommit, fHeights []int64, algo algo) {
  150. lazyGenerateFullCommits()
  151. b.StopTimer()
  152. mp := lite.NewMemStoreProvider()
  153. for i, fc := range fcs {
  154. if err := mp.StoreCommit(fc); err != nil {
  155. b.Fatalf("FullCommit #%d: err: %v", i, err)
  156. }
  157. }
  158. qHeights := make([]int64, len(fHeights))
  159. copy(qHeights, fHeights)
  160. // Append some non-existent heights to trigger the worst cases.
  161. qHeights = append(qHeights, 19, -100, -10000, 1e7, -17, 31, -1e9)
  162. searchFn := mp.GetByHeight
  163. if algo == binarySearch {
  164. searchFn = mp.(interface {
  165. GetByHeightBinarySearch(h int64) (lite.FullCommit, error)
  166. }).GetByHeightBinarySearch
  167. }
  168. hPerm := rng.Perm(len(qHeights))
  169. b.StartTimer()
  170. b.ResetTimer()
  171. for i := 0; i < b.N; i++ {
  172. for _, j := range hPerm {
  173. h := qHeights[j]
  174. if _, err := searchFn(h); err != nil {
  175. }
  176. }
  177. }
  178. b.ReportAllocs()
  179. }
  180. func genFullCommits(prevFC []lite.FullCommit, prevH []int64, want int) ([]lite.FullCommit, []int64) {
  181. fcs := make([]lite.FullCommit, len(prevFC))
  182. copy(fcs, prevFC)
  183. heights := make([]int64, len(prevH))
  184. copy(heights, prevH)
  185. appHash := []byte("benchmarks")
  186. chainID := "benchmarks-gen-full-commits"
  187. n := want
  188. keys := lite.GenValKeys(2 + (n / 3))
  189. for i := 0; i < n; i++ {
  190. vals := keys.ToValidators(10, int64(n/2))
  191. h := int64(20 + 10*i)
  192. fcs = append(fcs, keys.GenFullCommit(chainID, h, nil, vals, appHash, []byte("params"), []byte("results"), 0, 5))
  193. heights = append(heights, h)
  194. }
  195. return fcs, heights
  196. }