Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/staking: add ValidateGenesis benchmark #8746

Merged
merged 1 commit into from
Mar 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions x/staking/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package staking_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

func BenchmarkValidateGenesis10Validators(b *testing.B) {
benchmarkValidateGenesis(b, 10)
}

func BenchmarkValidateGenesis100Validators(b *testing.B) {
benchmarkValidateGenesis(b, 100)
}

func BenchmarkValidateGenesis400Validators(b *testing.B) {
benchmarkValidateGenesis(b, 400)
}

func benchmarkValidateGenesis(b *testing.B, n int) {
b.ReportAllocs()

validators := make([]types.Validator, 0, n)
addressL, pubKeyL := makeRandomAddressesAndPublicKeys(n)
for i := 0; i < n; i++ {
addr, pubKey := addressL[i], pubKeyL[i]
validator := teststaking.NewValidator(b, addr, pubKey)
ni := int64(i + 1)
validator.Tokens = sdk.NewInt(ni)
validator.DelegatorShares = sdk.NewDec(ni)
validators = append(validators, validator)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
genesisState := types.DefaultGenesisState()
genesisState.Validators = validators
if err := staking.ValidateGenesis(genesisState); err != nil {
b.Fatal(err)
}
}
}

func makeRandomAddressesAndPublicKeys(n int) (accL []sdk.ValAddress, pkL []*ed25519.PubKey) {
for i := 0; i < n; i++ {
pk := ed25519.GenPrivKey().PubKey().(*ed25519.PubKey)
pkL = append(pkL, pk)
accL = append(accL, sdk.ValAddress(pk.Address()))
}
return accL, pkL
}