bench_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. "bytes"
  19. "context"
  20. "fmt"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/common/bitutil"
  25. "github.com/ethereum/go-ethereum/core/bloombits"
  26. "github.com/ethereum/go-ethereum/core/rawdb"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/ethdb"
  29. "github.com/ethereum/go-ethereum/event"
  30. "github.com/ethereum/go-ethereum/node"
  31. )
  32. func BenchmarkBloomBits512(b *testing.B) {
  33. benchmarkBloomBits(b, 512)
  34. }
  35. func BenchmarkBloomBits1k(b *testing.B) {
  36. benchmarkBloomBits(b, 1024)
  37. }
  38. func BenchmarkBloomBits2k(b *testing.B) {
  39. benchmarkBloomBits(b, 2048)
  40. }
  41. func BenchmarkBloomBits4k(b *testing.B) {
  42. benchmarkBloomBits(b, 4096)
  43. }
  44. func BenchmarkBloomBits8k(b *testing.B) {
  45. benchmarkBloomBits(b, 8192)
  46. }
  47. func BenchmarkBloomBits16k(b *testing.B) {
  48. benchmarkBloomBits(b, 16384)
  49. }
  50. func BenchmarkBloomBits32k(b *testing.B) {
  51. benchmarkBloomBits(b, 32768)
  52. }
  53. const benchFilterCnt = 2000
  54. func benchmarkBloomBits(b *testing.B, sectionSize uint64) {
  55. benchDataDir := node.DefaultDataDir() + "/geth/chaindata"
  56. fmt.Println("Running bloombits benchmark section size:", sectionSize)
  57. db, err := ethdb.NewLDBDatabase(benchDataDir, 128, 1024)
  58. if err != nil {
  59. b.Fatalf("error opening database at %v: %v", benchDataDir, err)
  60. }
  61. head := rawdb.ReadHeadBlockHash(db)
  62. if head == (common.Hash{}) {
  63. b.Fatalf("chain data not found at %v", benchDataDir)
  64. }
  65. clearBloomBits(db)
  66. fmt.Println("Generating bloombits data...")
  67. headNum := rawdb.ReadHeaderNumber(db, head)
  68. if headNum == nil || *headNum < sectionSize+512 {
  69. b.Fatalf("not enough blocks for running a benchmark")
  70. }
  71. start := time.Now()
  72. cnt := (*headNum - 512) / sectionSize
  73. var dataSize, compSize uint64
  74. for sectionIdx := uint64(0); sectionIdx < cnt; sectionIdx++ {
  75. bc, err := bloombits.NewGenerator(uint(sectionSize))
  76. if err != nil {
  77. b.Fatalf("failed to create generator: %v", err)
  78. }
  79. var header *types.Header
  80. for i := sectionIdx * sectionSize; i < (sectionIdx+1)*sectionSize; i++ {
  81. hash := rawdb.ReadCanonicalHash(db, i)
  82. header = rawdb.ReadHeader(db, hash, i)
  83. if header == nil {
  84. b.Fatalf("Error creating bloomBits data")
  85. }
  86. bc.AddBloom(uint(i-sectionIdx*sectionSize), header.Bloom)
  87. }
  88. sectionHead := rawdb.ReadCanonicalHash(db, (sectionIdx+1)*sectionSize-1)
  89. for i := 0; i < types.BloomBitLength; i++ {
  90. data, err := bc.Bitset(uint(i))
  91. if err != nil {
  92. b.Fatalf("failed to retrieve bitset: %v", err)
  93. }
  94. comp := bitutil.CompressBytes(data)
  95. dataSize += uint64(len(data))
  96. compSize += uint64(len(comp))
  97. rawdb.WriteBloomBits(db, uint(i), sectionIdx, sectionHead, comp)
  98. }
  99. //if sectionIdx%50 == 0 {
  100. // fmt.Println(" section", sectionIdx, "/", cnt)
  101. //}
  102. }
  103. d := time.Since(start)
  104. fmt.Println("Finished generating bloombits data")
  105. fmt.Println(" ", d, "total ", d/time.Duration(cnt*sectionSize), "per block")
  106. fmt.Println(" data size:", dataSize, " compressed size:", compSize, " compression ratio:", float64(compSize)/float64(dataSize))
  107. fmt.Println("Running filter benchmarks...")
  108. start = time.Now()
  109. mux := new(event.TypeMux)
  110. var backend *testBackend
  111. for i := 0; i < benchFilterCnt; i++ {
  112. if i%20 == 0 {
  113. db.Close()
  114. db, _ = ethdb.NewLDBDatabase(benchDataDir, 128, 1024)
  115. backend = &testBackend{mux, db, cnt, new(event.Feed), new(event.Feed), new(event.Feed), new(event.Feed)}
  116. }
  117. var addr common.Address
  118. addr[0] = byte(i)
  119. addr[1] = byte(i / 256)
  120. filter := New(backend, 0, int64(cnt*sectionSize-1), []common.Address{addr}, nil)
  121. if _, err := filter.Logs(context.Background()); err != nil {
  122. b.Error("filter.Find error:", err)
  123. }
  124. }
  125. d = time.Since(start)
  126. fmt.Println("Finished running filter benchmarks")
  127. fmt.Println(" ", d, "total ", d/time.Duration(benchFilterCnt), "per address", d*time.Duration(1000000)/time.Duration(benchFilterCnt*cnt*sectionSize), "per million blocks")
  128. db.Close()
  129. }
  130. func forEachKey(db ethdb.Database, startPrefix, endPrefix []byte, fn func(key []byte)) {
  131. it := db.(*ethdb.LDBDatabase).NewIterator()
  132. it.Seek(startPrefix)
  133. for it.Valid() {
  134. key := it.Key()
  135. cmpLen := len(key)
  136. if len(endPrefix) < cmpLen {
  137. cmpLen = len(endPrefix)
  138. }
  139. if bytes.Compare(key[:cmpLen], endPrefix) == 1 {
  140. break
  141. }
  142. fn(common.CopyBytes(key))
  143. it.Next()
  144. }
  145. it.Release()
  146. }
  147. var bloomBitsPrefix = []byte("bloomBits-")
  148. func clearBloomBits(db ethdb.Database) {
  149. fmt.Println("Clearing bloombits data...")
  150. forEachKey(db, bloomBitsPrefix, bloomBitsPrefix, func(key []byte) {
  151. db.Delete(key)
  152. })
  153. }
  154. func BenchmarkNoBloomBits(b *testing.B) {
  155. benchDataDir := node.DefaultDataDir() + "/geth/chaindata"
  156. fmt.Println("Running benchmark without bloombits")
  157. db, err := ethdb.NewLDBDatabase(benchDataDir, 128, 1024)
  158. if err != nil {
  159. b.Fatalf("error opening database at %v: %v", benchDataDir, err)
  160. }
  161. head := rawdb.ReadHeadBlockHash(db)
  162. if head == (common.Hash{}) {
  163. b.Fatalf("chain data not found at %v", benchDataDir)
  164. }
  165. headNum := rawdb.ReadHeaderNumber(db, head)
  166. clearBloomBits(db)
  167. fmt.Println("Running filter benchmarks...")
  168. start := time.Now()
  169. mux := new(event.TypeMux)
  170. backend := &testBackend{mux, db, 0, new(event.Feed), new(event.Feed), new(event.Feed), new(event.Feed)}
  171. filter := New(backend, 0, int64(*headNum), []common.Address{{}}, nil)
  172. filter.Logs(context.Background())
  173. d := time.Since(start)
  174. fmt.Println("Finished running filter benchmarks")
  175. fmt.Println(" ", d, "total ", d*time.Duration(1000000)/time.Duration(*headNum+1), "per million blocks")
  176. db.Close()
  177. }