bench_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "crypto/ecdsa"
  19. "io/ioutil"
  20. "math/big"
  21. "os"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. "github.com/ethereum/go-ethereum/ethdb"
  27. "github.com/ethereum/go-ethereum/event"
  28. "github.com/ethereum/go-ethereum/params"
  29. )
  30. func BenchmarkInsertChain_empty_memdb(b *testing.B) {
  31. benchInsertChain(b, false, nil)
  32. }
  33. func BenchmarkInsertChain_empty_diskdb(b *testing.B) {
  34. benchInsertChain(b, true, nil)
  35. }
  36. func BenchmarkInsertChain_valueTx_memdb(b *testing.B) {
  37. benchInsertChain(b, false, genValueTx(0))
  38. }
  39. func BenchmarkInsertChain_valueTx_diskdb(b *testing.B) {
  40. benchInsertChain(b, true, genValueTx(0))
  41. }
  42. func BenchmarkInsertChain_valueTx_100kB_memdb(b *testing.B) {
  43. benchInsertChain(b, false, genValueTx(100*1024))
  44. }
  45. func BenchmarkInsertChain_valueTx_100kB_diskdb(b *testing.B) {
  46. benchInsertChain(b, true, genValueTx(100*1024))
  47. }
  48. func BenchmarkInsertChain_uncles_memdb(b *testing.B) {
  49. benchInsertChain(b, false, genUncles)
  50. }
  51. func BenchmarkInsertChain_uncles_diskdb(b *testing.B) {
  52. benchInsertChain(b, true, genUncles)
  53. }
  54. func BenchmarkInsertChain_ring200_memdb(b *testing.B) {
  55. benchInsertChain(b, false, genTxRing(200))
  56. }
  57. func BenchmarkInsertChain_ring200_diskdb(b *testing.B) {
  58. benchInsertChain(b, true, genTxRing(200))
  59. }
  60. func BenchmarkInsertChain_ring1000_memdb(b *testing.B) {
  61. benchInsertChain(b, false, genTxRing(1000))
  62. }
  63. func BenchmarkInsertChain_ring1000_diskdb(b *testing.B) {
  64. benchInsertChain(b, true, genTxRing(1000))
  65. }
  66. var (
  67. // This is the content of the genesis block used by the benchmarks.
  68. benchRootKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  69. benchRootAddr = crypto.PubkeyToAddress(benchRootKey.PublicKey)
  70. benchRootFunds = common.BigPow(2, 100)
  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)
  80. tx, _ := types.NewTransaction(gen.TxNonce(benchRootAddr), toaddr, big.NewInt(1), gas, nil, data).SignECDSA(benchRootKey)
  81. gen.AddTx(tx)
  82. }
  83. }
  84. var (
  85. ringKeys = make([]*ecdsa.PrivateKey, 1000)
  86. ringAddrs = make([]common.Address, len(ringKeys))
  87. )
  88. func init() {
  89. ringKeys[0] = benchRootKey
  90. ringAddrs[0] = benchRootAddr
  91. for i := 1; i < len(ringKeys); i++ {
  92. ringKeys[i], _ = crypto.GenerateKey()
  93. ringAddrs[i] = crypto.PubkeyToAddress(ringKeys[i].PublicKey)
  94. }
  95. }
  96. // genTxRing returns a block generator that sends ether in a ring
  97. // among n accounts. This is creates n entries in the state database
  98. // and fills the blocks with many small transactions.
  99. func genTxRing(naccounts int) func(int, *BlockGen) {
  100. from := 0
  101. return func(i int, gen *BlockGen) {
  102. gas := CalcGasLimit(gen.PrevBlock(i - 1))
  103. for {
  104. gas.Sub(gas, params.TxGas)
  105. if gas.Cmp(params.TxGas) < 0 {
  106. break
  107. }
  108. to := (from + 1) % naccounts
  109. tx := types.NewTransaction(
  110. gen.TxNonce(ringAddrs[from]),
  111. ringAddrs[to],
  112. benchRootFunds,
  113. params.TxGas,
  114. nil,
  115. nil,
  116. )
  117. tx, _ = tx.SignECDSA(ringKeys[from])
  118. gen.AddTx(tx)
  119. from = to
  120. }
  121. }
  122. }
  123. // genUncles generates blocks with two uncle headers.
  124. func genUncles(i int, gen *BlockGen) {
  125. if i >= 6 {
  126. b2 := gen.PrevBlock(i - 6).Header()
  127. b2.Extra = []byte("foo")
  128. gen.AddUncle(b2)
  129. b3 := gen.PrevBlock(i - 6).Header()
  130. b3.Extra = []byte("bar")
  131. gen.AddUncle(b3)
  132. }
  133. }
  134. func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
  135. // Create the database in memory or in a temporary directory.
  136. var db common.Database
  137. if !disk {
  138. db, _ = ethdb.NewMemDatabase()
  139. } else {
  140. dir, err := ioutil.TempDir("", "eth-core-bench")
  141. if err != nil {
  142. b.Fatalf("cannot create temporary directory: %v", err)
  143. }
  144. defer os.RemoveAll(dir)
  145. db, err = ethdb.NewLDBDatabase(dir)
  146. if err != nil {
  147. b.Fatalf("cannot create temporary database: %v", err)
  148. }
  149. defer db.Close()
  150. }
  151. // Generate a chain of b.N blocks using the supplied block
  152. // generator function.
  153. genesis := GenesisBlockForTesting(db, benchRootAddr, benchRootFunds)
  154. chain := GenerateChain(genesis, db, b.N, gen)
  155. // Time the insertion of the new chain.
  156. // State and blocks are stored in the same DB.
  157. evmux := new(event.TypeMux)
  158. chainman, _ := NewChainManager(genesis, db, db, db, FakePow{}, evmux)
  159. chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux))
  160. defer chainman.Stop()
  161. b.ReportAllocs()
  162. b.ResetTimer()
  163. if i, err := chainman.InsertChain(chain); err != nil {
  164. b.Fatalf("insert error (block %d): %v\n", i, err)
  165. }
  166. }