Repository navigation
Expand file tree
/
Copy pathgenerator.go
More file actions
359 lines (322 loc) · 9.58 KB
/
Copy pathgenerator.go
File metadata and controls
359 lines (322 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2025 The quickjs-go Authors. All rights reserved.
// Use of the source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
goruntime "runtime"
"slices"
"strings"
"time"
"golang.org/x/mod/modfile"
)
const (
docFn = "quickjs.go"
modFn = "go.mod"
qemu = " (qemu)"
// fingerprintPrefix starts the first line of a results file, recording the
// root go.mod the numbers below it were measured against. The benchmarks
// re-run when that no longer matches, ie. exactly when a builder
// regenerates: its autogen gate compares the same go.mod against
// internal/autogen/<goos>_<goarch>.mod. Nothing needs bumping by hand.
fingerprintPrefix = "# root go.mod sha256 "
// The labels of the geomean rows compare/benchreport.go prints, one per
// table: the V8 benchmark suite and the micro benchmarks.
geomeanV8 = "v8-geomean"
geomeanMicro = "geomean"
)
var (
goos = runtime.GOOS
goarch = runtime.GOARCH
target = fmt.Sprintf("%s_%s", goos, goarch) + ".txt"
resultsDir = path.Join("testdata", "benchmarks")
notes = map[string]string{
"freebsd/amd64": qemu,
"freebsd/arm64": qemu,
"linux/386": qemu,
"linux/s390x": qemu,
"openbsd/amd64": qemu,
"windows/386": qemu,
}
)
func fail(rc int, s string, args ...any) {
fmt.Fprintln(os.Stderr, fmt.Sprintf(s, args...))
os.Exit(rc)
}
func main() {
fp, err := fingerprint()
if err != nil {
fail(1, "%v", err)
}
resultsFn := path.Join(resultsDir, target)
ok, err := measured(resultsFn, fp)
if err != nil {
fail(1, "%v", err)
}
switch {
case ok:
fmt.Printf("%s: measured against the current %s, skipping benchmarks\n", resultsFn, modFn)
default:
fmt.Printf("%s: missing or measured against a different %s, benchmarking\n", resultsFn, modFn)
if err := benchmark(resultsFn, fp); err != nil {
fail(1, "%v", err)
}
}
// Unconditional: keeps the package documentation in sync with
// testdata/benchmarks even when this platform had nothing to measure.
if err := document(); err != nil {
fail(1, "%v", err)
}
}
// fingerprint returns the first line of a results file produced now.
func fingerprint() (string, error) {
b, err := os.ReadFile(modFn)
if err != nil {
return "", err
}
return fmt.Sprintf("%s%x", fingerprintPrefix, sha256.Sum256(b)), nil
}
// measured reports whether fn holds results recorded for fingerprint fp. A
// missing file, one predating fingerprintPrefix or one predating the V8 table
// is not measured.
func measured(fn, fp string) (bool, error) {
b, err := os.ReadFile(fn)
switch {
case err == nil:
line, _, _ := bytes.Cut(b, []byte("\n"))
return string(line) == fp && geomeans(b)[geomeanV8] != nil, nil
case os.IsNotExist(err):
return false, nil
default:
return false, err
}
}
// benchmark runs the ./compare module and records its report in fn, stamped
// with fp.
func benchmark(fn, fp string) error {
if err := os.MkdirAll(resultsDir, 0775); err != nil {
return err
}
var stdout, stderr bytes.Buffer
// Through a shell: on Windows the builders run 'go generate' from git-bash
// and make lives there.
cmd := exec.Command("sh", "-c", "make benchmark")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("make benchmark: %v\nstdout=%s\nstderr=%s", err, stdout.Bytes(), stderr.Bytes())
}
// Only on success: a partial file carrying the current fingerprint would
// pass for measured and suppress the retry the next cycle owes us.
return os.WriteFile(fn, append([]byte(fp+"\n"), stdout.Bytes()...), 0660)
}
// geomeans returns the geomean rows of the results in b, keyed by their label,
// the values in the order CCGO, GOJA, QJS.
func geomeans(b []byte) map[string][]string {
r := map[string][]string{}
for _, v := range strings.Split(string(b), "\n") {
if f := strings.Fields(v); len(f) >= 4 && (f[0] == geomeanV8 || f[0] == geomeanMicro) { // geomean 1.000 1.019 0.848
r[f[0]] = f[1:4]
}
}
return r
}
// requires returns the versions of the modules required by the go.mod at fn,
// keyed by module path.
func requires(fn string) (map[string]string, error) {
b, err := os.ReadFile(fn)
if err != nil {
return nil, err
}
mod, err := modfile.ParseLax(fn, b, nil)
if err != nil {
return nil, err
}
r := map[string]string{}
for _, v := range mod.Require {
r[v.Mod.Path] = v.Mod.Version
}
return r, nil
}
// document rewrites the '# Performance' section of the docFn package
// documentation from every results file present.
func document() error {
// The measured engine comes from the root go.mod. The compare module gets
// this one through a 'replace' pointing at the work tree, so its own
// modernc.org/quickjs require records nothing about what ran.
root, err := requires(modFn)
if err != nil {
return err
}
compare, err := requires(path.Join("compare", "go.mod"))
if err != nil {
return err
}
m, err := filepath.Glob(path.Join(resultsDir, "*.txt"))
if err != nil {
return err
}
for i, v := range m {
m[i] = filepath.ToSlash(v)
}
var results []platformResults
for _, fn := range m {
b, err := os.ReadFile(fn)
if err != nil {
return err
}
key := path.Base(fn) // os_arch.txt
key = key[:len(key)-len(".txt")] // os_arch
key = strings.Replace(key, "_", "/", 1) // os/arch
results = append(results, platformResults{key, geomeans(b)})
}
slices.SortFunc(results, func(a, b platformResults) int { return strings.Compare(a.key, b.key) })
doc, err := os.ReadFile(docFn)
if err != nil {
return err
}
a := strings.Split(string(doc), "\n")
buf := bytes.NewBuffer(nil)
state := 0
ver := func(ip string, versions map[string]string) string {
return fmt.Sprintf("%s@%s", ip, versions[ip])
}
for _, v := range a {
switch state {
case 0:
fmt.Fprintf(buf, "%s\n", v)
if strings.HasPrefix(v, "// # Performance") {
state++
}
case 1:
fmt.Fprintf(buf, `//
// Geomeans of time/op relative to CCGO, lower number is better, n/a where not
// measured yet. Detailed results are in the testdata/benchmarks directory.
//
// CCGO: %s
// GOJA: %s
// QJS: %s
//
// The V8 benchmark suite, version 7: Richards, DeltaBlue, Crypto, RayTrace,
// EarleyBoyer, RegExp, Splay and NavierStokes. These are complete programs and
// the better guide to how fast an application runs. QJS evaluates in strict
// mode only, in which Crypto and EarleyBoyer do not load; its number is over
// the other six.
//
`,
ver("modernc.org/libquickjs", root),
ver("github.com/dop251/goja", compare),
ver("github.com/fastschema/qjs", compare),
)
table(buf, results, geomeanV8)
fmt.Fprintf(buf, `//
// The QuickJS micro benchmarks, loops that each repeat a single operation,
// like a property read or a string append. They show which operations an
// engine is fast or slow at, not how fast it runs a program.
//
`)
table(buf, results, geomeanMicro)
state++
case 2:
switch {
case strings.HasPrefix(v, "// # Notes"):
fmt.Fprintf(buf, "%s\n", v)
state++
}
case 3:
fmt.Fprintf(buf, "%s\n", v)
}
}
if err := os.WriteFile(docFn, buf.Bytes(), 0660); err != nil {
return err
}
// The builders gofmt after generating, a local 'go generate' does not.
if out, err := exec.Command("gofmt", "-s", "-w", docFn).CombinedOutput(); err != nil {
return fmt.Errorf("gofmt %s: %v\n%s", docFn, err, out)
}
return nil
}
// platformResults are the geomean rows of one results file.
type platformResults struct {
key string // os/arch
geomeans map[string][]string // by label: CCGO, GOJA, QJS
}
// table writes the rows labeled label of results to w, n/a for a platform
// without one.
func table(w io.Writer, results []platformResults, label string) {
fmt.Fprintf(w, `// CCGO GOJA QJS
// -----------------------------------------------
`)
for _, v := range results {
g := v.geomeans[label]
if g == nil {
g = []string{"n/a", "n/a", "n/a"}
}
fmt.Fprintf(w, "// %20s%9s%9s%9s%s\n", v.key, g[0], g[1], g[2], notes[v.key])
}
fmt.Fprintf(w, `// -----------------------------------------------
// CCGO GOJA QJS
`)
}
// origin returns caller's short position, skipping skip frames.
func origin(skip int) string {
pc, fn, fl, _ := goruntime.Caller(skip)
f := goruntime.FuncForPC(pc)
var fns string
if f != nil {
fns = f.Name()
if x := strings.LastIndex(fns, "."); x > 0 {
fns = fns[x+1:]
}
if strings.HasPrefix(fns, "func") {
num := true
for _, c := range fns[len("func"):] {
if c < '0' || c > '9' {
num = false
break
}
}
if num {
return origin(skip + 2)
}
}
}
return fmt.Sprintf("%s:%d:%s", filepath.Base(fn), fl, fns)
}
// todo prints and return caller's position and an optional message tagged with TODO. Output goes to stderr.
func todo(s string, args ...interface{}) string {
switch {
case s == "":
s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
default:
s = fmt.Sprintf(s, args...)
}
r := fmt.Sprintf("%s\n\tTODO %s", origin(2), s)
// fmt.Fprintf(os.Stderr, "%s\n", r)
// os.Stdout.Sync()
return r
}
// trc prints and return caller's position and an optional message tagged with TRC. Output goes to stderr.
//
//lint:ignore U1000 debug helper
func trc(s string, args ...interface{}) string {
switch {
case s == "":
s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
default:
s = fmt.Sprintf(s, args...)
}
r := fmt.Sprintf("%s: TRC %s", origin(2), s)
fmt.Fprintf(os.Stderr, "%s %s\n", time.Now().Format("15:04:05.000"), r)
os.Stderr.Sync()
return r
}