Skip to content

Commit e2adf7b

Browse files
committed
Log out average duration per block at the end
1 parent e323368 commit e2adf7b

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

tests/load/execute.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func execute(ctx context.Context, preFundedKeys []*secp256k1.PrivateKey, config
5757
registry := prometheus.NewRegistry()
5858
metricsServer := tracker.NewMetricsServer("127.0.0.1:8082", registry)
5959
tracker := tracker.New(registry)
60+
defer func() {
61+
log.Info("average duration per block: %s\n", tracker.GetAverageDurationPerBlock())
62+
}()
6063

6164
agents := make([]*agent.Agent[*types.Transaction, common.Hash], config.agents)
6265
for i := range agents {

tests/load/tracker/tracker.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ type Tracker struct {
1818
metrics *metrics
1919

2020
stats struct {
21-
confirmed uint64
22-
failed uint64
21+
confirmed uint64
22+
failed uint64
23+
durationPerBlock []time.Duration
2324
}
2425
mutex sync.Mutex
2526
}
@@ -108,8 +109,9 @@ func (t *Tracker) ObserveBlock(number uint64) {
108109
// when polling the node periodically instead of using a subscription.
109110
numberDiff := number - t.lastBlockNumber
110111
timeDiff := now.Sub(t.lastBlockTime)
111-
secondsPerBlock := timeDiff.Seconds() / float64(numberDiff)
112-
t.metrics.BlockTimes.Observe(secondsPerBlock)
112+
durationPerBlock := timeDiff / time.Duration(numberDiff)
113+
t.stats.durationPerBlock = append(t.stats.durationPerBlock, durationPerBlock)
114+
t.metrics.BlockTimes.Observe(durationPerBlock.Seconds())
113115
t.lastBlockTime = now
114116
t.lastBlockNumber = number
115117
}
@@ -129,3 +131,14 @@ func (t *Tracker) GetObservedFailed() uint64 {
129131
defer t.mutex.Unlock()
130132
return t.stats.failed
131133
}
134+
135+
// GetAverageDurationPerBlock returns the average duration per block.
136+
func (t *Tracker) GetAverageDurationPerBlock() time.Duration {
137+
t.mutex.Lock()
138+
defer t.mutex.Unlock()
139+
var average time.Duration
140+
for _, durationPerBlock := range t.stats.durationPerBlock {
141+
average += durationPerBlock
142+
}
143+
return average / time.Duration(len(t.stats.durationPerBlock))
144+
}

0 commit comments

Comments
 (0)