@abide/harness 0.2.2 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abide/harness",
3
- "version": "0.2.2",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "description": "Ratios against hand-written code, counts of the work an op did, and gates whose revert can be run — three measurement lanes for anything that renders, on bun",
6
6
  "license": "MIT",
@@ -23,7 +23,7 @@
23
23
  "playwright"
24
24
  ],
25
25
  "engines": {
26
- "bun": ">=1.4.3"
26
+ "bun": ">=1.4.4"
27
27
  },
28
28
  "publishConfig": {
29
29
  "access": "public",
@@ -33,6 +33,9 @@
33
33
  "src",
34
34
  "scripts/buildInjectable.ts"
35
35
  ],
36
+ "bin": {
37
+ "harness-bench": "./src/runner/harnessBench.ts"
38
+ },
36
39
  "exports": {
37
40
  "./measure": "./src/measure/index.ts",
38
41
  "./preload": "./src/measure/preload.ts",
@@ -55,7 +58,7 @@
55
58
  },
56
59
  "peerDependencies": {
57
60
  "@playwright/test": "^1.64.0",
58
- "@abide/abide": ">=0.113.0"
61
+ "@abide/abide": ">=0.119.0"
59
62
  },
60
63
  "peerDependenciesMeta": {
61
64
  "@playwright/test": {
@@ -7,6 +7,7 @@
7
7
  // gated there, where the driver below can only run under `bun run bench`.
8
8
 
9
9
  import type { CDPSession, Page } from '@playwright/test'
10
+
10
11
  import type { Trace } from './Trace.ts'
11
12
 
12
13
  // `devtools.timeline` carries Paint and the Layout events; the `disabled-by-default`
@@ -7,7 +7,7 @@
7
7
  // `harness/report` is a FOURTH entry and is not a fourth lane: a lane is a DEPENDENCY
8
8
  // partition, and the leaf depends on nothing.
9
9
 
10
- import { type Batched, type BatchSpec, batch } from '../report/batch.ts'
10
+ import { batch, type Batched, type BatchSpec } from '../report/batch.ts'
11
11
  import { emulatedDom, underBun } from '../report/environment.ts'
12
12
  import { install, isArmed } from './install.ts'
13
13
 
@@ -8,6 +8,7 @@
8
8
  // members once per case, for the process. `arm()`/`disarm()` swap the record instead,
9
9
  // so the prototype shape is fixed for the process lifetime.
10
10
  import { GlobalRegistrator } from '@happy-dom/global-registrator'
11
+
11
12
  import { install } from './install.ts'
12
13
 
13
14
  // A child spawned with this env would paint its output in ANSI off a pipe (bun's console, and
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env bun
2
+ // `harness-bench [word]`: every bench under `src/` of the directory it runs in (`runBenches`), one
3
+ // block each, and exit 1 after listing every arm over its ceiling and every ceiling naming no arm
4
+ // or no bench.
5
+
6
+ import { runBenches } from './runBenches.ts'
7
+
8
+ const over: string[] = []
9
+ for (const bench of await runBenches(process.cwd(), process.argv[2])) {
10
+ for (const line of bench.over)
11
+ over.push(`${bench.file} ${bench.name}: ${line}`)
12
+ // A ceiling naming no bench (43.32) has nothing to print but its line above.
13
+ if (bench.case === '') continue
14
+ let times = ''
15
+ for (const [arm, nanoseconds] of Object.entries(bench.arms))
16
+ times += `${times === '' ? '' : ' · '}${arm} ${(nanoseconds / 1e6).toFixed(3)} ms`
17
+ console.log(
18
+ `${bench.file} ${bench.name}: ${bench.case}\n ${times}\n ${bench.claim}`,
19
+ )
20
+ }
21
+ if (over.length > 0) {
22
+ console.error(`\nOver the ceiling:\n ${over.join('\n ')}`)
23
+ process.exit(1)
24
+ }
@@ -1,9 +1,11 @@
1
- // RUNNERS THAT PRINT ROWS. A runner is a subprocess writing one JSON row per line, so a page or a
2
- // terminal reading it never shares the process a suite or a bench runs in — its clock, its heap,
3
- // its event loop. `@abide/harness/rows` is the contract the rows keep; this is what writes them.
1
+ // RUNNERS. A row runner is a subprocess writing one JSON row per line, so a page or a terminal
2
+ // reading it never shares the process a suite or a bench runs in — its clock, its heap, its event
3
+ // loop. `@abide/harness/rows` is the contract the rows keep; this is what writes them.
4
+ // `runBenches` runs an app's benches in its own process, and `harness-bench` is it as a command.
4
5
  //
5
6
  // Not a lane: it depends on bun and on whichever test runner a suite names, and it measures
6
7
  // nothing itself.
7
8
 
8
9
  export { rowsCommand } from './rowsCommand.ts'
10
+ export { runBenches } from './runBenches.ts'
9
11
  export { type TestSuite, testRows } from './testRows.ts'
@@ -0,0 +1,96 @@
1
+ import { type Batched, claimOf, ratio } from '../report/index.ts'
2
+
3
+ const milliseconds = (nanoseconds: number) =>
4
+ `${(nanoseconds / 1e6).toFixed(3)} ms`
5
+
6
+ /**
7
+ * Every bench beside the code, `src/**\/*.bench.ts` under `root`, run in this process one after
8
+ * another: each function a file exports is one bench, and its `CEILINGS` names the most each arm
9
+ * may take, in milliseconds, keyed by bench and then by arm. A ceiling naming no exported bench
10
+ * is reported as a bench of that name with no arms.
11
+ *
12
+ * `only` keeps the files whose path holds it.
13
+ */
14
+ export async function runBenches(
15
+ root: string,
16
+ only?: string,
17
+ ): Promise<
18
+ {
19
+ file: string
20
+ name: string
21
+ case: string
22
+ n: number
23
+ floor: number
24
+ /** Nanoseconds per operation, by arm. */
25
+ arms: Record<string, number>
26
+ /** The last arm over the first, as the harness is allowed to say it; '' for one arm. */
27
+ claim: string
28
+ /** An arm over its ceiling, and a ceiling naming no arm or no bench. */
29
+ over: string[]
30
+ }[]
31
+ > {
32
+ const base = root.endsWith('/') ? root : `${root}/`
33
+ const files = [...new Bun.Glob('src/**/*.bench.ts').scanSync(base)].sort()
34
+ const benches = []
35
+ for (const file of files) {
36
+ if (only !== undefined && !file.includes(only)) continue
37
+ const { CEILINGS = {}, ...exported } = (await import(
38
+ Bun.pathToFileURL(`${base}${file}`).href
39
+ )) as {
40
+ CEILINGS?: Record<string, Record<string, number>>
41
+ } & Record<string, unknown>
42
+ for (const [name, priced] of Object.entries(exported)) {
43
+ if (typeof priced !== 'function') continue
44
+ // `batch` keeps its A/A control out of `samples`, so every sample is an arm.
45
+ const run = (priced as () => Batched)()
46
+ const arms: Record<string, number> = {}
47
+ const names = Object.keys(run.samples)
48
+ for (const arm of names)
49
+ arms[arm] = (
50
+ run.samples[arm] as Batched['samples'][string]
51
+ ).nanoseconds
52
+ const first = run.samples[names[0] ?? '']
53
+ const last = run.samples[names[names.length - 1] ?? '']
54
+ const over: string[] = []
55
+ for (const [arm, most] of Object.entries(CEILINGS[name] ?? {})) {
56
+ const took = arms[arm]
57
+ if (took === undefined)
58
+ over.push(`no arm "${arm}" to hold to its ceiling`)
59
+ else if (took / 1e6 > most)
60
+ over.push(
61
+ `${arm} took ${milliseconds(took)}, over its ceiling of ${most} ms`,
62
+ )
63
+ }
64
+ benches.push({
65
+ file,
66
+ name,
67
+ case: run.case,
68
+ n: run.n,
69
+ floor: run.floor,
70
+ arms,
71
+ claim:
72
+ first && last && first !== last
73
+ ? `${last.arm} against ${first.arm}: ${claimOf(
74
+ ratio(last, first, { floor: run.floor }),
75
+ )}`
76
+ : '',
77
+ over,
78
+ })
79
+ }
80
+ // 43.32 — a ceiling whose bench is not exported holds nothing, so it is reported as a bench
81
+ // with no arms rather than passed over.
82
+ for (const name of Object.keys(CEILINGS))
83
+ if (typeof exported[name] !== 'function')
84
+ benches.push({
85
+ file,
86
+ name,
87
+ case: '',
88
+ n: 0,
89
+ floor: 0,
90
+ arms: {},
91
+ claim: '',
92
+ over: [`no bench "${name}" to hold to its ceiling`],
93
+ })
94
+ }
95
+ return benches
96
+ }
@@ -11,6 +11,7 @@
11
11
  // answer available.
12
12
 
13
13
  import { tmpdir } from 'node:os' // bun has no temporary-directory lookup of its own
14
+
14
15
  import type { TestCase } from '../rows/index.ts'
15
16
 
16
17
  // A RUNNER THAT IS STOPPED STOPS ITS SUITES. SIGTERM reaches this process and nothing under it, so