Skip to content

Add benchmark for gRPC GetValidatorSet #1326

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

Merged
merged 3 commits into from
Apr 12, 2023
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
50 changes: 50 additions & 0 deletions snow/validators/gvalidators/validator_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package gvalidators
import (
"context"
"errors"
"fmt"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -181,3 +182,52 @@ func TestGetValidatorSet(t *testing.T) {
_, err = state.client.GetValidatorSet(context.Background(), height, subnetID)
require.Error(err)
}

// BenchmarkGetValidatorSet measures the time it takes complete a gRPC client
// request based on a mocked validator set.
func BenchmarkGetValidatorSet(b *testing.B) {
for _, size := range []int{1, 16, 32, 1024, 2048} {
vs := setupValidatorSet(b, size)
b.Run(fmt.Sprintf("get_validator_set_%d_validators", size), func(b *testing.B) {
benchmarkGetValidatorSet(b, vs)
})
}
}

func benchmarkGetValidatorSet(b *testing.B, vs map[ids.NodeID]*validators.GetValidatorOutput) {
require := require.New(b)
ctrl := gomock.NewController(b)
state := setupState(b, ctrl)
defer func() {
ctrl.Finish()
state.closeFn()
}()

height := uint64(1337)
subnetID := ids.GenerateTestID()
state.server.EXPECT().GetValidatorSet(gomock.Any(), height, subnetID).Return(vs, nil).AnyTimes()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := state.client.GetValidatorSet(context.Background(), height, subnetID)
require.NoError(err)
}
b.StopTimer()
}

func setupValidatorSet(b *testing.B, size int) map[ids.NodeID]*validators.GetValidatorOutput {
b.Helper()

set := make(map[ids.NodeID]*validators.GetValidatorOutput, size)
sk, err := bls.NewSecretKey()
require.NoError(b, err)
pk := bls.PublicFromSecretKey(sk)
for i := 0; i < size; i++ {
id := ids.GenerateTestNodeID()
set[id] = &validators.GetValidatorOutput{
NodeID: id,
PublicKey: pk,
Weight: uint64(i),
}
}
return set
}