github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/bench_test.go (about)

     1  package staking_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"cosmossdk.io/math"
     7  
     8  	"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
     9  	sdk "github.com/cosmos/cosmos-sdk/types"
    10  	"github.com/cosmos/cosmos-sdk/x/staking"
    11  	"github.com/cosmos/cosmos-sdk/x/staking/testutil"
    12  	"github.com/cosmos/cosmos-sdk/x/staking/types"
    13  )
    14  
    15  func BenchmarkValidateGenesis10Validators(b *testing.B) {
    16  	benchmarkValidateGenesis(b, 10)
    17  }
    18  
    19  func BenchmarkValidateGenesis100Validators(b *testing.B) {
    20  	benchmarkValidateGenesis(b, 100)
    21  }
    22  
    23  func BenchmarkValidateGenesis400Validators(b *testing.B) {
    24  	benchmarkValidateGenesis(b, 400)
    25  }
    26  
    27  func benchmarkValidateGenesis(b *testing.B, n int) {
    28  	b.ReportAllocs()
    29  
    30  	validators := make([]types.Validator, 0, n)
    31  	addressL, pubKeyL := makeRandomAddressesAndPublicKeys(n)
    32  	for i := 0; i < n; i++ {
    33  		addr, pubKey := addressL[i], pubKeyL[i]
    34  		validator := testutil.NewValidator(b, addr, pubKey)
    35  		ni := int64(i + 1)
    36  		validator.Tokens = math.NewInt(ni)
    37  		validator.DelegatorShares = math.LegacyNewDec(ni)
    38  		validators = append(validators, validator)
    39  	}
    40  
    41  	b.ResetTimer()
    42  	for i := 0; i < b.N; i++ {
    43  		genesisState := types.DefaultGenesisState()
    44  		genesisState.Validators = validators
    45  		if err := staking.ValidateGenesis(genesisState); err != nil {
    46  			b.Fatal(err)
    47  		}
    48  	}
    49  }
    50  
    51  func makeRandomAddressesAndPublicKeys(n int) (accL []sdk.ValAddress, pkL []*ed25519.PubKey) {
    52  	for i := 0; i < n; i++ {
    53  		pk := ed25519.GenPrivKey().PubKey().(*ed25519.PubKey)
    54  		pkL = append(pkL, pk)
    55  		accL = append(accL, sdk.ValAddress(pk.Address()))
    56  	}
    57  	return accL, pkL
    58  }