github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/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/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/common/math" 26 "github.com/ethereum/go-ethereum/consensus/ethash" 27 "github.com/ethereum/go-ethereum/core/rawdb" 28 "github.com/ethereum/go-ethereum/core/types" 29 "github.com/ethereum/go-ethereum/core/vm" 30 "github.com/ethereum/go-ethereum/crypto" 31 "github.com/ethereum/go-ethereum/ethdb" 32 "github.com/ethereum/go-ethereum/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, false) 87 signer := gen.Signer() 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 := gen.Signer() 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: types.GenesisAlloc{benchRootAddr: {Balance: benchRootFunds}}, 193 } 194 _, chain, _ := GenerateChainWithGenesis(gspec, ethash.NewFaker(), b.N, gen) 195 196 // Time the insertion of the new chain. 197 // State and blocks are stored in the same DB. 198 chainman, _ := NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil) 199 defer chainman.Stop() 200 b.ReportAllocs() 201 b.ResetTimer() 202 if i, err := chainman.InsertChain(chain); err != nil { 203 b.Fatalf("insert error (block %d): %v\n", i, err) 204 } 205 } 206 207 func BenchmarkChainRead_header_10k(b *testing.B) { 208 benchReadChain(b, false, 10000) 209 } 210 func BenchmarkChainRead_full_10k(b *testing.B) { 211 benchReadChain(b, true, 10000) 212 } 213 func BenchmarkChainRead_header_100k(b *testing.B) { 214 benchReadChain(b, false, 100000) 215 } 216 func BenchmarkChainRead_full_100k(b *testing.B) { 217 benchReadChain(b, true, 100000) 218 } 219 func BenchmarkChainRead_header_500k(b *testing.B) { 220 benchReadChain(b, false, 500000) 221 } 222 func BenchmarkChainRead_full_500k(b *testing.B) { 223 benchReadChain(b, true, 500000) 224 } 225 func BenchmarkChainWrite_header_10k(b *testing.B) { 226 benchWriteChain(b, false, 10000) 227 } 228 func BenchmarkChainWrite_full_10k(b *testing.B) { 229 benchWriteChain(b, true, 10000) 230 } 231 func BenchmarkChainWrite_header_100k(b *testing.B) { 232 benchWriteChain(b, false, 100000) 233 } 234 func BenchmarkChainWrite_full_100k(b *testing.B) { 235 benchWriteChain(b, true, 100000) 236 } 237 func BenchmarkChainWrite_header_500k(b *testing.B) { 238 benchWriteChain(b, false, 500000) 239 } 240 func BenchmarkChainWrite_full_500k(b *testing.B) { 241 benchWriteChain(b, true, 500000) 242 } 243 244 // makeChainForBench writes a given number of headers or empty blocks/receipts 245 // into a database. 246 func makeChainForBench(db ethdb.Database, genesis *Genesis, full bool, count uint64) { 247 var hash common.Hash 248 for n := uint64(0); n < count; n++ { 249 header := &types.Header{ 250 Coinbase: common.Address{}, 251 Number: big.NewInt(int64(n)), 252 ParentHash: hash, 253 Difficulty: big.NewInt(1), 254 UncleHash: types.EmptyUncleHash, 255 TxHash: types.EmptyTxsHash, 256 ReceiptHash: types.EmptyReceiptsHash, 257 } 258 if n == 0 { 259 header = genesis.ToBlock().Header() 260 } 261 hash = header.Hash() 262 263 rawdb.WriteHeader(db, header) 264 rawdb.WriteCanonicalHash(db, hash, n) 265 rawdb.WriteTd(db, hash, n, big.NewInt(int64(n+1))) 266 267 if n == 0 { 268 rawdb.WriteChainConfig(db, hash, genesis.Config) 269 } 270 rawdb.WriteHeadHeaderHash(db, hash) 271 272 if full || n == 0 { 273 block := types.NewBlockWithHeader(header) 274 rawdb.WriteBody(db, hash, n, block.Body()) 275 rawdb.WriteReceipts(db, hash, n, nil) 276 rawdb.WriteHeadBlockHash(db, hash) 277 } 278 } 279 } 280 281 func benchWriteChain(b *testing.B, full bool, count uint64) { 282 genesis := &Genesis{Config: params.AllEthashProtocolChanges} 283 for i := 0; i < b.N; i++ { 284 dir := b.TempDir() 285 db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false) 286 if err != nil { 287 b.Fatalf("error opening database at %v: %v", dir, err) 288 } 289 makeChainForBench(db, genesis, full, count) 290 db.Close() 291 } 292 } 293 294 func benchReadChain(b *testing.B, full bool, count uint64) { 295 dir := b.TempDir() 296 297 db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false) 298 if err != nil { 299 b.Fatalf("error opening database at %v: %v", dir, err) 300 } 301 genesis := &Genesis{Config: params.AllEthashProtocolChanges} 302 makeChainForBench(db, genesis, full, count) 303 db.Close() 304 cacheConfig := *defaultCacheConfig 305 cacheConfig.TrieDirtyDisabled = true 306 307 b.ReportAllocs() 308 b.ResetTimer() 309 310 for i := 0; i < b.N; i++ { 311 db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false) 312 if err != nil { 313 b.Fatalf("error opening database at %v: %v", dir, err) 314 } 315 chain, err := NewBlockChain(db, &cacheConfig, genesis, nil, ethash.NewFaker(), vm.Config{}, nil, nil) 316 if err != nil { 317 b.Fatalf("error creating chain: %v", err) 318 } 319 320 for n := uint64(0); n < count; n++ { 321 header := chain.GetHeaderByNumber(n) 322 if full { 323 hash := header.Hash() 324 rawdb.ReadBody(db, hash, n) 325 rawdb.ReadReceipts(db, hash, n, header.Time, chain.Config()) 326 } 327 } 328 chain.Stop() 329 db.Close() 330 } 331 }