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