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