From 1970db1c1019dba2560ba933d5ade60e30559c8e Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Tue, 25 Apr 2023 16:49:52 -0400 Subject: [PATCH 1/4] warp: add benchmark for GetCanonicalValidatorSet --- vms/platformvm/warp/validator_test.go | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/vms/platformvm/warp/validator_test.go b/vms/platformvm/warp/validator_test.go index ef8998ebed67..91bcb0cfe196 100644 --- a/vms/platformvm/warp/validator_test.go +++ b/vms/platformvm/warp/validator_test.go @@ -5,6 +5,7 @@ package warp import ( "context" + "fmt" "math" "testing" @@ -304,3 +305,48 @@ func TestSumWeight(t *testing.T) { }) } } + +func BenchmarkGetCanonicalValidatorSet(b *testing.B) { + pChainHeight := uint64(1) + subnetID := ids.GenerateTestID() + numNodes := 10_000 + getValidatorOutputs := make([]*validators.GetValidatorOutput, 0, numNodes) + for i := 0; i < numNodes; i++ { + nodeID := ids.GenerateTestNodeID() + blsPrivateKey, err := bls.NewSecretKey() + if err != nil { + b.Fatal(err) + } + blsPublicKey := bls.PublicFromSecretKey(blsPrivateKey) + getValidatorOutputs = append(getValidatorOutputs, &validators.GetValidatorOutput{ + NodeID: nodeID, + PublicKey: blsPublicKey, + Weight: 20, + }) + } + + getNValidatorsMap := func(n int) map[ids.NodeID]*validators.GetValidatorOutput { + validators := make(map[ids.NodeID]*validators.GetValidatorOutput) + for i := 0; i < n; i++ { + validator := getValidatorOutputs[i] + validators[validator.NodeID] = validator + } + return validators + } + + for _, size := range []int{0, 1, 10, 100, 1_000, 10_000} { + validatorsOutput := getNValidatorsMap(size) + validatorState := &validators.TestState{ + GetValidatorSetF: func(ctx context.Context, height uint64, subnetID ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error) { + return validatorsOutput, nil + }, + } + + b.Run(fmt.Sprintf("%d", size), func(b *testing.B) { + for i := 0; i < b.N; i++ { + _, _, err := GetCanonicalValidatorSet(context.Background(), validatorState, pChainHeight, subnetID) + require.NoError(b, err) + } + }) + } +} From d05a8afa3bd939f262c4da96b23f902dcdbfc087 Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Tue, 25 Apr 2023 16:56:45 -0400 Subject: [PATCH 2/4] Address comments --- vms/platformvm/warp/validator_test.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/vms/platformvm/warp/validator_test.go b/vms/platformvm/warp/validator_test.go index 91bcb0cfe196..3634e6c09d89 100644 --- a/vms/platformvm/warp/validator_test.go +++ b/vms/platformvm/warp/validator_test.go @@ -325,20 +325,15 @@ func BenchmarkGetCanonicalValidatorSet(b *testing.B) { }) } - getNValidatorsMap := func(n int) map[ids.NodeID]*validators.GetValidatorOutput { - validators := make(map[ids.NodeID]*validators.GetValidatorOutput) - for i := 0; i < n; i++ { + for _, size := range []int{0, 1, 10, 100, 1_000, 10_000} { + getValidatorsOutput := make(map[ids.NodeID]*validators.GetValidatorOutput) + for i := 0; i < size; i++ { validator := getValidatorOutputs[i] - validators[validator.NodeID] = validator + getValidatorsOutput[validator.NodeID] = validator } - return validators - } - - for _, size := range []int{0, 1, 10, 100, 1_000, 10_000} { - validatorsOutput := getNValidatorsMap(size) validatorState := &validators.TestState{ - GetValidatorSetF: func(ctx context.Context, height uint64, subnetID ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error) { - return validatorsOutput, nil + GetValidatorSetF: func(_ context.Context, _ uint64, _ ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error) { + return getValidatorsOutput, nil }, } From 6d09a123fa920ed5c74f76561aa6365d96383eb7 Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Tue, 25 Apr 2023 17:02:41 -0400 Subject: [PATCH 3/4] Address PR comment --- vms/platformvm/warp/validator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vms/platformvm/warp/validator_test.go b/vms/platformvm/warp/validator_test.go index 3634e6c09d89..14b1239295ed 100644 --- a/vms/platformvm/warp/validator_test.go +++ b/vms/platformvm/warp/validator_test.go @@ -332,7 +332,7 @@ func BenchmarkGetCanonicalValidatorSet(b *testing.B) { getValidatorsOutput[validator.NodeID] = validator } validatorState := &validators.TestState{ - GetValidatorSetF: func(_ context.Context, _ uint64, _ ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error) { + GetValidatorSetF: func(context.Context, uint64, ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error) { return getValidatorsOutput, nil }, } From 83650718c4b8a16f18b21c44bb1539eb9213ddd7 Mon Sep 17 00:00:00 2001 From: Stephen Date: Tue, 25 Apr 2023 19:17:58 -0400 Subject: [PATCH 4/4] nit --- vms/platformvm/warp/validator_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/vms/platformvm/warp/validator_test.go b/vms/platformvm/warp/validator_test.go index 14b1239295ed..5bbdb0d942b6 100644 --- a/vms/platformvm/warp/validator_test.go +++ b/vms/platformvm/warp/validator_test.go @@ -314,9 +314,7 @@ func BenchmarkGetCanonicalValidatorSet(b *testing.B) { for i := 0; i < numNodes; i++ { nodeID := ids.GenerateTestNodeID() blsPrivateKey, err := bls.NewSecretKey() - if err != nil { - b.Fatal(err) - } + require.NoError(b, err) blsPublicKey := bls.PublicFromSecretKey(blsPrivateKey) getValidatorOutputs = append(getValidatorOutputs, &validators.GetValidatorOutput{ NodeID: nodeID,