github.com/klaytn/klaytn@v1.12.1/tests/tx_root_test.go (about)

     1  // Copyright 2018 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn 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 klaytn 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 klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package tests
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/klaytn/klaytn/blockchain/types"
    26  	"github.com/klaytn/klaytn/blockchain/types/derivesha"
    27  	"github.com/klaytn/klaytn/common/profile"
    28  	"github.com/klaytn/klaytn/crypto"
    29  )
    30  
    31  func BenchmarkDeriveSha(b *testing.B) {
    32  	funcs := map[string]derivesha.IDeriveSha{
    33  		"Orig":   derivesha.DeriveShaOrig{},
    34  		"Simple": derivesha.DeriveShaSimple{},
    35  		"Concat": derivesha.DeriveShaConcat{},
    36  	}
    37  
    38  	NTS := []int{1000}
    39  
    40  	for k, f := range funcs {
    41  		for _, nt := range NTS {
    42  			testName := fmt.Sprintf("%s,%d", k, nt)
    43  			b.Run(testName, func(b *testing.B) {
    44  				benchDeriveSha(b, nt, 4, f)
    45  			})
    46  		}
    47  	}
    48  }
    49  
    50  func BenchmarkDeriveShaSingleAccount(b *testing.B) {
    51  	txs, err := genTxs(4000)
    52  	if err != nil {
    53  		b.Fatal(err)
    54  	}
    55  
    56  	funcs := map[string]derivesha.IDeriveSha{
    57  		"Orig":   derivesha.DeriveShaOrig{},
    58  		"Simple": derivesha.DeriveShaSimple{},
    59  		"Concat": derivesha.DeriveShaConcat{},
    60  	}
    61  
    62  	for k, f := range funcs {
    63  		b.Run(k, func(b *testing.B) {
    64  			for i := 0; i < b.N; i++ {
    65  				f.DeriveSha(txs)
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func BenchmarkDeriveShaOrig(b *testing.B) {
    72  	b.Run("1000test-stackTrie", func(b *testing.B) {
    73  		benchDeriveSha(b, 1000, 4, derivesha.DeriveShaOrig{})
    74  	})
    75  }
    76  
    77  func benchDeriveSha(b *testing.B, numTransactions, numValidators int, sha derivesha.IDeriveSha) {
    78  	// Initialize blockchain
    79  	start := time.Now()
    80  	maxAccounts := numTransactions * 2
    81  	bcdata, err := NewBCData(maxAccounts, numValidators)
    82  	if err != nil {
    83  		b.Fatal(err)
    84  	}
    85  	profile.Prof.Profile("main_init_blockchain", time.Now().Sub(start))
    86  	defer bcdata.Shutdown()
    87  
    88  	// Initialize address-balance map for verification
    89  	start = time.Now()
    90  	accountMap := NewAccountMap()
    91  	if err := accountMap.Initialize(bcdata); err != nil {
    92  		b.Fatal(err)
    93  	}
    94  	profile.Prof.Profile("main_init_accountMap", time.Now().Sub(start))
    95  
    96  	signer := types.MakeSigner(bcdata.bc.Config(), bcdata.bc.CurrentBlock().Number())
    97  
    98  	txs, err := makeTransactions(accountMap,
    99  		bcdata.addrs[0:numTransactions], bcdata.privKeys[0:numTransactions],
   100  		signer, bcdata.addrs[numTransactions:numTransactions*2], nil, 0, false)
   101  	if err != nil {
   102  		b.Fatal(err)
   103  	}
   104  
   105  	b.ResetTimer()
   106  	for i := 0; i < b.N; i++ {
   107  		sha.DeriveSha(txs)
   108  	}
   109  }
   110  
   111  func genTxs(num uint64) (types.Transactions, error) {
   112  	key, err := crypto.HexToECDSA("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef")
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	addr := crypto.PubkeyToAddress(key.PublicKey)
   117  	newTx := func(i uint64) (*types.Transaction, error) {
   118  		signer := types.NewEIP155Signer(big.NewInt(18))
   119  		utx := types.NewTransaction(i, addr, new(big.Int), 0, new(big.Int).SetUint64(10000000), nil)
   120  		tx, err := types.SignTx(utx, signer, key)
   121  		return tx, err
   122  	}
   123  	var txs types.Transactions
   124  	for i := uint64(0); i < num; i++ {
   125  		tx, err := newTx(i)
   126  		if err != nil {
   127  			return nil, err
   128  		}
   129  		txs = append(txs, tx)
   130  	}
   131  	return txs, nil
   132  }