Skip to content

Commit

Permalink
x/staking: add ValidateGenesis benchmark (cosmos#8746)
Browse files Browse the repository at this point in the history
This benchmark examines how ValidateGenesis behaves.
In a follow-up issue, I'll show how it reveals an inefficiency
that'll affect trying to load repeatedly retrieve Validators'
ConsAddress values.

Updates cosmos#8744
  • Loading branch information
odeke-em authored Mar 2, 2021
1 parent 010eeef commit 585ffd6
Showing 1 changed file with 56 additions and 0 deletions.
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
}

0 comments on commit 585ffd6

Please sign in to comment.