Skip to content

Commit 9a90cf4

Browse files
authored
Add benchmark for gRPC GetValidatorSet (#1326)
1 parent 5354562 commit 9a90cf4

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

snow/validators/gvalidators/validator_state_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package gvalidators
66
import (
77
"context"
88
"errors"
9+
"fmt"
910
"testing"
1011

1112
"github.com/golang/mock/gomock"
@@ -181,3 +182,52 @@ func TestGetValidatorSet(t *testing.T) {
181182
_, err = state.client.GetValidatorSet(context.Background(), height, subnetID)
182183
require.Error(err)
183184
}
185+
186+
// BenchmarkGetValidatorSet measures the time it takes complete a gRPC client
187+
// request based on a mocked validator set.
188+
func BenchmarkGetValidatorSet(b *testing.B) {
189+
for _, size := range []int{1, 16, 32, 1024, 2048} {
190+
vs := setupValidatorSet(b, size)
191+
b.Run(fmt.Sprintf("get_validator_set_%d_validators", size), func(b *testing.B) {
192+
benchmarkGetValidatorSet(b, vs)
193+
})
194+
}
195+
}
196+
197+
func benchmarkGetValidatorSet(b *testing.B, vs map[ids.NodeID]*validators.GetValidatorOutput) {
198+
require := require.New(b)
199+
ctrl := gomock.NewController(b)
200+
state := setupState(b, ctrl)
201+
defer func() {
202+
ctrl.Finish()
203+
state.closeFn()
204+
}()
205+
206+
height := uint64(1337)
207+
subnetID := ids.GenerateTestID()
208+
state.server.EXPECT().GetValidatorSet(gomock.Any(), height, subnetID).Return(vs, nil).AnyTimes()
209+
b.ResetTimer()
210+
for i := 0; i < b.N; i++ {
211+
_, err := state.client.GetValidatorSet(context.Background(), height, subnetID)
212+
require.NoError(err)
213+
}
214+
b.StopTimer()
215+
}
216+
217+
func setupValidatorSet(b *testing.B, size int) map[ids.NodeID]*validators.GetValidatorOutput {
218+
b.Helper()
219+
220+
set := make(map[ids.NodeID]*validators.GetValidatorOutput, size)
221+
sk, err := bls.NewSecretKey()
222+
require.NoError(b, err)
223+
pk := bls.PublicFromSecretKey(sk)
224+
for i := 0; i < size; i++ {
225+
id := ids.GenerateTestNodeID()
226+
set[id] = &validators.GetValidatorOutput{
227+
NodeID: id,
228+
PublicKey: pk,
229+
Weight: uint64(i),
230+
}
231+
}
232+
return set
233+
}

0 commit comments

Comments
 (0)