diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index fb37708..6e3d49d 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -452,7 +452,7 @@ func (d *Deployer) gitModule(ctx context.Context, m Module) (resolved, error) { pctx, end = d.phase(ctx, "module.resolve", "module", m.Name) sha, err := d.revParse(pctx, mirror, cands...) dir := d.path("modules", key, sha) - end("cache", cache(err == nil && present(dir))) + end("cache", cache(d.Timings != nil && err == nil && present(dir))) if err != nil { return resolved{}, err } @@ -517,13 +517,14 @@ func (d *Deployer) forgeModule(ctx context.Context, base string, m Module) (reso var err error dl, err = forgeInstall(ctx, base, release, dir) ex = time.Since(t) - dl + dl += wait return err }) }) if !installed { dl = time.Since(start) } - d.emit(ctx, "module.download", dl, "module", slug, "cache", cache(!installed), "wait", wait) + d.emit(ctx, "module.download", dl, "module", slug, "cache", cache(!installed), "wait", wait.Round(time.Microsecond)) d.emit(ctx, "module.extract", ex, "module", slug, "cache", cache(!installed)) return resolved{name: slug, dir: dir, key: "forge " + base + " " + release, hit: !installed}, err } diff --git a/internal/deploy/deploy_test.go b/internal/deploy/deploy_test.go index c7e03dc..77d8ba7 100644 --- a/internal/deploy/deploy_test.go +++ b/internal/deploy/deploy_test.go @@ -806,3 +806,57 @@ func TestTimingsPhases(t *testing.T) { t.Log(out) } } + +func TestTimingsWaitWithinDur(t *testing.T) { + f := newFixture(t) + commit(t, f.control, map[string]string{"Puppetfile": f.puppetfile()}) + for _, b := range []string{"a", "b", "c", "d", "e", "f", "g", "h"} { + gitT(t, f.control, "branch", b) + } + var buf bytes.Buffer + d := New(f.cfg, 1, 10*time.Minute) + d.Timings = slog.New(slog.NewTextHandler(&buf, nil)) + if err := d.Deploy(context.Background(), nil, false); err != nil { + t.Fatal(err) + } + waits := 0 + for line := range strings.Lines(buf.String()) { + kv := map[string]string{} + for _, f := range strings.Fields(line) { + k, v, _ := strings.Cut(f, "=") + kv[k] = v + } + if kv["phase"] == "run" || kv["phase"] == "env.total" { + if _, ok := kv["wait"]; ok { + t.Errorf("aggregate phase reports wait: %s", line) + } + } + w, ok := kv["wait"] + if !ok { + continue + } + waits++ + wd, err1 := time.ParseDuration(w) + dd, err2 := time.ParseDuration(kv["dur"]) + if err1 != nil || err2 != nil || wd > dd { + t.Errorf("wait must not exceed dur: %s", line) + } + } + if waits == 0 { + t.Fatalf("no wait reported:\n%s", buf.String()) + } +} + +func TestTimingsOffEmitsNothing(t *testing.T) { + f := newFixture(t) + commit(t, f.control, map[string]string{"Puppetfile": f.puppetfile()}) + var buf bytes.Buffer + prev := slog.Default() + slog.SetDefault(slog.New(slog.NewTextHandler(&buf, nil))) + defer slog.SetDefault(prev) + f.mustDeploy(false) + f.mustDeploy(false) + if strings.Contains(buf.String(), "phase=") { + t.Fatalf("timings off must emit nothing:\n%s", buf.String()) + } +} diff --git a/internal/deploy/timing.go b/internal/deploy/timing.go index 74cf417..dcf7868 100644 --- a/internal/deploy/timing.go +++ b/internal/deploy/timing.go @@ -14,6 +14,7 @@ type span struct { parent *span attrs []any wait atomic.Int64 + kids atomic.Bool } func spanOf(ctx context.Context) *span { @@ -22,24 +23,28 @@ func spanOf(ctx context.Context) *span { } func addWait(ctx context.Context, w time.Duration) { - for s := spanOf(ctx); s != nil; s = s.parent { + if s := spanOf(ctx); s != nil { s.wait.Add(int64(w)) } } -// phase times one unit of work; nested phases inherit attrs, and -j slot waits are reported apart as wait. +// phase times one unit of work; nested phases inherit attrs, and leaf phases report their -j slot waits as wait. func (d *Deployer) phase(ctx context.Context, name string, attrs ...any) (context.Context, func(more ...any)) { if d.Timings == nil { return ctx, func(...any) {} } s := &span{parent: spanOf(ctx), attrs: attrs} if s.parent != nil { + s.parent.kids.Store(true) s.attrs = append(slices.Clip(s.parent.attrs), attrs...) } start := time.Now() ctx = context.WithValue(ctx, spanKey{}, s) return ctx, func(more ...any) { - d.emit(ctx, name, time.Since(start), append(more, "wait", time.Duration(s.wait.Load()).Round(time.Microsecond))...) + if !s.kids.Load() { + more = append(more, "wait", time.Duration(s.wait.Load()).Round(time.Microsecond)) + } + d.emit(ctx, name, time.Since(start), more...) } }