Skip to content

Commit 1edd273

Browse files
Merge pull request #141 from coinbase/patrick/log-blocks-per-second
Log Blocks Synced Per Second
2 parents 71b05bf + e634bb2 commit 1edd273

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

pkg/logger/logger.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import (
3434
var _ statefulsyncer.Logger = (*Logger)(nil)
3535

3636
const (
37+
// TimeElapsedCounter tracks the total time elapsed in seconds.
38+
TimeElapsedCounter = "time_elapsed"
39+
3740
// blockStreamFile contains the stream of processed
3841
// blocks and whether they were added or removed.
3942
blockStreamFile = "blocks.txt"
@@ -107,6 +110,17 @@ func (l *Logger) LogDataStats(ctx context.Context) error {
107110
return nil
108111
}
109112

113+
elapsedTime, err := l.CounterStorage.Get(ctx, TimeElapsedCounter)
114+
if err != nil {
115+
return fmt.Errorf("%w cannot get elapsed time", err)
116+
}
117+
118+
if elapsedTime.Sign() == 0 { // wait for at least some elapsed time
119+
return nil
120+
}
121+
122+
blocksPerSecond := new(big.Int).Div(blocks, elapsedTime)
123+
110124
orphans, err := l.CounterStorage.Get(ctx, storage.OrphanCounter)
111125
if err != nil {
112126
return fmt.Errorf("%w cannot get orphan counter", err)
@@ -133,9 +147,10 @@ func (l *Logger) LogDataStats(ctx context.Context) error {
133147
}
134148

135149
statsMessage := fmt.Sprintf(
136-
"[STATS] Blocks: %s (Orphaned: %s) Transactions: %s Operations: %s",
150+
"[STATS] Blocks: %s (Orphaned: %s, Rate: %s/sec) Transactions: %s Operations: %s",
137151
blocks.String(),
138152
orphans.String(),
153+
blocksPerSecond.String(),
139154
txs.String(),
140155
ops.String(),
141156
)

pkg/tester/data.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121
"log"
22+
"math/big"
2223
"os"
2324
"time"
2425

@@ -49,16 +50,15 @@ const (
4950
// until the client halts the search or the block is found).
5051
InactiveFailureLookbackWindow = 250
5152

53+
// periodicLoggingSeconds is the frequency to print stats in seconds.
54+
periodicLoggingSeconds = 10
55+
5256
// PeriodicLoggingFrequency is the frequency that stats are printed
5357
// to the terminal.
54-
//
55-
// TODO: make configurable
56-
PeriodicLoggingFrequency = 10 * time.Second
58+
PeriodicLoggingFrequency = periodicLoggingSeconds * time.Second
5759

5860
// EndAtTipCheckInterval is the frequency that EndAtTip condition
5961
// is evaludated
60-
//
61-
// TODO: make configurable
6262
EndAtTipCheckInterval = 10 * time.Second
6363
)
6464

@@ -357,6 +357,13 @@ func (t *DataTester) StartPeriodicLogger(
357357

358358
return ctx.Err()
359359
case <-tc.C:
360+
// Update the elapsed time in counter storage so that
361+
// we can log metrics about the current check:data run.
362+
_, _ = t.counterStorage.Update(
363+
ctx,
364+
logger.TimeElapsedCounter,
365+
big.NewInt(periodicLoggingSeconds),
366+
)
360367
_ = t.logger.LogDataStats(ctx)
361368
}
362369
}

0 commit comments

Comments
 (0)