Debian Codegraph @

scripts/format_benchmarks.go

raw symbols SCIP
1 package mainscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/
2
3 import (
4 "bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/"
5 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
6 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
7 "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/"
8 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
9 )
10
11 func mainscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/main().() {
12 if err := formatBenchmarksscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/formatBenchmarks().("README.md"); err != nil {
13 _, _ = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintln().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., err) //nolint:errcheck // printing error only
14 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Exitscip-go gomod github.com/golang/go/src go1.25.0 os/Exit().(1)
15 }
16 }
17
18 func formatBenchmarksscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/formatBenchmarks().(path string) error {
19 file, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Openscip-go gomod github.com/golang/go/src go1.25.0 os/Open().(path) // #nosec G304
20 if err != nil {
21 return err
22 }
23 defer file.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().() //nolint:errcheck // No need to check close error
24
25 lines := []string{}
26 scanner := bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.25.0 bufio/NewScanner().(file)
27 for scanner.Scanscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Scan().() {
28 lines = append(lines, scanner.Textscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Text().())
29 }
30 if err := scanner.Errscip-go gomod github.com/golang/go/src go1.25.0 bufio/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 := regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`\S+`)
45
46 for i, line := range lines {
47 trimmed := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 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 := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(line, "\t", " ")
56 if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(expanded, "ns/op") {
57 toks := re.FindAllStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#FindAllString().(expanded, -1)
58 benchEntries = append(benchEntries, toks)
59 lineMeta[i] = meta{kind: "bench", index: len(benchEntries) - 1}
60 } else if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(expanded, "allocs/op") {
61 toks := re.FindAllStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#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 := columnWidthsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/columnWidths().(benchEntries)
70 memWidths := columnWidthsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/columnWidths().(memEntries)
71
72 var output []string
73 inBlock = false
74 for i, line := range lines {
75 trimmed := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 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, padRightscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/padRight().(t, benchWidths[j]))
95 case 1, 2, 4, 6:
96 formatted = append(formatted, padLeftscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/padLeft().(t, benchWidths[j]))
97 default:
98 formatted = append(formatted, t)
99 }
100 }
101 output = append(output, stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 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, padLeftscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/padLeft().(t, memWidths[j]))
109 default:
110 formatted = append(formatted, t)
111 }
112 }
113 output = append(output, stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(formatted, " "))
114 } else {
115 output = append(output, line)
116 }
117 }
118
119 out := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(output, "\n")
120 if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(out, "\n") {
121 out += "\n"
122 }
123 return osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(path, []byte(out), 0o600) // #nosec G306
124 }
125
126 func columnWidthsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/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 padLeftscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/padLeft().(s string, n int) string {
146 if len(s) >= n {
147 return s
148 }
149 return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 strings/Repeat().(" ", n-len(s)) + s
150 }
151
152 func padRightscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/scripts`/padRight().(s string, n int) string {
153 if len(s) >= n {
154 return s
155 }
156 return s + stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 strings/Repeat().(" ", n-len(s))
157 }