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