Skip to content
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

fix(dot/sync): sync benchmark #2234

Merged
merged 4 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 14 additions & 10 deletions dot/sync/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,35 @@ type syncBenchmarker struct {
start time.Time
startBlock uint64
blocksPerSecond []float64
samplesToKeep int
}

func newSyncBenchmarker() *syncBenchmarker {
func newSyncBenchmarker(samplesToKeep int) *syncBenchmarker {
return &syncBenchmarker{
blocksPerSecond: []float64{},
blocksPerSecond: make([]float64, 0, samplesToKeep),
samplesToKeep: samplesToKeep,
}
}

func (b *syncBenchmarker) begin(block uint64) {
b.start = time.Now()
func (b *syncBenchmarker) begin(now time.Time, block uint64) {
b.start = now
b.startBlock = block
}

func (b *syncBenchmarker) end(block uint64) {
duration := time.Since(b.start)
func (b *syncBenchmarker) end(now time.Time, block uint64) {
duration := now.Sub(b.start)
blocks := block - b.startBlock
if blocks == 0 {
blocks = 1
}
bps := float64(blocks) / duration.Seconds()

if len(b.blocksPerSecond) == b.samplesToKeep {
b.blocksPerSecond = b.blocksPerSecond[1:]
}

b.blocksPerSecond = append(b.blocksPerSecond, bps)
}

func (b *syncBenchmarker) average() float64 {
sum := float64(0)
var sum float64
for _, bps := range b.blocksPerSecond {
sum += bps
}
Expand Down
7 changes: 4 additions & 3 deletions dot/sync/chain_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ type chainSyncConfig struct {

func newChainSync(cfg *chainSyncConfig) *chainSync {
ctx, cancel := context.WithCancel(context.Background())
const syncSamplesToKeep = 30
return &chainSync{
ctx: ctx,
cancel: cancel,
Expand All @@ -174,7 +175,7 @@ func newChainSync(cfg *chainSyncConfig) *chainSync {
pendingBlocks: cfg.pendingBlocks,
state: bootstrap,
handler: newBootstrapSyncer(cfg.bs),
benchmarker: newSyncBenchmarker(),
benchmarker: newSyncBenchmarker(syncSamplesToKeep),
finalisedCh: cfg.bs.GetFinalisedNotifierChannel(),
minPeers: cfg.minPeers,
maxWorkerRetries: uint16(cfg.maxPeers),
Expand Down Expand Up @@ -321,7 +322,7 @@ func (cs *chainSync) logSyncSpeed() {
}

if cs.state == bootstrap {
cs.benchmarker.begin(before.Number.Uint64())
cs.benchmarker.begin(time.Now(), before.Number.Uint64())
}

select {
Expand All @@ -345,7 +346,7 @@ func (cs *chainSync) logSyncSpeed() {

switch cs.state {
case bootstrap:
cs.benchmarker.end(after.Number.Uint64())
cs.benchmarker.end(time.Now(), after.Number.Uint64())
target := cs.getTarget()

logger.Infof(
Expand Down