gunk-dev/the-valley

checkcost_test.go

source17c92d08ab12main's published tip history
5.4 KiB
1package main
2
3import (
4 "fmt"
5 "strings"
6 "testing"
7)
8
9// A build has grown only when it is slower than its baseline by more than
10// both 20% and 30 s.
11func TestGrew(t *testing.T) {
12 for _, c := range []struct {
13 build, baseline int64
14 grown bool
15 }{
16 {100_000, 60_000, true}, // +40 s, +67%
17 {500_000, 400_000, true}, // +100 s, +25%
18 {431_000, 363_000, false}, // +68 s, but +18.7%
19 {80_000, 60_000, false}, // +20 s, +33%
20 {1_230_000, 1_200_000 - 1, false}, // +30 s, +2.5%
21 {400_000, 400_000, false},
22 {10_000, 400_000, false}, // faster
23 } {
24 if got := grew(c.build, c.baseline); got != c.grown {
25 t.Errorf("grew(%d, %d) = %v, want %v", c.build, c.baseline, got, c.grown)
26 }
27 }
28}
29
30func TestMedian(t *testing.T) {
31 for _, c := range []struct {
32 ms []int64
33 want int64
34 }{
35 {[]int64{5}, 5},
36 {[]int64{9, 1, 5}, 5},
37 {[]int64{4, 1, 3, 2}, 2},
38 {[]int64{10, 20}, 15},
39 } {
40 if got := median(c.ms); got != c.want {
41 t.Errorf("median(%v) = %d, want %d", c.ms, got, c.want)
42 }
43 }
44}
45
46// The baseline is the median of a check's newest 20 uncached, passing
47// builds in the project, with every version of the change left out.
48func TestAddCosts(t *testing.T) {
49 me := strings.Repeat("k", 32)
50 other := func(i int) string { return fmt.Sprintf("%s%02d", strings.Repeat("m", 30), i) }
51 var builds []checkBuild // newest first
52 add := func(b checkBuild) {
53 if b.project == "" {
54 b.project = "app"
55 }
56 if b.result == "" {
57 b.result = checkPassed
58 }
59 builds = append(builds, b)
60 }
61 // This version's own builds, in one run.
62 add(checkBuild{run: "r2", change: me, version: 2, tree: "t2", check: "slow", buildMS: 200_000, evalMS: 24_000})
63 add(checkBuild{run: "r2", change: me, version: 2, tree: "t2", check: "fast", buildMS: 120, evalMS: 24_000, cached: true})
64 add(checkBuild{run: "r2", change: me, version: 2, tree: "t2", check: "fresh", buildMS: 9_000, evalMS: 23_000})
65 // The change's earlier version, never counted.
66 add(checkBuild{run: "r1", change: me, version: 1, tree: "t1", check: "slow", buildMS: 1})
67 // Other changes: a cached build, a failed one and another project's
68 // are left out, then 25 builds of which the newest 20 count.
69 add(checkBuild{change: other(98), check: "slow", buildMS: 2, cached: true})
70 add(checkBuild{change: other(99), check: "slow", buildMS: 3, result: "failed"})
71 add(checkBuild{project: "site", change: other(97), check: "slow", buildMS: 4})
72 for i := 0; i < 25; i++ {
73 ms := int64(100_000 + i*1_000) // the newest 20: 100 s to 119 s
74 if i >= 20 {
75 ms = 900_000
76 }
77 add(checkBuild{change: other(i), check: "slow", buildMS: ms})
78 add(checkBuild{change: other(i), check: "fast", buildMS: 5_000})
79 }
80 out := &jsonVersionChecks{Tree: "t2", Required: []jsonCheckState{{Name: "slow"}, {Name: "fast"}, {Name: "fresh"}, {Name: "manual"}}}
81 required := []jsonRequired{{Name: "slow", Runner: "nix"}, {Name: "fast", Runner: "nix"}, {Name: "fresh", Runner: "nix"}, {Name: "manual", Runner: "command"}}
82 addCosts(out, required, "app", me, 2, builds)
83
84 slow, fast, fresh, manual := out.Required[0], out.Required[1], out.Required[2], out.Required[3]
85 if slow.BuildMS == nil || *slow.BuildMS != 200_000 || *slow.Cached || slow.BaselineMS == nil || *slow.BaselineMS != 109_500 || !slow.Grown || slow.New {
86 t.Errorf("slow: %+v", slow)
87 }
88 if fast.BuildMS == nil || *fast.BuildMS != 120 || !*fast.Cached || *fast.BaselineMS != 5_000 || fast.Grown {
89 t.Errorf("fast: %+v", fast)
90 }
91 if fresh.BuildMS == nil || *fresh.BuildMS != 9_000 || fresh.BaselineMS != nil || !fresh.New || fresh.Grown {
92 t.Errorf("fresh: %+v", fresh)
93 }
94 if manual.BuildMS != nil || manual.New || manual.BaselineMS != nil {
95 t.Errorf("manual: %+v", manual)
96 }
97 if out.BuildMS == nil || *out.BuildMS != 209_120 || *out.EvalMS != 24_000 {
98 t.Errorf("totals: build %v, eval %v", out.BuildMS, out.EvalMS)
99 }
100 out.State = checkPassed
101 for i := range out.Required {
102 out.Required[i].State = checkPassed
103 }
104 want := "checks: 4 passed in 3m29s (eval 24s); slow 200s, +91s against its recent 110s; fresh 9s, new"
105 if got := checksLine(out); got != want {
106 t.Errorf("checksLine:\n got %s\nwant %s", got, want)
107 }
108}
109
110// A build is compared only with builds on its own capacity. A check whose
111// builds so far ran on another is new, and is not grown.
112func TestAddCostsCapacity(t *testing.T) {
113 builds := []checkBuild{ // newest first
114 {run: "r", change: "me", version: 1, check: "same", buildMS: 200_000, capacity: "big"},
115 {run: "r", change: "me", version: 1, check: "resized", buildMS: 200_000, capacity: "big"},
116 {change: "o1", check: "same", buildMS: 100_000, capacity: "big"},
117 {change: "o1", check: "resized", buildMS: 10_000, capacity: "small"},
118 }
119 var states []jsonCheckState
120 var required []jsonRequired
121 for _, name := range []string{"same", "resized"} {
122 builds[len(states)].project, builds[len(states)+2].project = "app", "app"
123 builds[len(states)].result, builds[len(states)+2].result = checkPassed, checkPassed
124 states = append(states, jsonCheckState{Name: name, State: checkPassed})
125 required = append(required, jsonRequired{Name: name, Runner: "nix"})
126 }
127 out := &jsonVersionChecks{State: checkPassed, Required: states}
128 addCosts(out, required, "app", "me", 1, builds)
129 if same := out.Required[0]; !same.Grown || same.New || *same.BaselineMS != 100_000 {
130 t.Errorf("same: %+v", same)
131 }
132 if resized := out.Required[1]; !resized.New || !resized.OtherCapacity || resized.Grown || resized.BaselineMS != nil {
133 t.Errorf("resized: %+v", resized)
134 }
135}