Skip to content

Commit 06bf746

Browse files
authored
Add trace timer (#261)
This PR adds a `StartTimer` utility that we can use to show how much time different internal stages of the system are taking if the playground runs in `--log-level=trace` mode. Example of the output: ```$ go run main.go cook l1 --output ./output --log-level trace 2025/12/17 09:08:57 Log level: trace 2025/12/17 09:08:57 deleting existing output directory ./output 2025/12/17 09:08:57 Genesis block hash: 0xf755a8f8203a2a94c3b9c028898f0c218c220bbc4a99648274726912da70e2ee 2025/12/17 09:09:00 [TRACE] artifacts.builder: 3.435216667s 2025/12/17 09:09:00 [TRACE] docker.pull-available-images: 23.35025ms 2025/12/17 09:09:18 [TRACE] docker.up: 18.081256709s ``` Note that in this case, `docker.up` contains the time for `mev-boost-relay` to be ready and healthy which needs beacon to be running.
1 parent a42a462 commit 06bf746

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/flashbots/builder-playground/playground"
13+
"github.com/flashbots/builder-playground/utils"
1314
"github.com/flashbots/builder-playground/utils/mainctx"
1415
"github.com/spf13/cobra"
1516
)
@@ -205,6 +206,11 @@ func runIt(recipe playground.Recipe) error {
205206
return fmt.Errorf("failed to parse log level: %w", err)
206207
}
207208

209+
if logLevel == playground.LevelTrace {
210+
// TODO: We can remove this once we have a logger with log.Trace support
211+
utils.TraceMode = true
212+
}
213+
208214
log.Printf("Log level: %s\n", logLevel)
209215

210216
// parse the overrides

playground/artifacts.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/ethereum/go-ethereum/common/hexutil"
3232
"github.com/ethereum/go-ethereum/core/types"
3333
ecrypto "github.com/ethereum/go-ethereum/crypto"
34+
"github.com/flashbots/builder-playground/utils"
3435
"github.com/hashicorp/go-uuid"
3536
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
3637
"gopkg.in/yaml.v2"
@@ -115,6 +116,8 @@ type Artifacts struct {
115116
}
116117

117118
func (b *ArtifactsBuilder) Build() (*Artifacts, error) {
119+
defer utils.StartTimer("artifacts.builder")()
120+
118121
homeDir, err := GetHomeDir()
119122
if err != nil {
120123
return nil, err

playground/local_runner.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/docker/docker/client"
2727
"github.com/docker/docker/pkg/stdcopy"
2828
"github.com/ethereum/go-ethereum/log"
29+
"github.com/flashbots/builder-playground/utils"
2930
"golang.org/x/sync/errgroup"
3031
"gopkg.in/yaml.v2"
3132
)
@@ -884,6 +885,8 @@ func (d *LocalRunner) ensureImage(ctx context.Context, imageName string) error {
884885
}
885886

886887
func (d *LocalRunner) pullNotAvailableImages(ctx context.Context) error {
888+
defer utils.StartTimer("docker.pull-available-images")()
889+
887890
g, ctx := errgroup.WithContext(ctx)
888891

889892
for _, svc := range d.manifest.Services {
@@ -921,6 +924,8 @@ func (d *LocalRunner) Run(ctx context.Context) error {
921924
return err
922925
}
923926

927+
defer utils.StartTimer("docker.up")()
928+
924929
// First start the services that are running in docker-compose
925930
cmd := exec.CommandContext(
926931
ctx, "docker", "compose",

utils/time_watcher.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package utils
2+
3+
import (
4+
"log"
5+
"time"
6+
)
7+
8+
var TraceMode bool
9+
10+
// StartTimer starts a timer with the given name and returns a function that prints the elapsed time when called.
11+
// The elapsed time is only printed if TraceMode is enabled.
12+
func StartTimer(name string) func() {
13+
start := time.Now()
14+
return func() {
15+
if TraceMode {
16+
elapsed := time.Since(start)
17+
log.Printf("[TRACE] %s: %v\n", name, elapsed)
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)