Skip to content

Commit

Permalink
chore: gradual benchmark pub rand (#214)
Browse files Browse the repository at this point in the history
I had to introduce helper functions to get gradual timing of functions
inside `commitPubRandPairs`.

### Findings 

Seems that most of the time is spent at `GetPubRandList` following
`AddPubRandProofList`. As input (NumPubRand) grows we see an increase in
time.


----
Results:

NumPubRand | Iterations (b.N) | Time per Iteration (ns/op) | Time per
Iteration (ms) | AddPubRandProofList (ns / ms) | % AddPubRandProofList |
CommitPubRandList (ns / ms) | % CommitPubRandList | GetPubRandList (ns /
ms) | % GetPubRandList
-- | -- | -- | -- | -- | -- | -- | -- | -- | --
10 | 42 | 27,770,395 | 27.77 | 14,544,106 / 14.54 | 52.43% | 1,442,187 /
1.44 | 5.19% | 11,753,113 / 11.75 | 42.34%
50 | 16 | 68,078,846 | 68.08 | 15,242,174 / 15.24 | 22.40% | 1,242,602 /
1.24 | 1.83% | 51,516,203 / 51.52 | 75.69%
100 | 9 | 122,120,093 | 122.12 | 16,093,750 / 16.09 | 13.16% | 1,271,602
/ 1.27 | 1.04% | 104,604,153 / 104.60 | 85.62%
200 | 5 | 231,700,700 | 231.70 | 17,093,217 / 17.09 | 7.37% | 1,268,492
/ 1.27 | 0.55% | 213,090,417 / 213.09 | 92.08%
500 | 2 | 546,630,958 | 546.63 | 20,933,896 / 20.93 | 3.83% | 1,244,730
/ 1.24 | 0.23% | 523,769,854 / 523.77 | 95.52%
1,000 | 1 | 1,063,210,125 | 1,063.21 | 20,605,666 / 20.61 | 1.94% |
1,276,208 / 1.28 | 0.12% | 1,039,780,500 / 1,039.78 | 97.72%
5,000 | 1 | 5,235,733,292 | 5,235.73 | 37,567,250 / 37.57 | 0.72% |
1,250,916 / 1.25 | 0.02% | 5,193,632,875 / 5,193.63 | 99.15%
10,000 | 1 | 10,479,790,875 | 10,479.79 | 73,979,584 / 73.98 | 0.71% |
1,314,334 / 1.31 | 0.01% | 10,397,781,458 / 10,397.78 | 99.07%
25,000 | 1 | 26,012,446,417 | 26,012.45 | 245,269,750 / 245.27 | 0.94% |
1,380,083 / 1.38 | 0.01% | 25,750,781,708 / 25,750.78 | 99.06%
50,000 | 1 | 52,544,326,750 | 52,544.33 | 816,153,875 / 816.15 | 1.55% |
1,354,375 / 1.35 | 0.03% | 51,695,706,875 / 51,695.71 | 98.41%
75,000 | 1 | 79,272,072,959 | 79,272.07 | 1,696,296,750 / 1,696.30 |
2.14% | 1,275,250 / 1.28 | 0.02% | 77,517,351,000 / 77,517.35 | 97.67%
100,000 | 1 | 106,582,957,334 | 106,582.96 | 2,905,885,458 / 2,905.89 |
2.73% | 1,341,375 / 1.34 | 0.01% | 103,598,760,375 / 103,598.76 | 97.22%


----


[Closes](#192)
  • Loading branch information
Lazar955 authored Dec 11, 2024
1 parent 0c0b494 commit e305548
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* [#207](https://github.com/babylonlabs-io/finality-provider/pull/207) create finality provider from JSON file
* [#208](https://github.com/babylonlabs-io/finality-provider/pull/208) Remove sync fp status loop
* [#211](https://github.com/babylonlabs-io/finality-provider/pull/211) Clean up unused cmd
* [#214](https://github.com/babylonlabs-io/finality-provider/pull/214) Gradual benchmark

## v0.13.1

Expand Down
88 changes: 88 additions & 0 deletions finality-provider/service/benchmark_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package service

import (
"fmt"
"github.com/babylonlabs-io/finality-provider/types"
"go.uber.org/zap"
"time"
)

// 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
}
11 changes: 10 additions & 1 deletion finality-provider/service/fp_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -259,16 +260,24 @@ 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)
}

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")
})
}
}

0 comments on commit e305548

Please sign in to comment.