gunk-dev/the-valley

checkcost_test.go

source937924e2f63emain's published tip history
8.4 KiB
1package main
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strconv"
10 "strings"
11 "syscall"
12 "testing"
13 "time"
14)
15
16// A host or a proxy that says far more on ssh's stderr than a read keeps
17// costs it no more than sshSaidLimit bytes: the read's error carries the
18// last of what was said, which says why it failed.
19func TestAReadKeepsTheLastOfWhatSSHSaid(t *testing.T) {
20 dir := t.TempDir()
21 toolsOnPath(t, dir)
22 ssh := "#!/bin/sh\nhead -c 1048576 /dev/zero | tr '\\0' x >&2\necho ' the host went away' >&2\nexit 255\n"
23 if err := os.WriteFile(filepath.Join(dir, "bin", "ssh"), []byte(ssh), 0o755); err != nil {
24 t.Fatal(err)
25 }
26 _, err := readPage(context.Background(), sshDest{host: "valley-host"}, "")
27 if err == nil || !strings.HasSuffix(err.Error(), "x the host went away") || len(err.Error()) > sshSaidLimit+200 {
28 t.Errorf("the read said %d bytes, ending %q", len(fmt.Sprint(err)), fmt.Sprint(err)[max(len(fmt.Sprint(err))-60, 0):])
29 }
30}
31
32// lastBytes keeps the last bytes written through it, however they come.
33func TestLastBytes(t *testing.T) {
34 l := &lastBytes{max: 4}
35 for _, w := range []string{"ab", "cde", "f", "ghijklm", "n"} {
36 l.Write([]byte(w))
37 }
38 if string(l.b) != "klmn" {
39 t.Errorf("kept %q, not the last four bytes", l.b)
40 }
41}
42
43// Closing the reads cancels one still running at once, even when a
44// process its ssh started, as a ProxyCommand is, holds ssh's output open:
45// close never waits for that output, which a wait for ssh would take up
46// to sshWaitDelay to give up on.
47func TestClosingAReadDoesNotWaitForOutputHeldOpen(t *testing.T) {
48 dir := t.TempDir()
49 toolsOnPath(t, dir, "git")
50 said := filepath.Join(dir, "ssh.pids")
51 if err := syscall.Mkfifo(said, 0o600); err != nil {
52 t.Fatal(err)
53 }
54 // ssh starts a process that holds its output, says both process IDs,
55 // and stalls.
56 ssh := fmt.Sprintf("#!/bin/sh\nsleep 86397 &\necho \"$! $$\" > '%s'\nexec sleep 86396\n", said)
57 if err := os.WriteFile(filepath.Join(dir, "bin", "ssh"), []byte(ssh), 0o755); err != nil {
58 t.Fatal(err)
59 }
60 repo := filepath.Join(dir, "repo")
61 for _, args := range [][]string{{"init", "--quiet", repo}, {"-C", repo, "config", "valley.telemetry.remote", "ssh://git@valley-host/app.git"}} {
62 if out, err := exec.Command("git", args...).CombinedOutput(); err != nil {
63 t.Fatalf("git %v: %v\n%s", args, err, out)
64 }
65 }
66
67 c := newCosts()
68 read := c.start(git{env: os.Environ(), dir: repo})
69 pids := make(chan []string, 1)
70 go func() {
71 data, _ := os.ReadFile(said)
72 pids <- strings.Fields(string(data))
73 }()
74 var held []string
75 select {
76 case held = <-pids:
77 case <-read.done:
78 unheard(said)
79 t.Fatalf("the read ended before ssh started: %s", read.why)
80 case <-time.After(time.Minute):
81 unheard(said)
82 t.Fatal("the read's ssh did not start in a minute")
83 }
84 for _, p := range held {
85 if n, err := strconv.Atoi(p); err == nil {
86 defer syscall.Kill(n, syscall.SIGKILL)
87 }
88 }
89 began := time.Now()
90 c.close()
91 if took := time.Since(began); took >= sshWaitDelay {
92 t.Errorf("closing the read took %s, waiting for the output a process its ssh started held open", took)
93 }
94}
95
96// A build has grown only when it is slower than its baseline by more than
97// both 20% and 30 s.
98func TestGrew(t *testing.T) {
99 for _, c := range []struct {
100 build, baseline int64
101 grown bool
102 }{
103 {100_000, 60_000, true}, // +40 s, +67%
104 {500_000, 400_000, true}, // +100 s, +25%
105 {431_000, 363_000, false}, // +68 s, but +18.7%
106 {80_000, 60_000, false}, // +20 s, +33%
107 {1_230_000, 1_200_000 - 1, false}, // +30 s, +2.5%
108 {400_000, 400_000, false},
109 {10_000, 400_000, false}, // faster
110 } {
111 if got := grew(c.build, c.baseline); got != c.grown {
112 t.Errorf("grew(%d, %d) = %v, want %v", c.build, c.baseline, got, c.grown)
113 }
114 }
115}
116
117func TestMedian(t *testing.T) {
118 for _, c := range []struct {
119 ms []int64
120 want int64
121 }{
122 {[]int64{5}, 5},
123 {[]int64{9, 1, 5}, 5},
124 {[]int64{4, 1, 3, 2}, 2},
125 {[]int64{10, 20}, 15},
126 } {
127 if got := median(c.ms); got != c.want {
128 t.Errorf("median(%v) = %d, want %d", c.ms, got, c.want)
129 }
130 }
131}
132
133// The baseline is the median of a check's newest 20 uncached, passing
134// builds in the project, with every version of the change left out.
135func TestAddCosts(t *testing.T) {
136 me := strings.Repeat("k", 32)
137 other := func(i int) string { return fmt.Sprintf("%s%02d", strings.Repeat("m", 30), i) }
138 var builds []checkBuild // newest first
139 add := func(b checkBuild) {
140 if b.project == "" {
141 b.project = "app"
142 }
143 if b.result == "" {
144 b.result = checkPassed
145 }
146 builds = append(builds, b)
147 }
148 // This version's own builds, in one run.
149 add(checkBuild{run: "r2", change: me, version: 2, tree: "t2", check: "slow", buildMS: 200_000, evalMS: 24_000})
150 add(checkBuild{run: "r2", change: me, version: 2, tree: "t2", check: "fast", buildMS: 120, evalMS: 24_000, cached: true})
151 add(checkBuild{run: "r2", change: me, version: 2, tree: "t2", check: "fresh", buildMS: 9_000, evalMS: 23_000})
152 // The change's earlier version, never counted.
153 add(checkBuild{run: "r1", change: me, version: 1, tree: "t1", check: "slow", buildMS: 1})
154 // Other changes: a cached build, a failed one and another project's
155 // are left out, then 25 builds of which the newest 20 count.
156 add(checkBuild{change: other(98), check: "slow", buildMS: 2, cached: true})
157 add(checkBuild{change: other(99), check: "slow", buildMS: 3, result: "failed"})
158 add(checkBuild{project: "site", change: other(97), check: "slow", buildMS: 4})
159 for i := 0; i < 25; i++ {
160 ms := int64(100_000 + i*1_000) // the newest 20: 100 s to 119 s
161 if i >= 20 {
162 ms = 900_000
163 }
164 add(checkBuild{change: other(i), check: "slow", buildMS: ms})
165 add(checkBuild{change: other(i), check: "fast", buildMS: 5_000})
166 }
167 out := &jsonVersionChecks{Tree: "t2", Required: []jsonCheckState{{Name: "slow"}, {Name: "fast"}, {Name: "fresh"}, {Name: "manual"}}}
168 required := []jsonRequired{{Name: "slow", Runner: "nix"}, {Name: "fast", Runner: "nix"}, {Name: "fresh", Runner: "nix"}, {Name: "manual", Runner: "command"}}
169 addCosts(out, required, "app", me, 2, builds)
170
171 slow, fast, fresh, manual := out.Required[0], out.Required[1], out.Required[2], out.Required[3]
172 if slow.BuildMS == nil || *slow.BuildMS != 200_000 || *slow.Cached || slow.BaselineMS == nil || *slow.BaselineMS != 109_500 || !slow.Grown || slow.New {
173 t.Errorf("slow: %+v", slow)
174 }
175 if fast.BuildMS == nil || *fast.BuildMS != 120 || !*fast.Cached || *fast.BaselineMS != 5_000 || fast.Grown {
176 t.Errorf("fast: %+v", fast)
177 }
178 if fresh.BuildMS == nil || *fresh.BuildMS != 9_000 || fresh.BaselineMS != nil || !fresh.New || fresh.Grown {
179 t.Errorf("fresh: %+v", fresh)
180 }
181 if manual.BuildMS != nil || manual.New || manual.BaselineMS != nil {
182 t.Errorf("manual: %+v", manual)
183 }
184 if out.BuildMS == nil || *out.BuildMS != 209_120 || *out.EvalMS != 24_000 {
185 t.Errorf("totals: build %v, eval %v", out.BuildMS, out.EvalMS)
186 }
187 out.State = checkPassed
188 for i := range out.Required {
189 out.Required[i].State = checkPassed
190 }
191 want := "checks: 4 passed in 3m29s (eval 24s); slow 200s, +91s against its recent 110s"
192 if got := checksLine(out); got != want {
193 t.Errorf("checksLine:\n got %s\nwant %s", got, want)
194 }
195}
196
197// A build is compared only with builds on its own capacity. A check whose
198// builds so far ran on another is new, and is not grown.
199func TestAddCostsCapacity(t *testing.T) {
200 builds := []checkBuild{ // newest first
201 {run: "r", change: "me", version: 1, check: "same", buildMS: 200_000, capacity: "big"},
202 {run: "r", change: "me", version: 1, check: "resized", buildMS: 200_000, capacity: "big"},
203 {change: "o1", check: "same", buildMS: 100_000, capacity: "big"},
204 {change: "o1", check: "resized", buildMS: 10_000, capacity: "small"},
205 }
206 var states []jsonCheckState
207 var required []jsonRequired
208 for _, name := range []string{"same", "resized"} {
209 builds[len(states)].project, builds[len(states)+2].project = "app", "app"
210 builds[len(states)].result, builds[len(states)+2].result = checkPassed, checkPassed
211 states = append(states, jsonCheckState{Name: name, State: checkPassed})
212 required = append(required, jsonRequired{Name: name, Runner: "nix"})
213 }
214 out := &jsonVersionChecks{State: checkPassed, Required: states}
215 addCosts(out, required, "app", "me", 1, builds)
216 if same := out.Required[0]; !same.Grown || same.New || *same.BaselineMS != 100_000 {
217 t.Errorf("same: %+v", same)
218 }
219 if resized := out.Required[1]; !resized.New || !resized.OtherCapacity || resized.Grown || resized.BaselineMS != nil {
220 t.Errorf("resized: %+v", resized)
221 }
222}