Debian Codegraph @

scripts/format_benchmarks.go

raw
1 package main
2
3 import (
4 "bufio"
5 "fmt"
6 "os"
7 "regexp"
8 "strings"
9 )
10
11 func main() {
12 if err := formatBenchmarks("README.md"); err != nil {
13 _, _ = fmt.Fprintln(os.Stderr, err) //nolint:errcheck // printing error only
14 os.Exit(1)
15 }
16 }
17
18 func formatBenchmarks(path string) error {
19 file, err := os.Open(path) // #nosec G304
20 if err != nil {
21 return err
22 }
23 defer file.Close() //nolint:errcheck // No need to check close error
24
25 lines := []string{}
26 scanner := bufio.NewScanner(file)
27 for scanner.Scan() {
28 lines = append(lines, scanner.Text())
29 }
30 if err := scanner.Err(); err != nil {
31 return err
32 }
33
34 type meta struct {
35 kind string
36 index int
37 }
38
39 lineMeta := make([]meta, len(lines))
40 benchEntries := [][]string{}
41 memEntries := [][]string{}
42
43 inBlock := false
44 re := regexp.MustCompile(`\S+`)
45
46 for i, line := range lines {
47 trimmed := strings.TrimSpace(line)
48 switch {
49 case trimmed == "```text":
50 inBlock = true
51 case trimmed == "```" && inBlock:
52 inBlock = false
53 default:
54 if inBlock {
55 expanded := strings.ReplaceAll(line, "\t", " ")
56 if strings.Contains(expanded, "ns/op") {
57 toks := re.FindAllString(expanded, -1)
58 benchEntries = append(benchEntries, toks)
59 lineMeta[i] = meta{kind: "bench", index: len(benchEntries) - 1}
60 } else if strings.Contains(expanded, "allocs/op") {
61 toks := re.FindAllString(expanded, -1)
62 memEntries = append(memEntries, toks)
63 lineMeta[i] = meta{kind: "mem", index: len(memEntries) - 1}
64 }
65 }
66 }
67 }
68
69 benchWidths := columnWidths(benchEntries)
70 memWidths := columnWidths(memEntries)
71
72 var output []string
73 inBlock = false
74 for i, line := range lines {
75 trimmed := strings.TrimSpace(line)
76 switch {
77 case trimmed == "```text":
78 inBlock = true
79 output = append(output, line)
80 continue
81 case trimmed == "```" && inBlock:
82 inBlock = false
83 output = append(output, line)
84 continue
85 }
86
87 m := lineMeta[i]
88 if m.kind == "bench" {
89 toks := benchEntries[m.index]
90 formatted := []string{}
91 for j, t := range toks {
92 switch j {
93 case 0:
94 formatted = append(formatted, padRight(t, benchWidths[j]))
95 case 1, 2, 4, 6:
96 formatted = append(formatted, padLeft(t, benchWidths[j]))
97 default:
98 formatted = append(formatted, t)
99 }
100 }
101 output = append(output, strings.Join(formatted, " "))
102 } else if m.kind == "mem" {
103 toks := memEntries[m.index]
104 formatted := []string{}
105 for j, t := range toks {
106 switch j {
107 case 0, 2:
108 formatted = append(formatted, padLeft(t, memWidths[j]))
109 default:
110 formatted = append(formatted, t)
111 }
112 }
113 output = append(output, strings.Join(formatted, " "))
114 } else {
115 output = append(output, line)
116 }
117 }
118
119 out := strings.Join(output, "\n")
120 if !strings.HasSuffix(out, "\n") {
121 out += "\n"
122 }
123 return os.WriteFile(path, []byte(out), 0o600) // #nosec G306
124 }
125
126 func columnWidths(entries [][]string) []int {
127 maxCols := 0
128 for _, e := range entries {
129 if len(e) > maxCols {
130 maxCols = len(e)
131 }
132 }
133
134 widths := make([]int, maxCols)
135 for _, e := range entries {
136 for i, t := range e {
137 if len(t) > widths[i] {
138 widths[i] = len(t)
139 }
140 }
141 }
142 return widths
143 }
144
145 func padLeft(s string, n int) string {
146 if len(s) >= n {
147 return s
148 }
149 return strings.Repeat(" ", n-len(s)) + s
150 }
151
152 func padRight(s string, n int) string {
153 if len(s) >= n {
154 return s
155 }
156 return s + strings.Repeat(" ", n-len(s))
157 }