bench_test.go 8.7 KB

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