bench_test.go 8.7 KB

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