bench_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/math"
  23. "github.com/ethereum/go-ethereum/consensus/ethash"
  24. "github.com/ethereum/go-ethereum/core/rawdb"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/core/vm"
  27. "github.com/ethereum/go-ethereum/crypto"
  28. "github.com/ethereum/go-ethereum/ethdb"
  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 = math.BigPow(2, 200)
  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, nil, false, false, false)
  81. signer := types.MakeSigner(gen.config, big.NewInt(int64(i)))
  82. gasPrice := big.NewInt(0)
  83. if gen.header.BaseFee != nil {
  84. gasPrice = gen.header.BaseFee
  85. }
  86. tx, _ := types.SignNewTx(benchRootKey, signer, &types.LegacyTx{
  87. Nonce: gen.TxNonce(benchRootAddr),
  88. To: &toaddr,
  89. Value: big.NewInt(1),
  90. Gas: gas,
  91. Data: data,
  92. GasPrice: gasPrice,
  93. })
  94. gen.AddTx(tx)
  95. }
  96. }
  97. var (
  98. ringKeys = make([]*ecdsa.PrivateKey, 1000)
  99. ringAddrs = make([]common.Address, len(ringKeys))
  100. )
  101. func init() {
  102. ringKeys[0] = benchRootKey
  103. ringAddrs[0] = benchRootAddr
  104. for i := 1; i < len(ringKeys); i++ {
  105. ringKeys[i], _ = crypto.GenerateKey()
  106. ringAddrs[i] = crypto.PubkeyToAddress(ringKeys[i].PublicKey)
  107. }
  108. }
  109. // genTxRing returns a block generator that sends ether in a ring
  110. // among n accounts. This is creates n entries in the state database
  111. // and fills the blocks with many small transactions.
  112. func genTxRing(naccounts int) func(int, *BlockGen) {
  113. from := 0
  114. availableFunds := new(big.Int).Set(benchRootFunds)
  115. return func(i int, gen *BlockGen) {
  116. block := gen.PrevBlock(i - 1)
  117. gas := block.GasLimit()
  118. gasPrice := big.NewInt(0)
  119. if gen.header.BaseFee != nil {
  120. gasPrice = gen.header.BaseFee
  121. }
  122. signer := types.MakeSigner(gen.config, big.NewInt(int64(i)))
  123. for {
  124. gas -= params.TxGas
  125. if gas < params.TxGas {
  126. break
  127. }
  128. to := (from + 1) % naccounts
  129. burn := new(big.Int).SetUint64(params.TxGas)
  130. burn.Mul(burn, gen.header.BaseFee)
  131. availableFunds.Sub(availableFunds, burn)
  132. if availableFunds.Cmp(big.NewInt(1)) < 0 {
  133. panic("not enough funds")
  134. }
  135. tx, err := types.SignNewTx(ringKeys[from], signer,
  136. &types.LegacyTx{
  137. Nonce: gen.TxNonce(ringAddrs[from]),
  138. To: &ringAddrs[to],
  139. Value: availableFunds,
  140. Gas: params.TxGas,
  141. GasPrice: gasPrice,
  142. })
  143. if err != nil {
  144. panic(err)
  145. }
  146. gen.AddTx(tx)
  147. from = to
  148. }
  149. }
  150. }
  151. // genUncles generates blocks with two uncle headers.
  152. func genUncles(i int, gen *BlockGen) {
  153. if i >= 7 {
  154. b2 := gen.PrevBlock(i - 6).Header()
  155. b2.Extra = []byte("foo")
  156. gen.AddUncle(b2)
  157. b3 := gen.PrevBlock(i - 6).Header()
  158. b3.Extra = []byte("bar")
  159. gen.AddUncle(b3)
  160. }
  161. }
  162. func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
  163. // Create the database in memory or in a temporary directory.
  164. var db ethdb.Database
  165. var err error
  166. if !disk {
  167. db = rawdb.NewMemoryDatabase()
  168. } else {
  169. dir := b.TempDir()
  170. db, err = rawdb.NewLevelDBDatabase(dir, 128, 128, "", false)
  171. if err != nil {
  172. b.Fatalf("cannot create temporary database: %v", err)
  173. }
  174. defer db.Close()
  175. }
  176. // Generate a chain of b.N blocks using the supplied block
  177. // generator function.
  178. gspec := Genesis{
  179. Config: params.TestChainConfig,
  180. Alloc: GenesisAlloc{benchRootAddr: {Balance: benchRootFunds}},
  181. }
  182. genesis := gspec.MustCommit(db)
  183. chain, _ := GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, b.N, gen)
  184. // Time the insertion of the new chain.
  185. // State and blocks are stored in the same DB.
  186. chainman, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
  187. defer chainman.Stop()
  188. b.ReportAllocs()
  189. b.ResetTimer()
  190. if i, err := chainman.InsertChain(chain); err != nil {
  191. b.Fatalf("insert error (block %d): %v\n", i, err)
  192. }
  193. }
  194. func BenchmarkChainRead_header_10k(b *testing.B) {
  195. benchReadChain(b, false, 10000)
  196. }
  197. func BenchmarkChainRead_full_10k(b *testing.B) {
  198. benchReadChain(b, true, 10000)
  199. }
  200. func BenchmarkChainRead_header_100k(b *testing.B) {
  201. benchReadChain(b, false, 100000)
  202. }
  203. func BenchmarkChainRead_full_100k(b *testing.B) {
  204. benchReadChain(b, true, 100000)
  205. }
  206. func BenchmarkChainRead_header_500k(b *testing.B) {
  207. benchReadChain(b, false, 500000)
  208. }
  209. func BenchmarkChainRead_full_500k(b *testing.B) {
  210. benchReadChain(b, true, 500000)
  211. }
  212. func BenchmarkChainWrite_header_10k(b *testing.B) {
  213. benchWriteChain(b, false, 10000)
  214. }
  215. func BenchmarkChainWrite_full_10k(b *testing.B) {
  216. benchWriteChain(b, true, 10000)
  217. }
  218. func BenchmarkChainWrite_header_100k(b *testing.B) {
  219. benchWriteChain(b, false, 100000)
  220. }
  221. func BenchmarkChainWrite_full_100k(b *testing.B) {
  222. benchWriteChain(b, true, 100000)
  223. }
  224. func BenchmarkChainWrite_header_500k(b *testing.B) {
  225. benchWriteChain(b, false, 500000)
  226. }
  227. func BenchmarkChainWrite_full_500k(b *testing.B) {
  228. benchWriteChain(b, true, 500000)
  229. }
  230. // makeChainForBench writes a given number of headers or empty blocks/receipts
  231. // into a database.
  232. func makeChainForBench(db ethdb.Database, full bool, count uint64) {
  233. var hash common.Hash
  234. for n := uint64(0); n < count; n++ {
  235. header := &types.Header{
  236. Coinbase: common.Address{},
  237. Number: big.NewInt(int64(n)),
  238. ParentHash: hash,
  239. Difficulty: big.NewInt(1),
  240. UncleHash: types.EmptyUncleHash,
  241. TxHash: types.EmptyRootHash,
  242. ReceiptHash: types.EmptyRootHash,
  243. }
  244. hash = header.Hash()
  245. rawdb.WriteHeader(db, header)
  246. rawdb.WriteCanonicalHash(db, hash, n)
  247. rawdb.WriteTd(db, hash, n, big.NewInt(int64(n+1)))
  248. if full || n == 0 {
  249. block := types.NewBlockWithHeader(header)
  250. rawdb.WriteBody(db, hash, n, block.Body())
  251. rawdb.WriteReceipts(db, hash, n, nil)
  252. rawdb.WriteHeadBlockHash(db, hash)
  253. }
  254. }
  255. }
  256. func benchWriteChain(b *testing.B, full bool, count uint64) {
  257. for i := 0; i < b.N; i++ {
  258. dir := b.TempDir()
  259. db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false)
  260. if err != nil {
  261. b.Fatalf("error opening database at %v: %v", dir, err)
  262. }
  263. makeChainForBench(db, full, count)
  264. db.Close()
  265. }
  266. }
  267. func benchReadChain(b *testing.B, full bool, count uint64) {
  268. dir := b.TempDir()
  269. db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false)
  270. if err != nil {
  271. b.Fatalf("error opening database at %v: %v", dir, err)
  272. }
  273. makeChainForBench(db, full, count)
  274. db.Close()
  275. cacheConfig := *defaultCacheConfig
  276. cacheConfig.TrieDirtyDisabled = true
  277. b.ReportAllocs()
  278. b.ResetTimer()
  279. for i := 0; i < b.N; i++ {
  280. db, err := rawdb.NewLevelDBDatabase(dir, 128, 1024, "", false)
  281. if err != nil {
  282. b.Fatalf("error opening database at %v: %v", dir, err)
  283. }
  284. chain, err := NewBlockChain(db, &cacheConfig, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil, nil)
  285. if err != nil {
  286. b.Fatalf("error creating chain: %v", err)
  287. }
  288. for n := uint64(0); n < count; n++ {
  289. header := chain.GetHeaderByNumber(n)
  290. if full {
  291. hash := header.Hash()
  292. rawdb.ReadBody(db, hash, n)
  293. rawdb.ReadReceipts(db, hash, n, chain.Config())
  294. }
  295. }
  296. chain.Stop()
  297. db.Close()
  298. }
  299. }