github.com/Gessiux/neatchain@v1.3.1/chain/core/bench_test.go (about)

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