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

     1  // Copyright 2019 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  	"crypto/ecdsa"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/klaytn/klaytn/blockchain/types"
    25  	"github.com/klaytn/klaytn/common"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func BenchmarkTxHash(b *testing.B) {
    30  	testfns := []genTx{
    31  		genLegacyValueTransfer,
    32  		genNewValueTransfer,
    33  		genLegacySmartContractDeploy,
    34  		genNewSmartContractDeploy,
    35  		genLegacySmartContractExecution,
    36  		genNewSmartContractExecution,
    37  		genNewAccountUpdateAccountKeyPublic,
    38  		genNewFeeDelegatedValueTransfer,
    39  		genNewFeeDelegatedValueTransferWithRatio,
    40  		genNewCancel,
    41  	}
    42  
    43  	for _, fn := range testfns {
    44  		fnname := getFunctionName(fn)
    45  		fnname = fnname[strings.LastIndex(fnname, ".")+1:]
    46  		if strings.Contains(fnname, "New") {
    47  			benchName = "New/" + strings.Split(fnname, "New")[1]
    48  		} else {
    49  			benchName = "Legacy/" + strings.Split(fnname, "Legacy")[1]
    50  		}
    51  		b.Run(benchName, func(b *testing.B) {
    52  			benchmarkTxHash(b, fn)
    53  		})
    54  	}
    55  }
    56  
    57  func benchmarkTxHash(b *testing.B, genTx genTx) {
    58  	signer := types.LatestSignerForChainID(common.Big1)
    59  
    60  	// Initialize blockchain
    61  	bcdata, err := NewBCData(6, 4)
    62  	if err != nil {
    63  		b.Fatal(err)
    64  	}
    65  	defer bcdata.Shutdown()
    66  
    67  	// reservoir account
    68  	reservoir := &TestAccountType{
    69  		Addr:  *bcdata.addrs[0],
    70  		Keys:  []*ecdsa.PrivateKey{bcdata.privKeys[0]},
    71  		Nonce: uint64(0),
    72  	}
    73  
    74  	anon, err := createAnonymousAccount("ed580f5bd71a2ee4dae5cb43e331b7d0318596e561e6add7844271ed94156b20")
    75  	assert.Equal(b, nil, err)
    76  
    77  	tx := genTx(signer, reservoir, anon)
    78  
    79  	b.ResetTimer()
    80  	for i := 0; i < b.N; i++ {
    81  		signer.Hash(tx)
    82  	}
    83  }