| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
"runtime" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
const fixture = `# Title |
| 12 |
|
| 13 |
` + "```text" + ` |
| 14 |
sample output that is not a benchmark table |
| 15 |
` + "```" + ` |
| 16 |
|
| 17 |
Environment: |
| 18 |
goos: darwin |
| 19 |
goarch: arm64 |
| 20 |
pkg: github.com/gofiber/utils/v2 |
| 21 |
cpu: Apple M2 Pro |
| 22 |
|
| 23 |
` + "```text" + ` |
| 24 |
// go test ./... -benchmem -run=^$ -bench=Benchmark_ -count=1 |
| 25 |
|
| 26 |
# Group |
| 27 |
Benchmark_Kept/a-12 100 1.000 ns/op 0 B/op 0 allocs/op |
| 28 |
Benchmark_Gone/b-12 200 2.000 ns/op 8 B/op 1 allocs/op |
| 29 |
Benchmark_Both/c-12 300 3.000 ns/op 0 B/op 0 allocs/op |
| 30 |
Benchmark_Wide/d-12 400 4.000 ns/op 0 B/op 0 allocs/op |
| 31 |
` + "```" + ` |
| 32 |
` |
| 33 |
|
| 34 |
func TestParseBlocks(t *testing.T) { |
| 35 |
blocks, err := parseBlocks(strings.Split(fixture, "\n")) |
| 36 |
if err != nil { |
| 37 |
t.Fatal(err) |
| 38 |
} |
| 39 |
// The plain ```text block above carries no Environment header and must not |
| 40 |
// shift the numbering that -block uses. |
| 41 |
if len(blocks) != 1 { |
| 42 |
t.Fatalf("got %d tables, want 1", len(blocks)) |
| 43 |
} |
| 44 |
b := blocks[0] |
| 45 |
if b.goos != "darwin" || b.goarch != "arm64" { |
| 46 |
t.Errorf("got %s/%s, want darwin/arm64", b.goos, b.goarch) |
| 47 |
} |
| 48 |
if b.pkg != "github.com/gofiber/utils/v2" { |
| 49 |
t.Errorf("got pkg %q", b.pkg) |
| 50 |
} |
| 51 |
if want := "go test ./... -benchmem -run=^$ -bench=Benchmark_ -count=1"; strings.Join(b.cmd, " ") != want { |
| 52 |
t.Errorf("got command %q, want %q", strings.Join(b.cmd, " "), want) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func TestParseBlocksRejectsBrokenFences(t *testing.T) { |
| 57 |
for name, md := range map[string]string{ |
| 58 |
"unclosed at end of file": "```text\nrow\n", |
| 59 |
"never closed before the next one": "```text\nrow\n" + |
| 60 |
"```text\nrow\n```\n", |
| 61 |
} { |
| 62 |
if _, err := parseBlocks(strings.Split(md, "\n")); err == nil { |
| 63 |
t.Errorf("%s was accepted", name) |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func TestApplyResults(t *testing.T) { |
| 69 |
lines := strings.Split(fixture, "\n") |
| 70 |
blocks, err := parseBlocks(lines) |
| 71 |
if err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
|
| 75 |
applyResults(lines, blocks[0], 1, strings.Join([]string{ |
| 76 |
"goos: linux", |
| 77 |
"goarch: amd64", |
| 78 |
"pkg: github.com/gofiber/utils/v2", |
| 79 |
"cpu: Intel(R) Xeon(R) Processor @ 2.80GHz", |
| 80 |
"Benchmark_Kept/a-4 300 3.500 ns/op 16 B/op 2 allocs/op", |
| 81 |
// b.SetBytes adds an MB/s column that the table has no room for. |
| 82 |
"Benchmark_Wide/d-4 500 5.500 ns/op 181.82 MB/s 0 B/op 0 allocs/op", |
| 83 |
"Benchmark_New/e-4 400 4.500 ns/op 0 B/op 0 allocs/op", |
| 84 |
// Benchmark_Both is measured by two packages, and only one of them is the |
| 85 |
// one this table's header names. |
| 86 |
"Benchmark_Both/c-4 100 10.000 ns/op 0 B/op 0 allocs/op", |
| 87 |
"pkg: github.com/gofiber/utils/v2/simd", |
| 88 |
"Benchmark_Both/c-4 600 600.000 ns/op 0 B/op 0 allocs/op", |
| 89 |
"PASS", |
| 90 |
}, "\n"), knownRows(lines, blocks)) |
| 91 |
|
| 92 |
out := strings.Join(lines, "\n") |
| 93 |
for _, want := range []string{ |
| 94 |
"Benchmark_Kept/a-4 300 3.500 ns/op 16 B/op 2 allocs/op", |
| 95 |
"Benchmark_Wide/d-4 500 5.500 ns/op 0 B/op 0 allocs/op", |
| 96 |
"goos: linux", |
| 97 |
"goarch: amd64", |
| 98 |
"cpu: Intel(R) Xeon(R) Processor @ 2.80GHz", |
| 99 |
} { |
| 100 |
if !strings.Contains(out, want) { |
| 101 |
t.Errorf("missing %q in:\n%s", want, out) |
| 102 |
} |
| 103 |
} |
| 104 |
if strings.Contains(out, "MB/s") { |
| 105 |
t.Errorf("the MB/s column must not reach the table:\n%s", out) |
| 106 |
} |
| 107 |
if !strings.Contains(out, "Benchmark_Gone/b-12 200 2.000 ns/op 8 B/op 1 allocs/op") { |
| 108 |
t.Error("a row missing from the output must stay untouched") |
| 109 |
} |
| 110 |
if !strings.Contains(out, "Benchmark_Both/c-4 100 10.000 ns/op") { |
| 111 |
t.Errorf("a name in two packages must take the one the table names:\n%s", out) |
| 112 |
} |
| 113 |
if strings.Contains(out, "Benchmark_New") { |
| 114 |
t.Error("a new benchmark must be reported, not inserted") |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func TestApplyResultsNamesNoMachineItCannotRead(t *testing.T) { |
| 119 |
lines := strings.Split(fixture, "\n") |
| 120 |
blocks, err := parseBlocks(lines) |
| 121 |
if err != nil { |
| 122 |
t.Fatal(err) |
| 123 |
} |
| 124 |
|
| 125 |
// arm64 Linux carries no model name, so `go test` prints no cpu line at all. |
| 126 |
applyResults(lines, blocks[0], 1, strings.Join([]string{ |
| 127 |
"goos: linux", |
| 128 |
"goarch: arm64", |
| 129 |
"pkg: github.com/gofiber/utils/v2", |
| 130 |
"Benchmark_Kept/a-4 1 3.500 ns/op 0 B/op 0 allocs/op", |
| 131 |
}, "\n"), knownRows(lines, blocks)) |
| 132 |
|
| 133 |
out := strings.Join(lines, "\n") |
| 134 |
if strings.Contains(out, "Apple M2 Pro") { |
| 135 |
t.Errorf("the header must not keep a machine that did not run:\n%s", out) |
| 136 |
} |
| 137 |
if !strings.Contains(out, "cpu: unknown") { |
| 138 |
t.Errorf("a cpu the run did not report must be named unknown:\n%s", out) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestPickResolvesOnThePackageOfTheTable(t *testing.T) { |
| 143 |
root := benchResult{pkg: "m", ns: 1} |
| 144 |
simd := benchResult{pkg: "m/simd", ns: 2} |
| 145 |
|
| 146 |
if runs, ok := pick([]benchResult{root, root}, "m/other"); !ok || len(runs) != 2 { |
| 147 |
t.Error("one package means no ambiguity, whatever the table says") |
| 148 |
} |
| 149 |
if runs, ok := pick([]benchResult{root, simd}, "m/simd"); !ok || len(runs) != 1 || runs[0].pkg != "m/simd" { |
| 150 |
t.Errorf("got %v, want only the table's own package", runs) |
| 151 |
} |
| 152 |
if _, ok := pick([]benchResult{root, simd}, "m/third"); ok { |
| 153 |
t.Error("an unresolvable name must be reported, not guessed") |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func TestMedianPicksMiddleRun(t *testing.T) { |
| 158 |
_, results, odd := parseBenchOutput(strings.Join([]string{ |
| 159 |
"Benchmark_X-4 1 9.000 ns/op 0 B/op 0 allocs/op", |
| 160 |
"Benchmark_X-4 1 1.000 ns/op 0 B/op 0 allocs/op", |
| 161 |
"Benchmark_X-4 1 5.000 ns/op 0 B/op 0 allocs/op", |
| 162 |
"Benchmark_Odd-4 1 1 x 0 B/op", |
| 163 |
}, "\n")) |
| 164 |
if got := median(results["Benchmark_X"]); got == nil || got.ns != 5 { |
| 165 |
t.Errorf("got %v, want the 5.000 ns/op run", got) |
| 166 |
} |
| 167 |
if median(results["Benchmark_missing"]) != nil { |
| 168 |
t.Error("an unknown benchmark must not resolve to a run") |
| 169 |
} |
| 170 |
// An even -count has no middle; the doc comment promises the slower of the two. |
| 171 |
two := []benchResult{{ns: 1}, {ns: 9}} |
| 172 |
if got := median(two); got == nil || got.ns != 9 { |
| 173 |
t.Errorf("got %v, want the slower of the two middles", got) |
| 174 |
} |
| 175 |
if len(odd) != 1 || odd[0] != "Benchmark_Odd-4" { |
| 176 |
t.Errorf("got %v, want the unusable row reported", odd) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
func TestSplitArgs(t *testing.T) { |
| 181 |
got, err := splitArgs(` go test -run=^$ . -bench='^Benchmark_(A|B)$' `) |
| 182 |
if err != nil { |
| 183 |
t.Fatal(err) |
| 184 |
} |
| 185 |
want := []string{"go", "test", "-run=^$", ".", "-bench=^Benchmark_(A|B)$"} |
| 186 |
if strings.Join(got, "|") != strings.Join(want, "|") { |
| 187 |
t.Errorf("got %q, want %q", got, want) |
| 188 |
} |
| 189 |
|
| 190 |
for _, bad := range []string{"go build ./...", "make bench", `go test -bench='^X`, ""} { |
| 191 |
if _, err := splitArgs(bad); err == nil { |
| 192 |
t.Errorf("%q was accepted", bad) |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
func TestStripProcs(t *testing.T) { |
| 198 |
for in, want := range map[string]string{ |
| 199 |
"Benchmark_X-12": "Benchmark_X", |
| 200 |
"Benchmark_X": "Benchmark_X", |
| 201 |
"Benchmark_IndexControl/a-16B/f": "Benchmark_IndexControl/a-16B/f", |
| 202 |
"Benchmark_IndexControl/a-16B-4": "Benchmark_IndexControl/a-16B", |
| 203 |
} { |
| 204 |
if got := stripProcs(in); got != want { |
| 205 |
t.Errorf("stripProcs(%q) = %q, want %q", in, got, want) |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func TestParseFlags(t *testing.T) { |
| 211 |
for _, args := range [][]string{{"-update"}, {"--update"}, {"-update", "-block", "2"}, {"-update", "-block=2"}} { |
| 212 |
update, block, err := parseFlags(args) |
| 213 |
if err != nil || !update || (len(args) > 1 && block != 2) { |
| 214 |
t.Errorf("parseFlags(%q) = %v, %d, %v", args, update, block, err) |
| 215 |
} |
| 216 |
} |
| 217 |
for _, args := range [][]string{ |
| 218 |
{"-bogus"}, |
| 219 |
{"-block", "2"}, |
| 220 |
{"-update", "-block"}, |
| 221 |
{"-update", "-block", "0"}, |
| 222 |
{"-update", "-block", "x"}, |
| 223 |
{"-update=false"}, |
| 224 |
{"-h=x"}, |
| 225 |
} { |
| 226 |
if _, _, err := parseFlags(args); err == nil { |
| 227 |
t.Errorf("parseFlags(%q) was accepted", args) |
| 228 |
} |
| 229 |
} |
| 230 |
if _, _, err := parseFlags([]string{"-h"}); err != errHelp { //nolint:errorlint,err113 // the sentinel is returned directly |
| 231 |
t.Errorf("-h returned %v, want errHelp", err) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func TestReadWriteLinesRoundTrip(t *testing.T) { |
| 236 |
path := filepath.Join(t.TempDir(), "f.md") |
| 237 |
for in, want := range map[string]string{ |
| 238 |
"a\nb\n": "a\nb\n", |
| 239 |
"a\nb": "a\nb\n", // a missing final newline is added |
| 240 |
"a\n\n": "a\n\n", |
| 241 |
} { |
| 242 |
if err := os.WriteFile(path, []byte(in), 0o600); err != nil { |
| 243 |
t.Fatal(err) |
| 244 |
} |
| 245 |
lines, err := readLines(path) |
| 246 |
if err != nil { |
| 247 |
t.Fatal(err) |
| 248 |
} |
| 249 |
if err := writeLines(path, lines); err != nil { |
| 250 |
t.Fatal(err) |
| 251 |
} |
| 252 |
got, err := os.ReadFile(path) // #nosec G304 -- path is the test's own temp file |
| 253 |
if err != nil { |
| 254 |
t.Fatal(err) |
| 255 |
} |
| 256 |
if string(got) != want { |
| 257 |
t.Errorf("%q round-tripped to %q, want %q", in, got, want) |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// The tool finds its tables by shape, so a README that drifts out of that shape |
| 263 |
// must fail here rather than silently refreshing nothing. |
| 264 |
func TestParseBlocksOnReadme(t *testing.T) { |
| 265 |
lines, err := readLines(filepath.Join("..", readmePath)) |
| 266 |
if err != nil { |
| 267 |
t.Fatal(err) |
| 268 |
} |
| 269 |
blocks, err := parseBlocks(lines) |
| 270 |
if err != nil { |
| 271 |
t.Fatal(err) |
| 272 |
} |
| 273 |
if len(blocks) != 3 { |
| 274 |
t.Fatalf("got %d benchmark tables in README.md, want 3", len(blocks)) |
| 275 |
} |
| 276 |
for i, b := range blocks { |
| 277 |
if len(b.cmd) == 0 { |
| 278 |
t.Errorf(`table %d has no "// go test ..." line`, i+1) |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
func TestUpdateLeavesForeignArchitecturesAlone(t *testing.T) { |
| 284 |
dir := t.TempDir() |
| 285 |
path := filepath.Join(dir, readmePath) |
| 286 |
readme := strings.Replace(fixture, "goarch: arm64", "goarch: "+runtime.GOARCH+"-other", 1) |
| 287 |
if err := os.WriteFile(path, []byte(readme), 0o600); err != nil { |
| 288 |
t.Fatal(err) |
| 289 |
} |
| 290 |
|
| 291 |
err := updateBenchmarks(t.Context(), path, 0) |
| 292 |
if err == nil || !strings.Contains(err.Error(), "no benchmark table was refreshed") { |
| 293 |
t.Fatalf("got %v, want the no-table error", err) |
| 294 |
} |
| 295 |
after, err := os.ReadFile(path) // #nosec G304 -- path is the test's own temp file |
| 296 |
if err != nil { |
| 297 |
t.Fatal(err) |
| 298 |
} |
| 299 |
if string(after) != readme { |
| 300 |
t.Error("a skipped table must not be rewritten") |
| 301 |
} |
| 302 |
|
| 303 |
if err := updateBenchmarks(t.Context(), path, 1); err == nil || !strings.Contains(err.Error(), "no benchmark table was refreshed") { |
| 304 |
t.Errorf("-block must not override the architecture guard, got %v", err) |
| 305 |
} |
| 306 |
if err := updateBenchmarks(t.Context(), path, 2); err == nil { |
| 307 |
t.Error("-block past the last table was accepted") |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
func TestFormatBenchmarksTouchesOnlyTables(t *testing.T) { |
| 312 |
path := filepath.Join(t.TempDir(), readmePath) |
| 313 |
sample := "Benchmark_Sample-8 1 9.000 ns/op 0 B/op 0 allocs/op" |
| 314 |
note := "// swar wins below 32B: 3.2 ns/op against 11.6 ns/op" |
| 315 |
md := strings.Replace(fixture, "sample output that is not a benchmark table", sample, 1) |
| 316 |
md = strings.Replace(md, "# Group", note+"\n\n# Group", 1) |
| 317 |
if err := os.WriteFile(path, []byte(md), 0o600); err != nil { |
| 318 |
t.Fatal(err) |
| 319 |
} |
| 320 |
|
| 321 |
if err := formatBenchmarks(path); err != nil { |
| 322 |
t.Fatal(err) |
| 323 |
} |
| 324 |
after, err := os.ReadFile(path) // #nosec G304 -- path is the test's own temp file |
| 325 |
if err != nil { |
| 326 |
t.Fatal(err) |
| 327 |
} |
| 328 |
if !strings.Contains(string(after), sample) { |
| 329 |
t.Errorf("a fence without an Environment header must stay as it is:\n%s", after) |
| 330 |
} |
| 331 |
if !strings.Contains(string(after), note) { |
| 332 |
t.Errorf("prose inside a table must stay as it is:\n%s", after) |
| 333 |
} |
| 334 |
if !strings.Contains(string(after), "Benchmark_Kept/a-12 100 1.000 ns/op 0 B/op 0 allocs/op") { |
| 335 |
t.Errorf("table rows must be re-aligned:\n%s", after) |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
func TestFormatBenchmarksRefusesABrokenFence(t *testing.T) { |
| 340 |
path := filepath.Join(t.TempDir(), readmePath) |
| 341 |
md := "```text\nBenchmark_X-8 1 1.000 ns/op 0 B/op 0 allocs/op\nprose: 5 ns/op matters\n" |
| 342 |
if err := os.WriteFile(path, []byte(md), 0o600); err != nil { |
| 343 |
t.Fatal(err) |
| 344 |
} |
| 345 |
|
| 346 |
if err := formatBenchmarks(path); err == nil { |
| 347 |
t.Fatal("an unclosed fence was accepted") |
| 348 |
} |
| 349 |
after, err := os.ReadFile(path) // #nosec G304 -- path is the test's own temp file |
| 350 |
if err != nil { |
| 351 |
t.Fatal(err) |
| 352 |
} |
| 353 |
if string(after) != md { |
| 354 |
t.Errorf("a refused file must not be rewritten:\n%s", after) |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
func TestKnownRowsCoversRowsItCannotRefresh(t *testing.T) { |
| 359 |
// No ns/op column, which is what `go test` prints for a sub-nanosecond result. |
| 360 |
md := strings.Replace(fixture, |
| 361 |
"Benchmark_Gone/b-12 200 2.000 ns/op 8 B/op 1 allocs/op", |
| 362 |
"Benchmark_Gone/b-12 200 8 B/op 1 allocs/op", 1) |
| 363 |
lines := strings.Split(md, "\n") |
| 364 |
blocks, err := parseBlocks(lines) |
| 365 |
if err != nil { |
| 366 |
t.Fatal(err) |
| 367 |
} |
| 368 |
if !knownRows(lines, blocks)["Benchmark_Gone/b"] { |
| 369 |
t.Error("a row this tool cannot refresh would be reported as documented nowhere") |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
func TestApplyResultsKeepsThePackageTheTableNames(t *testing.T) { |
| 374 |
lines := strings.Split(fixture, "\n") |
| 375 |
blocks, err := parseBlocks(lines) |
| 376 |
if err != nil { |
| 377 |
t.Fatal(err) |
| 378 |
} |
| 379 |
|
| 380 |
// `go test ./...` spans packages, and whichever prints first is not the one the |
| 381 |
// table documents. Taking it from the run would decide the next run's rows. |
| 382 |
applyResults(lines, blocks[0], 1, strings.Join([]string{ |
| 383 |
"goos: darwin", |
| 384 |
"goarch: arm64", |
| 385 |
"pkg: github.com/gofiber/utils/v2/simd", |
| 386 |
"cpu: Apple M2 Pro", |
| 387 |
"Benchmark_Both/c-12 1 600.000 ns/op 0 B/op 0 allocs/op", |
| 388 |
}, "\n"), knownRows(lines, blocks)) |
| 389 |
|
| 390 |
out := strings.Join(lines, "\n") |
| 391 |
if !strings.Contains(out, "pkg: github.com/gofiber/utils/v2\n") { |
| 392 |
t.Errorf("the table's own package must survive the run:\n%s", out) |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
func TestApplyResultsKeepsRowsAMeasurementCannotFill(t *testing.T) { |
| 397 |
lines := strings.Split(fixture, "\n") |
| 398 |
blocks, err := parseBlocks(lines) |
| 399 |
if err != nil { |
| 400 |
t.Fatal(err) |
| 401 |
} |
| 402 |
|
| 403 |
// What `go test` prints without -benchmem. |
| 404 |
applyResults(lines, blocks[0], 1, strings.Join([]string{ |
| 405 |
"goos: darwin", |
| 406 |
"goarch: arm64", |
| 407 |
"pkg: github.com/gofiber/utils/v2", |
| 408 |
"cpu: Apple M2 Pro", |
| 409 |
"Benchmark_Kept/a-12 1 9.000 ns/op", |
| 410 |
}, "\n"), knownRows(lines, blocks)) |
| 411 |
|
| 412 |
out := strings.Join(lines, "\n") |
| 413 |
if !strings.Contains(out, "Benchmark_Kept/a-12 100 1.000 ns/op 0 B/op 0 allocs/op") { |
| 414 |
t.Errorf("a row must not lose the columns the table documents:\n%s", out) |
| 415 |
} |
| 416 |
} |