bench_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package filters
  17. import (
  18. "context"
  19. "fmt"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/common/bitutil"
  24. "github.com/ethereum/go-ethereum/core/bloombits"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. "github.com/ethereum/go-ethereum/node"
  29. )
  30. func BenchmarkBloomBits512(b *testing.B) {
  31. benchmarkBloomBits(b, 512)
  32. }
  33. func BenchmarkBloomBits1k(b *testing.B) {
  34. benchmarkBloomBits(b, 1024)
  35. }
  36. func BenchmarkBloomBits2k(b *testing.B) {
  37. benchmarkBloomBits(b, 2048)
  38. }
  39. func BenchmarkBloomBits4k(b *testing.B) {
  40. benchmarkBloomBits(b, 4096)
  41. }
  42. func BenchmarkBloomBits8k(b *testing.B) {
  43. benchmarkBloomBits(b, 8192)
  44. }
  45. func BenchmarkBloomBits16k(b *testing.B) {
  46. benchmarkBloomBits(b, 16384)
  47. }
  48. func BenchmarkBloomBits32k(b *testing.B) {
  49. benchmarkBloomBits(b, 32768)
  50. }
  51. const benchFilterCnt = 2000
  52. func benchmarkBloomBits(b *testing.B, sectionSize uint64) {
  53. b.Skip("test disabled: this tests presume (and modify) an existing datadir.")
  54. benchDataDir := node.DefaultDataDir() + "/geth/chaindata"
  55. b.Log("Running bloombits benchmark section size:", sectionSize)
  56. db, err := rawdb.NewLevelDBDatabase(benchDataDir, 128, 1024, "", false)
  57. if err != nil {
  58. b.Fatalf("error opening database at %v: %v", benchDataDir, err)
  59. }
  60. head := rawdb.ReadHeadBlockHash(db)
  61. if head == (common.Hash{}) {
  62. b.Fatalf("chain data not found at %v", benchDataDir)
  63. }
  64. clearBloomBits(db)
  65. b.Log("Generating bloombits data...")
  66. headNum := rawdb.ReadHeaderNumber(db, head)
  67. if headNum == nil || *headNum < sectionSize+512 {
  68. b.Fatalf("not enough blocks for running a benchmark")
  69. }
  70. start := time.Now()
  71. cnt := (*headNum - 512) / sectionSize
  72. var dataSize, compSize uint64
  73. for sectionIdx := uint64(0); sectionIdx < cnt; sectionIdx++ {
  74. bc, err := bloombits.NewGenerator(uint(sectionSize))
  75. if err != nil {
  76. b.Fatalf("failed to create generator: %v", err)
  77. }
  78. var header *types.Header
  79. for i := sectionIdx * sectionSize; i < (sectionIdx+1)*sectionSize; i++ {
  80. hash := rawdb.ReadCanonicalHash(db, i)
  81. if header = rawdb.ReadHeader(db, hash, i); header == nil {
  82. b.Fatalf("Error creating bloomBits data")
  83. return
  84. }
  85. bc.AddBloom(uint(i-sectionIdx*sectionSize), header.Bloom)
  86. }
  87. sectionHead := rawdb.ReadCanonicalHash(db, (sectionIdx+1)*sectionSize-1)
  88. for i := 0; i < types.BloomBitLength; i++ {
  89. data, err := bc.Bitset(uint(i))
  90. if err != nil {
  91. b.Fatalf("failed to retrieve bitset: %v", err)
  92. }
  93. comp := bitutil.CompressBytes(data)
  94. dataSize += uint64(len(data))
  95. compSize += uint64(len(comp))
  96. rawdb.WriteBloomBits(db, uint(i), sectionIdx, sectionHead, comp)
  97. }
  98. //if sectionIdx%50 == 0 {
  99. // b.Log(" section", sectionIdx, "/", cnt)
  100. //}
  101. }
  102. d := time.Since(start)
  103. b.Log("Finished generating bloombits data")
  104. b.Log(" ", d, "total ", d/time.Duration(cnt*sectionSize), "per block")
  105. b.Log(" data size:", dataSize, " compressed size:", compSize, " compression ratio:", float64(compSize)/float64(dataSize))
  106. b.Log("Running filter benchmarks...")
  107. start = time.Now()
  108. var (
  109. backend *testBackend
  110. sys *FilterSystem
  111. )
  112. for i := 0; i < benchFilterCnt; i++ {
  113. if i%20 == 0 {
  114. db.Close()
  115. db, _ = rawdb.NewLevelDBDatabase(benchDataDir, 128, 1024, "", false)
  116. backend = &testBackend{db: db, sections: cnt}
  117. sys = NewFilterSystem(backend, Config{})
  118. }
  119. var addr common.Address
  120. addr[0] = byte(i)
  121. addr[1] = byte(i / 256)
  122. filter := sys.NewRangeFilter(0, int64(cnt*sectionSize-1), []common.Address{addr}, nil)
  123. if _, err := filter.Logs(context.Background()); err != nil {
  124. b.Error("filter.Logs error:", err)
  125. }
  126. }
  127. d = time.Since(start)
  128. b.Log("Finished running filter benchmarks")
  129. b.Log(" ", d, "total ", d/time.Duration(benchFilterCnt), "per address", d*time.Duration(1000000)/time.Duration(benchFilterCnt*cnt*sectionSize), "per million blocks")
  130. db.Close()
  131. }
  132. //nolint:unused
  133. func clearBloomBits(db ethdb.Database) {
  134. var bloomBitsPrefix = []byte("bloomBits-")
  135. fmt.Println("Clearing bloombits data...")
  136. it := db.NewIterator(bloomBitsPrefix, nil)
  137. for it.Next() {
  138. db.Delete(it.Key())
  139. }
  140. it.Release()
  141. }
  142. func BenchmarkNoBloomBits(b *testing.B) {
  143. b.Skip("test disabled: this tests presume (and modify) an existing datadir.")
  144. benchDataDir := node.DefaultDataDir() + "/geth/chaindata"
  145. b.Log("Running benchmark without bloombits")
  146. db, err := rawdb.NewLevelDBDatabase(benchDataDir, 128, 1024, "", false)
  147. if err != nil {
  148. b.Fatalf("error opening database at %v: %v", benchDataDir, err)
  149. }
  150. head := rawdb.ReadHeadBlockHash(db)
  151. if head == (common.Hash{}) {
  152. b.Fatalf("chain data not found at %v", benchDataDir)
  153. }
  154. headNum := rawdb.ReadHeaderNumber(db, head)
  155. clearBloomBits(db)
  156. _, sys := newTestFilterSystem(b, db, Config{})
  157. b.Log("Running filter benchmarks...")
  158. start := time.Now()
  159. filter := sys.NewRangeFilter(0, int64(*headNum), []common.Address{{}}, nil)
  160. filter.Logs(context.Background())
  161. d := time.Since(start)
  162. b.Log("Finished running filter benchmarks")
  163. b.Log(" ", d, "total ", d*time.Duration(1000000)/time.Duration(*headNum+1), "per million blocks")
  164. db.Close()
  165. }