From a6c05c95622ec0ba437876f952e29b95ee5f7635 Mon Sep 17 00:00:00 2001 From: Lazar Date: Wed, 11 Dec 2024 11:52:36 +0100 Subject: [PATCH] gradual measure --- finality-provider/service/fp_instance.go | 80 +++++++++++++++++++ finality-provider/service/fp_instance_test.go | 11 ++- 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/finality-provider/service/fp_instance.go b/finality-provider/service/fp_instance.go index 1c688f4a..2f557603 100644 --- a/finality-provider/service/fp_instance.go +++ b/finality-provider/service/fp_instance.go @@ -1016,3 +1016,83 @@ func (fp *FinalityProviderInstance) GetFinalityProviderSlashedOrJailedWithRetry( return slashed, jailed, nil } + +// CommitPubRandTiming - helper struct used to capture times for benchmark +type CommitPubRandTiming struct { + GetPubRandListTime time.Duration + AddPubRandProofListTime time.Duration + CommitPubRandListTime time.Duration +} + +// HelperCommitPubRand used for benchmark +func (fp *FinalityProviderInstance) HelperCommitPubRand(tipHeight uint64) (*types.TxResponse, *CommitPubRandTiming, error) { + lastCommittedHeight, err := fp.GetLastCommittedHeight() + if err != nil { + return nil, nil, err + } + + var startHeight uint64 + switch { + case lastCommittedHeight == uint64(0): + // the finality-provider has never submitted public rand before + startHeight = tipHeight + 1 + case lastCommittedHeight < uint64(fp.cfg.MinRandHeightGap)+tipHeight: + // (should not use subtraction because they are in the type of uint64) + // we are running out of the randomness + startHeight = lastCommittedHeight + 1 + default: + fp.logger.Debug( + "the finality-provider has sufficient public randomness, skip committing more", + zap.String("pk", fp.GetBtcPkHex()), + zap.Uint64("block_height", tipHeight), + zap.Uint64("last_committed_height", lastCommittedHeight), + ) + return nil, nil, nil + } + + return fp.commitPubRandPairsWithTiming(startHeight) +} + +func (fp *FinalityProviderInstance) commitPubRandPairsWithTiming(startHeight uint64) (*types.TxResponse, *CommitPubRandTiming, error) { + timing := &CommitPubRandTiming{} + + activationBlkHeight, err := fp.cc.QueryFinalityActivationBlockHeight() + if err != nil { + return nil, timing, err + } + + startHeight = max(startHeight, activationBlkHeight) + + // Measure getPubRandList + pubRandListStart := time.Now() + pubRandList, err := fp.getPubRandList(startHeight, fp.cfg.NumPubRand) + if err != nil { + return nil, timing, fmt.Errorf("failed to generate randomness: %w", err) + } + timing.GetPubRandListTime = time.Since(pubRandListStart) + + numPubRand := uint64(len(pubRandList)) + commitment, proofList := types.GetPubRandCommitAndProofs(pubRandList) + + // Measure addPubRandProofList + addProofStart := time.Now() + if err := fp.pubRandState.addPubRandProofList(pubRandList, proofList); err != nil { + return nil, timing, fmt.Errorf("failed to save public randomness to DB: %w", err) + } + timing.AddPubRandProofListTime = time.Since(addProofStart) + + // Measure CommitPubRandList + commitListStart := time.Now() + schnorrSig, err := fp.signPubRandCommit(startHeight, numPubRand, commitment) + if err != nil { + return nil, timing, fmt.Errorf("failed to sign the Schnorr signature: %w", err) + } + + res, err := fp.cc.CommitPubRandList(fp.GetBtcPk(), startHeight, numPubRand, commitment, schnorrSig) + if err != nil { + return nil, timing, fmt.Errorf("failed to commit public randomness to the consumer chain: %w", err) + } + timing.CommitPubRandListTime = time.Since(commitListStart) + + return res, timing, nil +} diff --git a/finality-provider/service/fp_instance_test.go b/finality-provider/service/fp_instance_test.go index da3a3e25..468d2fba 100644 --- a/finality-provider/service/fp_instance_test.go +++ b/finality-provider/service/fp_instance_test.go @@ -249,6 +249,7 @@ func setupBenchmarkEnvironment(t *testing.T, seed int64, numPubRand uint32) (*ty return startingBlock, fpIns, cleanUp } + func BenchmarkCommitPubRand(b *testing.B) { for _, numPubRand := range []uint32{10, 50, 100, 200, 500, 1000, 5000, 10000, 25000, 50000, 75000, 100000} { b.Run(fmt.Sprintf("numPubRand=%d", numPubRand), func(b *testing.B) { @@ -259,8 +260,9 @@ func BenchmarkCommitPubRand(b *testing.B) { // exclude setup time b.ResetTimer() + var totalTiming service.CommitPubRandTiming for i := 0; i < b.N; i++ { - res, err := fpIns.CommitPubRand(startingBlock.Height) + res, timing, err := fpIns.HelperCommitPubRand(startingBlock.Height) if err != nil { b.Fatalf("unexpected error: %v", err) } @@ -268,7 +270,14 @@ func BenchmarkCommitPubRand(b *testing.B) { if res == nil { b.Fatalf("unexpected result") } + // Accumulate timings for averaging + totalTiming.GetPubRandListTime += timing.GetPubRandListTime + totalTiming.AddPubRandProofListTime += timing.AddPubRandProofListTime + totalTiming.CommitPubRandListTime += timing.CommitPubRandListTime } + b.ReportMetric(float64(totalTiming.GetPubRandListTime.Nanoseconds())/float64(b.N), "ns/GetPubRandList") + b.ReportMetric(float64(totalTiming.AddPubRandProofListTime.Nanoseconds())/float64(b.N), "ns/AddPubRandProofList") + b.ReportMetric(float64(totalTiming.CommitPubRandListTime.Nanoseconds())/float64(b.N), "ns/CommitPubRandList") }) } }