github.com/Finschia/finschia-sdk@v0.49.1/x/bank/bench_test.go (about)

     1  package bank_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	ocabci "github.com/Finschia/ostracon/abci/types"
     7  	"github.com/stretchr/testify/require"
     8  	abci "github.com/tendermint/tendermint/abci/types"
     9  	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
    10  
    11  	"github.com/Finschia/finschia-sdk/simapp"
    12  	simappparams "github.com/Finschia/finschia-sdk/simapp/params"
    13  	sdk "github.com/Finschia/finschia-sdk/types"
    14  	authtypes "github.com/Finschia/finschia-sdk/x/auth/types"
    15  	stakingtypes "github.com/Finschia/finschia-sdk/x/staking/types"
    16  )
    17  
    18  var moduleAccAddr = authtypes.NewModuleAddress(stakingtypes.BondedPoolName)
    19  
    20  func BenchmarkOneBankSendTxPerBlock(b *testing.B) {
    21  	b.ReportAllocs()
    22  	// Add an account at genesis
    23  	acc := authtypes.BaseAccount{
    24  		Address: addr1.String(),
    25  	}
    26  
    27  	// construct genesis state
    28  	genAccs := []authtypes.GenesisAccount{&acc}
    29  	benchmarkApp := simapp.SetupWithGenesisAccounts(genAccs)
    30  	ctx := benchmarkApp.BaseApp.NewContext(false, tmproto.Header{})
    31  
    32  	// some value conceivably higher than the benchmarks would ever go
    33  	require.NoError(b, simapp.FundAccount(benchmarkApp, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000))))
    34  
    35  	benchmarkApp.Commit()
    36  	txGen := simappparams.MakeTestEncodingConfig().TxConfig
    37  
    38  	// Precompute all txs
    39  	txs, err := simapp.GenSequenceOfTxs(txGen, []sdk.Msg{sendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
    40  	require.NoError(b, err)
    41  	b.ResetTimer()
    42  
    43  	height := int64(3)
    44  
    45  	// Run this with a profiler, so its easy to distinguish what time comes from
    46  	// Committing, and what time comes from Check/Deliver Tx.
    47  	for i := 0; i < b.N; i++ {
    48  		benchmarkApp.BeginBlock(ocabci.RequestBeginBlock{Header: tmproto.Header{Height: height}})
    49  		_, err := benchmarkApp.Check(txGen.TxEncoder(), txs[i])
    50  		if err != nil {
    51  			panic("something is broken in checking transaction")
    52  		}
    53  
    54  		_, _, err = benchmarkApp.Deliver(txGen.TxEncoder(), txs[i])
    55  		require.NoError(b, err)
    56  		benchmarkApp.EndBlock(abci.RequestEndBlock{Height: height})
    57  		benchmarkApp.Commit()
    58  		height++
    59  	}
    60  }
    61  
    62  func BenchmarkOneBankMultiSendTxPerBlock(b *testing.B) {
    63  	b.ReportAllocs()
    64  	// Add an account at genesis
    65  	acc := authtypes.BaseAccount{
    66  		Address: addr1.String(),
    67  	}
    68  
    69  	// Construct genesis state
    70  	genAccs := []authtypes.GenesisAccount{&acc}
    71  	benchmarkApp := simapp.SetupWithGenesisAccounts(genAccs)
    72  	ctx := benchmarkApp.BaseApp.NewContext(false, tmproto.Header{})
    73  
    74  	// some value conceivably higher than the benchmarks would ever go
    75  	require.NoError(b, simapp.FundAccount(benchmarkApp, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 100000000000))))
    76  
    77  	benchmarkApp.Commit()
    78  	txGen := simappparams.MakeTestEncodingConfig().TxConfig
    79  
    80  	// Precompute all txs
    81  	txs, err := simapp.GenSequenceOfTxs(txGen, []sdk.Msg{multiSendMsg1}, []uint64{0}, []uint64{uint64(0)}, b.N, priv1)
    82  	require.NoError(b, err)
    83  	b.ResetTimer()
    84  
    85  	height := int64(3)
    86  
    87  	// Run this with a profiler, so its easy to distinguish what time comes from
    88  	// Committing, and what time comes from Check/Deliver Tx.
    89  	for i := 0; i < b.N; i++ {
    90  		benchmarkApp.BeginBlock(ocabci.RequestBeginBlock{Header: tmproto.Header{Height: height}})
    91  		_, err := benchmarkApp.Check(txGen.TxEncoder(), txs[i])
    92  		if err != nil {
    93  			panic("something is broken in checking transaction")
    94  		}
    95  
    96  		_, _, err = benchmarkApp.Deliver(txGen.TxEncoder(), txs[i])
    97  		require.NoError(b, err)
    98  		benchmarkApp.EndBlock(abci.RequestEndBlock{Height: height})
    99  		benchmarkApp.Commit()
   100  		height++
   101  	}
   102  }