github.com/daethereum/go-dae@v2.2.3+incompatible/core/bench_test.go (about) 1 // Copyright 2015 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 core 18 19 import ( 20 "crypto/ecdsa" 21 "math/big" 22 "testing" 23 24 "github.com/daethereum/go-dae/common" 25 "github.com/daethereum/go-dae/common/math" 26 "github.com/daethereum/go-dae/consensus/ethash" 27 "github.com/daethereum/go-dae/core/rawdb" 28 "github.com/daethereum/go-dae/core/types" 29 "github.com/daethereum/go-dae/core/vm" 30 "github.com/daethereum/go-dae/crypto" 31 "github.com/daethereum/go-dae/ethdb" 32 "github.com/daethereum/go-dae/params" 33 ) 34 35 func BenchmarkInsertChain_empty_memdb(b *testing.B) { 36 benchInsertChain(b, false, nil) 37 } 38 func BenchmarkInsertChain_empty_diskdb(b *testing.B) { 39 benchInsertChain(b, true, nil) 40 } 41 func BenchmarkInsertChain_valueTx_memdb(b *testing.B) { 42 benchInsertChain(b, false, genValueTx(0)) 43 } 44 func BenchmarkInsertChain_valueTx_diskdb(b *testing.B) { 45 benchInsertChain(b, true, genValueTx(0)) 46 } 47 func BenchmarkInsertChain_valueTx_100kB_memdb(b *testing.B) { 48 benchInsertChain(b, false, genValueTx(100*1024)) 49 } 50 func BenchmarkInsertChain_valueTx_100kB_diskdb(b *testing.B) { 51 benchInsertChain(b, true, genValueTx(100*1024)) 52 } 53 func BenchmarkInsertChain_uncles_memdb(b *testing.B) { 54 benchInsertChain(b, false, genUncles) 55 } 56 func BenchmarkInsertChain_uncles_diskdb(b *testing.B) { 57 benchInsertChain(b, true, genUncles) 58 } 59 func BenchmarkInsertChain_ring200_memdb(b *testing.B) { 60 benchInsertChain(b, false, genTxRing(200)) 61 } 62 func BenchmarkInsertChain_ring200_diskdb(b *testing.B) { 63 benchInsertChain(b, true, genTxRing(200)) 64 } 65 func BenchmarkInsertChain_ring1000_memdb(b *testing.B) { 66 benchInsertChain(b, false, genTxRing(1000)) 67 } 68 func BenchmarkInsertChain_ring1000_diskdb(b *testing.B) { 69 benchInsertChain(b, true, genTxRing(1000)) 70 } 71 72 var ( 73 // This is the content of the genesis block used by the benchmarks. 74 benchRootKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 75 benchRootAddr = crypto.PubkeyToAddress(benchRootKey.PublicKey) 76 benchRootFunds = math.BigPow(2, 200) 77 ) 78 79 // genValueTx returns a block generator that includes a single 80 // value-transfer transaction with n bytes of extra data in each 81 // block. 82 func genValueTx(nbytes int) func(int, *BlockGen) { 83 return func(i int, gen *BlockGen) { 84 toaddr := common.Address{} 85 data := make([]byte, nbytes) 86 gas, _ := IntrinsicGas(data, nil, false, false, false) 87 signer := types.MakeSigner(gen.config, big.NewInt(int64(i))) 88 gasPrice := big.NewInt(0) 89 if gen.header.BaseFee != nil { 90 gasPrice = gen.header.BaseFee 91 } 92 tx, _ := types.SignNewTx(benchRootKey, signer, &types.LegacyTx{ 93 Nonce: gen.TxNonce(benchRootAddr), 94 To: &toaddr, 95 Value: big.NewInt(1), 96 Gas: gas, 97 Data: data, 98 GasPrice: gasPrice, 99 }) 100 gen.AddTx(tx) 101 } 102 } 103 104 var ( 105 ringKeys = make([]*ecdsa.PrivateKey, 1000) 106 ringAddrs = make([]common.Address, len(ringKeys)) 107 ) 108 109 func init() { 110 ringKeys[0] = benchRootKey 111 ringAddrs[0] = benchRootAddr 112 for i := 1; i < len(ringKeys); i++ { 113 ringKeys[i], _ = crypto.GenerateKey() 114 ringAddrs[i] = crypto.PubkeyToAddress(ringKeys[i].PublicKey) 115 } 116 } 117 118 // genTxRing returns a block generator that sends ether in a ring 119 // among n accounts. This is creates n entries in the state database 120 // and fills the blocks with many small transactions. 121 func genTxRing(naccounts int) func(int, *BlockGen) { 122 from := 0 123 availableFunds := new(big.Int).Set(benchRootFunds) 124 return func(i int, gen *BlockGen) { 125 block := gen.PrevBlock(i - 1) 126 gas := block.GasLimit() 127 gasPrice := big.NewInt(0) 128 if gen.header.BaseFee != nil { 129 gasPrice = gen.header.BaseFee 130 } 131 signer := types.MakeSigner(gen.config, big.NewInt(int64(i))) 132 for { 133 gas -= params.TxGas 134 if gas < params.TxGas { 135 break 136 } 137 to := (from + 1) % naccounts 138 burn := new(big.Int).SetUint64(params.TxGas) 139 burn.Mul(burn, gen.header.BaseFee) 140 availableFunds.Sub(availableFunds, burn) 141 if availableFunds.Cmp(big.NewInt(1)) < 0 { 142 panic("not enough funds") 143 } 144 tx, err := types.SignNewTx(ringKeys[from], signer, 145 &types.LegacyTx{ 146 Nonce: gen.TxNonce(ringAddrs[from]), 147 To: &ringAddrs[to], 148 Value: availableFunds, 149 Gas: params.TxGas, 150 GasPrice: gasPrice, 151 }) 152 if err != nil { 153 panic(err) 154 } 155 gen.AddTx(tx) 156 from = to 157 } 158 } 159 } 160 161 // genUncles generates blocks with two uncle headers. 162 func genUncles(i int, gen *BlockGen) { 163 if i >= 7 { 164 b2 := gen.PrevBlock(i - 6).Header() 165 b2.Extra = []byte("foo") 166 gen.AddUncle(b2) 167 b3 := gen.PrevBlock(i - 6).Header() 168 b3.Extra = []byte("bar") 169 gen.AddUncle(b3) 170 } 171 } 172 173 func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) { 174 // Create the database in memory or in a temporary directory. 175 var db ethdb.Database 176 var err error 177 if !disk { 178 db = rawdb.NewMemoryDatabase() 179 } else { 180 dir := b.TempDir() 181 db, err = rawdb.NewLevelDBDatabase(dir, 128, 128, "", false) 182 if err != nil { 183 b.Fatalf("cannot create temporary database: %v", err) 184 } 185 defer db.Close() 186 } 187 188 // Generate a chain of b.N blocks using the supplied block 189 // generator function. 190 gspec := Genesis{ 191 Config: params.TestChainConfig, 192 Alloc: GenesisAlloc{benchRootAddr: {Balance: benchRootFunds}}, 193 } 194 genesis := gspec.MustCommit(db) 195 chain, _ := GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, b.N, gen) 196 197 // Time the insertion of the new chain. 198 // State and blocks are stored in the same DB. 199 chainman, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil) 200 defer chainman.Stop() 201 b.ReportAllocs() 202 b.ResetTimer() 203 if i, err := chainman.InsertChain(chain); err != nil { 204 b.Fatalf("insert error (block %d): %v\n", i, err) 205 } 206 } 207 208 func BenchmarkChainRead_header_10k(b *testing.B) { 209 benchReadChain(b, false, 10000) 210 } 211 func BenchmarkChainRead_full_10k(b *testing.B) { 212 benchReadChain(b, true, 10000) 213 } 214 func BenchmarkChainRead_header_100k(b *testing.B) { 215 benchReadChain(b, false, 100000) 216 } 217 func BenchmarkChainRead_full_100k(b *testing.B) { 218 benchReadChain(b, true, 100000) 219 } 220 func BenchmarkChainRead_header_500k(b *testing.B) { 221 benchReadChain(b, false, 500000) 222 } 223 func BenchmarkChainRead_full_500k(b *testing.B) { 224 benchReadChain(b, true, 500000) 225 } 226 func BenchmarkChainWrite_header_10k(b *testing.B) { 227 benchWriteChain(b, false, 10000) 228 } 229 func BenchmarkChainWrite_full_10k(b *testing.B) { 230 benchWriteChain(b, true, 10000) 231 } 232 func BenchmarkChainWrite_header_100k(b *testing.B) { 233 benchWriteChain(b, false, 100000) 234 } 235 func BenchmarkChainWrite_full_100k(b *testing.B) { 236 benchWriteChain(b, true, 100000) 237 } 238 func BenchmarkChainWrite_header_500k(b *testing.B) { 239 benchWriteChain(b, false, 500000) 240 } 241 func BenchmarkChainWrite_full_500k(b *testing.B) { 242 benchWriteChain(b, true, 500000) 243 } 244 245 // makeChainForBench writes a given number of headers or empty blocks/receipts 246 // into a database. 247 func makeChainForBench(db ethdb.Database, full bool, count uint64) { 248 var hash common.Hash 249 for n := uint64(0); n < count; n++ { 250 header := &types.Header{ 251 Coinbase: common.Address{}, 252 Number: big.NewInt(int64(n)), 253 ParentHash: hash, 254 Difficulty: big.NewInt(1), 255 UncleHash: types.EmptyUncleHash, 256 TxHash: types.EmptyRootHash, 257 ReceiptHash: types.EmptyRootHash, 258 } 259 hash = header.Hash() 260 261 rawdb.WriteHeader(db, header) 262 rawdb.WriteCanonicalHash(db, hash, n) 263 rawdb.WriteTd(db, hash, n, big.NewInt(int64(n+1))) 264 265 if full || n == 0 { 266 block := types.NewBlockWithHeader(header) 267 rawdb.WriteBody(db, hash, n, block.Body()) 268 rawdb.WriteReceipts(db, hash, n, nil) 269 rawdb.WriteHeadBlockHash(db, hash) 270 } 271 } 272 } 273 274 func benchWriteChain(b *testing.B, full bool, count uint64) { 275 for i := 0; i < b.N; i++ { 276 dir := b.TempDir() 277 db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false) 278 if err != nil { 279 b.Fatalf("error opening database at %v: %v", dir, err) 280 } 281 makeChainForBench(db, full, count) 282 db.Close() 283 } 284 } 285 286 func benchReadChain(b *testing.B, full bool, count uint64) { 287 dir := b.TempDir() 288 289 db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false) 290 if err != nil { 291 b.Fatalf("error opening database at %v: %v", dir, err) 292 } 293 makeChainForBench(db, full, count) 294 db.Close() 295 cacheConfig := *defaultCacheConfig 296 cacheConfig.TrieDirtyDisabled = true 297 298 b.ReportAllocs() 299 b.ResetTimer() 300 301 for i := 0; i < b.N; i++ { 302 db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false) 303 if err != nil { 304 b.Fatalf("error opening database at %v: %v", dir, err) 305 } 306 chain, err := NewBlockChain(db, &cacheConfig, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil, nil) 307 if err != nil { 308 b.Fatalf("error creating chain: %v", err) 309 } 310 311 for n := uint64(0); n < count; n++ { 312 header := chain.GetHeaderByNumber(n) 313 if full { 314 hash := header.Hash() 315 rawdb.ReadBody(db, hash, n) 316 rawdb.ReadReceipts(db, hash, n, chain.Config()) 317 } 318 } 319 chain.Stop() 320 db.Close() 321 } 322 }