bench_test.go 8.8 KB

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