Report wait on leaf phases only and include it in module.download
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful

This commit is contained in:
2026-11-08 20:30:31 +11:00
parent dd1421f77c
commit 55224fd9f7
3 changed files with 65 additions and 5 deletions
+3 -2
View File
@@ -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
}
+54
View File
@@ -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())
}
}
+8 -3
View File
@@ -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...)
}
}