@yaks/benchmark 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +457 -2
- package/_dist/bench.d.ts +27 -0
- package/_dist/bench.d.ts.map +1 -0
- package/_dist/deno.d.ts +21 -0
- package/_dist/deno.d.ts.map +1 -0
- package/_dist/host.d.ts +42 -0
- package/_dist/host.d.ts.map +1 -0
- package/_dist/mod.d.ts +8 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/process.d.ts +2 -0
- package/_dist/process.d.ts.map +1 -0
- package/_dist/ratchet.d.ts +64 -0
- package/_dist/ratchet.d.ts.map +1 -0
- package/_dist/result.d.ts +103 -0
- package/_dist/result.d.ts.map +1 -0
- package/_dist/runner.d.ts +45 -0
- package/_dist/runner.d.ts.map +1 -0
- package/bench.js +89 -0
- package/bench.js.map +1 -0
- package/bench.ts +103 -0
- package/deno.js +35 -0
- package/deno.js.map +1 -0
- package/deno.json +1 -0
- package/deno.ts +50 -0
- package/host.js +145 -0
- package/host.js.map +1 -0
- package/host.ts +177 -0
- package/invocations.ts +127 -0
- package/mod.js +8 -0
- package/mod.js.map +1 -0
- package/mod.ts +51 -0
- package/package.json +22 -4
- package/process.js +53 -0
- package/process.js.map +1 -0
- package/process.ts +49 -0
- package/ratchet.js +290 -0
- package/ratchet.js.map +1 -0
- package/ratchet.ts +341 -0
- package/result.js +75 -0
- package/result.js.map +1 -0
- package/result.ts +221 -0
- package/runner.js +303 -0
- package/runner.js.map +1 -0
- package/runner.ts +406 -0
- package/rusage.js +25 -0
- package/rusage.js.map +1 -0
- package/rusage.ts +27 -0
package/host.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import * as io from "@yaks/file/io";
|
|
2
|
+
import * as system from "@yaks/process/system";
|
|
3
|
+
import { command as processCommand } from "@yaks/process/command";
|
|
4
|
+
import { layouts, milliseconds, nanos } from "./rusage.js";
|
|
5
|
+
let resolve = ()=>[];
|
|
6
|
+
/** Install the process's reporter factory. It reads the current host config
|
|
7
|
+
* on every run; workload modules neither configure nor construct reporters. */ export let configure = (factory)=>{
|
|
8
|
+
let previous = resolve;
|
|
9
|
+
resolve = factory;
|
|
10
|
+
return ()=>{
|
|
11
|
+
resolve = previous;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export let reporters = (host)=>resolve(host);
|
|
15
|
+
export let host = async ()=>{
|
|
16
|
+
let path = system.env.get('YAK_CONFIG');
|
|
17
|
+
let required = !!path;
|
|
18
|
+
let home = system.env.get('HOME');
|
|
19
|
+
path ??= home ? `${home}/.yak/yak.json` : undefined;
|
|
20
|
+
let config = {};
|
|
21
|
+
let configURL = null;
|
|
22
|
+
if (path) {
|
|
23
|
+
let url = new URL(path, `file://${system.cwd()}/`);
|
|
24
|
+
try {
|
|
25
|
+
let read = JSON.parse(await io.readTextFile(url));
|
|
26
|
+
if (!read || typeof read != 'object' || Array.isArray(read)) {
|
|
27
|
+
throw new Error('Host config must be a JSON object');
|
|
28
|
+
}
|
|
29
|
+
config = read;
|
|
30
|
+
configURL = url.href;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (required || !io.isNotFound(error)) throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
let commit = null;
|
|
36
|
+
try {
|
|
37
|
+
let result = await processCommand('git', {
|
|
38
|
+
args: [
|
|
39
|
+
'rev-parse',
|
|
40
|
+
'HEAD'
|
|
41
|
+
],
|
|
42
|
+
stdout: 'piped',
|
|
43
|
+
stderr: 'null'
|
|
44
|
+
}).output();
|
|
45
|
+
if (result.success) commit = new TextDecoder().decode(result.stdout).trim();
|
|
46
|
+
} catch {}
|
|
47
|
+
return {
|
|
48
|
+
config,
|
|
49
|
+
configURL,
|
|
50
|
+
commit,
|
|
51
|
+
runtime: `${system.runtimeName}/${system.runtimeVersion} ${system.build.target}`,
|
|
52
|
+
cpu: system.cpus()[0]?.model ?? 'unknown',
|
|
53
|
+
load: system.loadavg(),
|
|
54
|
+
busy: ()=>busy()
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
/** The CPU time the box has used in all, in microseconds, from its cgroup (a
|
|
58
|
+
* container's own); undefined where the box does not say. */ let used = ()=>{
|
|
59
|
+
try {
|
|
60
|
+
let n = io.readTextFileSync('/sys/fs/cgroup/cpu.stat').match(/^usage_usec (\d+)/m);
|
|
61
|
+
return n ? Number(n[1]) : undefined;
|
|
62
|
+
} catch {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
/** The share of the box's CPUs in use over the next `ms` while the caller
|
|
67
|
+
* waits: the other work on the box. It is use, not CPU pressure, because a
|
|
68
|
+
* busy hyperthread slows the work on its sibling without making anything wait
|
|
69
|
+
* for a CPU: two of four busy cost a CPU-bound bench about a tenth. */ export let busy = async (ms = 1000)=>{
|
|
70
|
+
let before = used(), start = performance.now();
|
|
71
|
+
await new Promise((done)=>setTimeout(done, ms));
|
|
72
|
+
let after = used(), took = performance.now() - start;
|
|
73
|
+
return before === undefined || after === undefined ? undefined : (after - before) / (took * 1000 * navigator.hardwareConcurrency);
|
|
74
|
+
};
|
|
75
|
+
let clocks;
|
|
76
|
+
let open = ()=>{
|
|
77
|
+
if (clocks) return clocks;
|
|
78
|
+
let layout = layouts[system.os];
|
|
79
|
+
if (!layout) {
|
|
80
|
+
throw new Error(`CPU accounting with child processes is unsupported on ${system.os}`);
|
|
81
|
+
}
|
|
82
|
+
let { symbols } = Deno.dlopen(layout.library, {
|
|
83
|
+
getrusage: {
|
|
84
|
+
parameters: [
|
|
85
|
+
'i32',
|
|
86
|
+
'buffer'
|
|
87
|
+
],
|
|
88
|
+
result: 'i32'
|
|
89
|
+
},
|
|
90
|
+
clock_gettime: {
|
|
91
|
+
parameters: [
|
|
92
|
+
'i32',
|
|
93
|
+
'buffer'
|
|
94
|
+
],
|
|
95
|
+
result: 'i32'
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
// Two 16-byte timevals and fourteen 64-bit longs, on both systems.
|
|
99
|
+
let usage = new Uint8Array(144);
|
|
100
|
+
let view = new DataView(usage.buffer);
|
|
101
|
+
let time = new Uint8Array(16);
|
|
102
|
+
let spec = new DataView(time.buffer);
|
|
103
|
+
return clocks = {
|
|
104
|
+
rusage: (who)=>{
|
|
105
|
+
if (symbols.getrusage(who, usage) != 0) {
|
|
106
|
+
throw new Error(`getrusage(${who}) failed`);
|
|
107
|
+
}
|
|
108
|
+
return milliseconds(view, layout.micros);
|
|
109
|
+
},
|
|
110
|
+
thread: ()=>{
|
|
111
|
+
if (symbols.clock_gettime(layout.thread, time) != 0) {
|
|
112
|
+
throw new Error('clock_gettime(CLOCK_THREAD_CPUTIME_ID) failed');
|
|
113
|
+
}
|
|
114
|
+
return nanos(spec);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
/** CPU milliseconds used by every thread in this process and its finished,
|
|
119
|
+
* waited-for children, user and system, from getrusage(2) on Linux and Darwin.
|
|
120
|
+
* Other platforms fail explicitly: self-only CPU would miss child work.
|
|
121
|
+
* A child counts after it exits and is reaped; a process that outlives a
|
|
122
|
+
* measurement must report its CPU separately. */ export let cputime = ()=>{
|
|
123
|
+
let { rusage } = open();
|
|
124
|
+
return rusage(0) + rusage(-1);
|
|
125
|
+
};
|
|
126
|
+
/** CPU milliseconds used by the calling thread and this process's finished,
|
|
127
|
+
* waited-for children, user and system: `cputime` without the process's other
|
|
128
|
+
* threads, where V8 compiles, collects garbage and runs blocking I/O beside
|
|
129
|
+
* the thread whenever it likes. Two readings bracket the thread's own work. */ export let threadtime = ()=>{
|
|
130
|
+
let { rusage, thread } = open();
|
|
131
|
+
return thread() + rusage(-1);
|
|
132
|
+
};
|
|
133
|
+
/** How long the calling thread has waited for a CPU since it started, in ms:
|
|
134
|
+
* the time other work took from it, as Linux keeps it (schedstat's run delay);
|
|
135
|
+
* undefined where the platform does not say. Two readings bracket what load
|
|
136
|
+
* added to a span of the thread's work, so wall time less their difference is
|
|
137
|
+
* what the span would have taken on an idle box. */ export let waited = ()=>{
|
|
138
|
+
try {
|
|
139
|
+
let delay = io.readTextFileSync('/proc/thread-self/schedstat').split(' ')[1];
|
|
140
|
+
return delay ? Number(delay) / 1e6 : undefined;
|
|
141
|
+
} catch {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
//# sourceMappingURL=host.js.map
|
package/host.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host.js","sources":["./host.ts"],"names":[],"mappings":"AAAA,YAAY,6BAAkC;AAC9C,YAAY,wCAA6C;AACzD,SAAS,WAAW,cAAc,qCAA0C;AAK5E,SAAS,OAAO,EAAE,YAAY,EAAE,KAAK,sBAAqB;AAwB1D,IAAI,UAAqB,IAAM,EAAE;AACjC;6EAC6E,GAC7E,OAAO,IAAI,YAAY,CAAC;EACtB,IAAI,WAAW;EACf,UAAU;EACV,OAAO;IACL,UAAU;EACZ;AACF,EAAC;AACD,OAAO,IAAI,YAAY,CAAC,OAAsC,QAAQ,MAAK;AAE3E,OAAO,IAAI,OAAO;EAChB,IAAI,OAAO,OAAO,GAAG,CAAC,GAAG,CAAC;EAC1B,IAAI,WAAW,CAAC,CAAC;EACjB,IAAI,OAAO,OAAO,GAAG,CAAC,GAAG,CAAC;EAC1B,SAAS,OAAO,GAAG,KAAK,cAAc,CAAC,GAAG;EAC1C,IAAI,SAAkC,CAAC;EACvC,IAAI,YAA2B;EAC/B,IAAI,MAAM;IACR,IAAI,MAAM,IAAI,IAAI,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,GAAG,CAAC,CAAC;IACjD,IAAI;MACF,IAAI,OAAgB,KAAK,KAAK,CAAC,MAAM,GAAG,YAAY,CAAC;MACrD,IAAI,CAAC,QAAQ,OAAO,QAAQ,YAAY,MAAM,OAAO,CAAC,OAAO;QAC3D,MAAM,IAAI,MAAM;MACl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AO;EACT;AACF,EAAC"}
|
package/host.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import * as io from '@yaks/file/io'
|
|
2
|
+
import * as system from '@yaks/process/system'
|
|
3
|
+
import { command as processCommand } from '@yaks/process/command'
|
|
4
|
+
/** The process boundary: host config and machine metadata are read for each
|
|
5
|
+
* run, not when a bench is declared. Reporter factories receive this context;
|
|
6
|
+
* the default factory is empty until a reporting provider is installed. */
|
|
7
|
+
import type { Run } from './result.ts'
|
|
8
|
+
import { layouts, milliseconds, nanos } from './rusage.js'
|
|
9
|
+
|
|
10
|
+
export type Host = {
|
|
11
|
+
config: Record<string, unknown>
|
|
12
|
+
/** Config-relative provider paths are resolved against this URL. */
|
|
13
|
+
configURL: string | null
|
|
14
|
+
commit: string | null
|
|
15
|
+
runtime: string
|
|
16
|
+
cpu: string
|
|
17
|
+
load: readonly number[]
|
|
18
|
+
/** The share of the box's CPUs in use over a second the bench rests; absent
|
|
19
|
+
* or undefined where the box does not say. The runner samples it before
|
|
20
|
+
* each round (`Run.busy`). */
|
|
21
|
+
busy?: () => Promise<number | undefined>
|
|
22
|
+
}
|
|
23
|
+
type Immutable<T> = T extends object
|
|
24
|
+
? { readonly [K in keyof T]: Immutable<T[K]> }
|
|
25
|
+
: T
|
|
26
|
+
export type ReportedRun = Immutable<Run>
|
|
27
|
+
export type Reporter = (run: ReportedRun) => void | Promise<void>
|
|
28
|
+
export type Reporters = (
|
|
29
|
+
host: Host,
|
|
30
|
+
) => readonly Reporter[] | Promise<readonly Reporter[]>
|
|
31
|
+
|
|
32
|
+
let resolve: Reporters = () => []
|
|
33
|
+
/** Install the process's reporter factory. It reads the current host config
|
|
34
|
+
* on every run; workload modules neither configure nor construct reporters. */
|
|
35
|
+
export let configure = (factory: Reporters): () => void => {
|
|
36
|
+
let previous = resolve
|
|
37
|
+
resolve = factory
|
|
38
|
+
return () => {
|
|
39
|
+
resolve = previous
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export let reporters = (host: Host): ReturnType<Reporters> => resolve(host)
|
|
43
|
+
|
|
44
|
+
export let host = async (): Promise<Host> => {
|
|
45
|
+
let path = system.env.get('YAK_CONFIG')
|
|
46
|
+
let required = !!path
|
|
47
|
+
let home = system.env.get('HOME')
|
|
48
|
+
path ??= home ? `${home}/.yak/yak.json` : undefined
|
|
49
|
+
let config: Record<string, unknown> = {}
|
|
50
|
+
let configURL: string | null = null
|
|
51
|
+
if (path) {
|
|
52
|
+
let url = new URL(path, `file://${system.cwd()}/`)
|
|
53
|
+
try {
|
|
54
|
+
let read: unknown = JSON.parse(await io.readTextFile(url))
|
|
55
|
+
if (!read || typeof read != 'object' || Array.isArray(read)) {
|
|
56
|
+
throw new Error('Host config must be a JSON object')
|
|
57
|
+
}
|
|
58
|
+
config = read as Record<string, unknown>
|
|
59
|
+
configURL = url.href
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (required || !io.isNotFound(error)) throw error
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
let commit: string | null = null
|
|
65
|
+
try {
|
|
66
|
+
let result = await processCommand('git', {
|
|
67
|
+
args: ['rev-parse', 'HEAD'],
|
|
68
|
+
stdout: 'piped',
|
|
69
|
+
stderr: 'null',
|
|
70
|
+
}).output()
|
|
71
|
+
if (result.success) commit = new TextDecoder().decode(result.stdout).trim()
|
|
72
|
+
} catch { /* A workload can run outside a Git checkout. */ }
|
|
73
|
+
return {
|
|
74
|
+
config,
|
|
75
|
+
configURL,
|
|
76
|
+
commit,
|
|
77
|
+
runtime:
|
|
78
|
+
`${system.runtimeName}/${system.runtimeVersion} ${system.build.target}`,
|
|
79
|
+
cpu: system.cpus()[0]?.model ?? 'unknown',
|
|
80
|
+
load: system.loadavg(),
|
|
81
|
+
busy: () => busy(),
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The CPU time the box has used in all, in microseconds, from its cgroup (a
|
|
86
|
+
* container's own); undefined where the box does not say. */
|
|
87
|
+
let used = (): number | undefined => {
|
|
88
|
+
try {
|
|
89
|
+
let n = io.readTextFileSync('/sys/fs/cgroup/cpu.stat')
|
|
90
|
+
.match(/^usage_usec (\d+)/m)
|
|
91
|
+
return n ? Number(n[1]) : undefined
|
|
92
|
+
} catch {
|
|
93
|
+
return undefined
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The share of the box's CPUs in use over the next `ms` while the caller
|
|
98
|
+
* waits: the other work on the box. It is use, not CPU pressure, because a
|
|
99
|
+
* busy hyperthread slows the work on its sibling without making anything wait
|
|
100
|
+
* for a CPU: two of four busy cost a CPU-bound bench about a tenth. */
|
|
101
|
+
export let busy = async (ms = 1000): Promise<number | undefined> => {
|
|
102
|
+
let before = used(), start = performance.now()
|
|
103
|
+
await new Promise((done) => setTimeout(done, ms))
|
|
104
|
+
let after = used(), took = performance.now() - start
|
|
105
|
+
return before === undefined || after === undefined
|
|
106
|
+
? undefined
|
|
107
|
+
: (after - before) / (took * 1000 * navigator.hardwareConcurrency)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
type Clocks = { rusage: (who: number) => number; thread: () => number }
|
|
111
|
+
let clocks: Clocks | undefined
|
|
112
|
+
let open = (): Clocks => {
|
|
113
|
+
if (clocks) return clocks
|
|
114
|
+
let layout = layouts[system.os]
|
|
115
|
+
if (!layout) {
|
|
116
|
+
throw new Error(
|
|
117
|
+
`CPU accounting with child processes is unsupported on ${system.os}`,
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
let { symbols } = Deno.dlopen(layout.library, {
|
|
121
|
+
getrusage: { parameters: ['i32', 'buffer'], result: 'i32' },
|
|
122
|
+
clock_gettime: { parameters: ['i32', 'buffer'], result: 'i32' },
|
|
123
|
+
})
|
|
124
|
+
// Two 16-byte timevals and fourteen 64-bit longs, on both systems.
|
|
125
|
+
let usage = new Uint8Array(144)
|
|
126
|
+
let view = new DataView(usage.buffer)
|
|
127
|
+
let time = new Uint8Array(16)
|
|
128
|
+
let spec = new DataView(time.buffer)
|
|
129
|
+
return clocks = {
|
|
130
|
+
rusage: (who) => {
|
|
131
|
+
if (symbols.getrusage(who, usage) != 0) {
|
|
132
|
+
throw new Error(`getrusage(${who}) failed`)
|
|
133
|
+
}
|
|
134
|
+
return milliseconds(view, layout.micros)
|
|
135
|
+
},
|
|
136
|
+
thread: () => {
|
|
137
|
+
if (symbols.clock_gettime(layout.thread, time) != 0) {
|
|
138
|
+
throw new Error('clock_gettime(CLOCK_THREAD_CPUTIME_ID) failed')
|
|
139
|
+
}
|
|
140
|
+
return nanos(spec)
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** CPU milliseconds used by every thread in this process and its finished,
|
|
146
|
+
* waited-for children, user and system, from getrusage(2) on Linux and Darwin.
|
|
147
|
+
* Other platforms fail explicitly: self-only CPU would miss child work.
|
|
148
|
+
* A child counts after it exits and is reaped; a process that outlives a
|
|
149
|
+
* measurement must report its CPU separately. */
|
|
150
|
+
export let cputime = (): number => {
|
|
151
|
+
let { rusage } = open()
|
|
152
|
+
return rusage(0) + rusage(-1)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** CPU milliseconds used by the calling thread and this process's finished,
|
|
156
|
+
* waited-for children, user and system: `cputime` without the process's other
|
|
157
|
+
* threads, where V8 compiles, collects garbage and runs blocking I/O beside
|
|
158
|
+
* the thread whenever it likes. Two readings bracket the thread's own work. */
|
|
159
|
+
export let threadtime = (): number => {
|
|
160
|
+
let { rusage, thread } = open()
|
|
161
|
+
return thread() + rusage(-1)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** How long the calling thread has waited for a CPU since it started, in ms:
|
|
165
|
+
* the time other work took from it, as Linux keeps it (schedstat's run delay);
|
|
166
|
+
* undefined where the platform does not say. Two readings bracket what load
|
|
167
|
+
* added to a span of the thread's work, so wall time less their difference is
|
|
168
|
+
* what the span would have taken on an idle box. */
|
|
169
|
+
export let waited = (): number | undefined => {
|
|
170
|
+
try {
|
|
171
|
+
let delay = io.readTextFileSync('/proc/thread-self/schedstat')
|
|
172
|
+
.split(' ')[1]
|
|
173
|
+
return delay ? Number(delay) / 1e6 : undefined
|
|
174
|
+
} catch {
|
|
175
|
+
return undefined
|
|
176
|
+
}
|
|
177
|
+
}
|
package/invocations.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/** Deno workload declarations, measured with one CPU clock for the whole
|
|
2
|
+
* batch. Explicit start/end boundaries exclude fixture setup and teardown. */
|
|
3
|
+
import { stopwatch } from './bench.ts'
|
|
4
|
+
import type { Sampling } from './result.ts'
|
|
5
|
+
|
|
6
|
+
export type Invocation = Pick<
|
|
7
|
+
Deno.BenchDefinition,
|
|
8
|
+
'name' | 'fn' | 'n' | 'warmup'
|
|
9
|
+
>
|
|
10
|
+
type Reading = { value: number; cpu: number }
|
|
11
|
+
type Timer = () => () => Reading
|
|
12
|
+
|
|
13
|
+
export let invocation = async (
|
|
14
|
+
entry: Invocation,
|
|
15
|
+
timer: Timer = stopwatch,
|
|
16
|
+
minimum = 200,
|
|
17
|
+
): Promise<{
|
|
18
|
+
avg: number
|
|
19
|
+
cpu: number
|
|
20
|
+
n: number
|
|
21
|
+
cpuSamples: number[]
|
|
22
|
+
sampling?: Sampling
|
|
23
|
+
}> => {
|
|
24
|
+
let batch = async (n: number): Promise<Reading & { outer: number }> => {
|
|
25
|
+
let explicit = false, running = false
|
|
26
|
+
let sum = { value: 0, cpu: 0 }
|
|
27
|
+
let stop: () => Reading
|
|
28
|
+
let context = {
|
|
29
|
+
name: entry.name,
|
|
30
|
+
origin: '',
|
|
31
|
+
start: () => {
|
|
32
|
+
if (running) throw new Error('Benchmark timer already started')
|
|
33
|
+
explicit = running = true
|
|
34
|
+
stop = timer()
|
|
35
|
+
},
|
|
36
|
+
end: () => {
|
|
37
|
+
if (!running) throw new Error('Benchmark timer was not started')
|
|
38
|
+
let reading = stop()
|
|
39
|
+
sum.value += reading.value
|
|
40
|
+
sum.cpu += reading.cpu
|
|
41
|
+
running = false
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
let elapsed = timer()
|
|
45
|
+
for (let i = 0; i < n; i++) {
|
|
46
|
+
let result = entry.fn(context)
|
|
47
|
+
if (result && typeof result.then == 'function') await result
|
|
48
|
+
if (running) context.end()
|
|
49
|
+
}
|
|
50
|
+
let total = elapsed()
|
|
51
|
+
return { ...(explicit ? sum : total), outer: total.cpu }
|
|
52
|
+
}
|
|
53
|
+
await batch(entry.warmup ?? 5)
|
|
54
|
+
let n = Math.max(1, entry.n ?? 1)
|
|
55
|
+
let reading: Reading & { outer: number }
|
|
56
|
+
let sampling: Sampling | undefined
|
|
57
|
+
if (entry.n === undefined) {
|
|
58
|
+
let outer = 0, calibrated = false
|
|
59
|
+
let budget = minimum * 5
|
|
60
|
+
while (true) {
|
|
61
|
+
reading = await batch(n)
|
|
62
|
+
outer += reading.outer
|
|
63
|
+
sampling = {
|
|
64
|
+
status: 'insufficient',
|
|
65
|
+
minimum,
|
|
66
|
+
measured: reading.cpu,
|
|
67
|
+
outer,
|
|
68
|
+
}
|
|
69
|
+
if (reading.cpu >= minimum) {
|
|
70
|
+
// Discard calibration, then verify the retained batch itself. A GC
|
|
71
|
+
// outlier during calibration cannot leave a tiny final batch valid.
|
|
72
|
+
if (calibrated) {
|
|
73
|
+
sampling = undefined
|
|
74
|
+
break
|
|
75
|
+
}
|
|
76
|
+
calibrated = true
|
|
77
|
+
continue
|
|
78
|
+
}
|
|
79
|
+
// Setup CPU limits growth but cannot stand in for measured work.
|
|
80
|
+
let scale = Math.max(
|
|
81
|
+
2,
|
|
82
|
+
Math.min(10, Math.ceil(minimum * 1.25 / (reading.cpu || .001))),
|
|
83
|
+
)
|
|
84
|
+
let allowed = Math.floor(
|
|
85
|
+
Math.max(0, budget - outer) * n / (reading.outer || .001),
|
|
86
|
+
)
|
|
87
|
+
let next = Math.min(10_000_000, n * scale, allowed)
|
|
88
|
+
if (next <= n) break
|
|
89
|
+
n = next
|
|
90
|
+
}
|
|
91
|
+
} else reading = await batch(n)
|
|
92
|
+
// A declared n is the total measured invocation count. Paired rounds own
|
|
93
|
+
// repeated observations; this collector never multiplies that contract.
|
|
94
|
+
let cpu = reading!.cpu * 1e6 / n
|
|
95
|
+
return {
|
|
96
|
+
avg: reading!.value * 1e6 / n,
|
|
97
|
+
cpu,
|
|
98
|
+
n,
|
|
99
|
+
cpuSamples: [cpu],
|
|
100
|
+
...sampling ? { sampling } : {},
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Install only at the isolated collector's process boundary. Workload files
|
|
105
|
+
* retain Deno.bench's declarations and context without using its wall timer. */
|
|
106
|
+
export let declarations = (): Invocation[] => {
|
|
107
|
+
let entries: Invocation[] = []
|
|
108
|
+
Deno.bench = ((
|
|
109
|
+
first: string | Deno.BenchDefinition | Deno.BenchDefinition['fn'],
|
|
110
|
+
second?: Partial<Deno.BenchDefinition> | Deno.BenchDefinition['fn'],
|
|
111
|
+
third?: Deno.BenchDefinition['fn'],
|
|
112
|
+
) => {
|
|
113
|
+
let definition = typeof first == 'object'
|
|
114
|
+
? first
|
|
115
|
+
: typeof first == 'function'
|
|
116
|
+
? { name: first.name, fn: first }
|
|
117
|
+
: {
|
|
118
|
+
...typeof second == 'object' ? second : {},
|
|
119
|
+
name: first,
|
|
120
|
+
fn: typeof second == 'function' ? second : third,
|
|
121
|
+
}
|
|
122
|
+
if (!definition.fn) throw new Error('Missing benchmark function')
|
|
123
|
+
if (definition.ignore) return
|
|
124
|
+
entries.push(definition as Invocation)
|
|
125
|
+
}) as typeof Deno.bench
|
|
126
|
+
return entries
|
|
127
|
+
}
|
package/mod.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Named workloads and their measurements, with a pure ratchet and a JSON
|
|
2
|
+
* runner. Benches know their work and unit; the process owns reporting. */ export { bench, stopwatch } from "./bench.js";
|
|
3
|
+
export { insufficient, median } from "./result.js";
|
|
4
|
+
export { baseline, best, compare, incomparable, limit, loaded, spread } from "./ratchet.js";
|
|
5
|
+
export { busy, configure, cputime, host, threadtime, waited } from "./host.js";
|
|
6
|
+
export { Regressed, run } from "./runner.js";
|
|
7
|
+
export { processCpu } from "./process.js";
|
|
8
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;yEACyE,GACzE,SACE,KAAK,EAEL,SAAS,qBAGQ;AACnB,SAKE,YAAY,EACZ,MAAM,sBAUY;AACpB,SACE,QAAQ,EACR,IAAI,EACJ,OAAO,EAEP,YAAY,EACZ,KAAK,EACL,MAAM,EACN,MAAM,uBACa;AACrB,SACE,IAAI,EACJ,SAAS,EACT,OAAO,EAEP,IAAI,EAIJ,UAAU,EACV,MAAM,oBACU;AAClB,SAAmC,SAAS,EAAE,GAAG,sBAAqB;AAEtE,SAAS,UAAU,uBAAsB"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** Named workloads and their measurements, with a pure ratchet and a JSON
|
|
2
|
+
* runner. Benches know their work and unit; the process owns reporting. */
|
|
3
|
+
export {
|
|
4
|
+
bench,
|
|
5
|
+
type Iteration,
|
|
6
|
+
stopwatch,
|
|
7
|
+
type TimedBench,
|
|
8
|
+
type TimeUnit,
|
|
9
|
+
} from './bench.js'
|
|
10
|
+
export {
|
|
11
|
+
type Baseline,
|
|
12
|
+
type Bench,
|
|
13
|
+
type CollectedSuite,
|
|
14
|
+
type Direction,
|
|
15
|
+
insufficient,
|
|
16
|
+
median,
|
|
17
|
+
type Regression,
|
|
18
|
+
type Result,
|
|
19
|
+
type Round,
|
|
20
|
+
type Run,
|
|
21
|
+
type Sample,
|
|
22
|
+
type Samples,
|
|
23
|
+
type Sampling,
|
|
24
|
+
type Suite,
|
|
25
|
+
type Workload,
|
|
26
|
+
} from './result.js'
|
|
27
|
+
export {
|
|
28
|
+
baseline,
|
|
29
|
+
best,
|
|
30
|
+
compare,
|
|
31
|
+
type Coverage,
|
|
32
|
+
incomparable,
|
|
33
|
+
limit,
|
|
34
|
+
loaded,
|
|
35
|
+
spread,
|
|
36
|
+
} from './ratchet.js'
|
|
37
|
+
export {
|
|
38
|
+
busy,
|
|
39
|
+
configure,
|
|
40
|
+
cputime,
|
|
41
|
+
type Host,
|
|
42
|
+
host,
|
|
43
|
+
type ReportedRun,
|
|
44
|
+
type Reporter,
|
|
45
|
+
type Reporters,
|
|
46
|
+
threadtime,
|
|
47
|
+
waited,
|
|
48
|
+
} from './host.js'
|
|
49
|
+
export { type Files, type Options, Regressed, run } from './runner.js'
|
|
50
|
+
|
|
51
|
+
export { processCpu } from './process.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/benchmark",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/benchmark",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
},
|
|
11
|
+
"./deno": {
|
|
12
|
+
"types": "./_dist/deno.d.ts",
|
|
13
|
+
"default": "./deno.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"license": "Apache-2.0",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@yaks/file": "0.2.36",
|
|
19
|
+
"@yaks/process": "0.2.36",
|
|
20
|
+
"@yaks/trace": "0.2.36"
|
|
21
|
+
},
|
|
22
|
+
"description": "Named workloads, raw samples, JSON runs and explicit baseline ratchets, with process-configured reporters.",
|
|
23
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
24
|
+
}
|
package/process.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as io from "@yaks/file/io";
|
|
2
|
+
import * as system from "@yaks/process/system";
|
|
3
|
+
/** CPU used by an external process tree. Process CPU clocks include every
|
|
4
|
+
* thread at nanosecond precision; proc supplies descendants and reaped work. */ let library = ()=>Deno.dlopen('libc.so.6', {
|
|
5
|
+
clock_gettime: {
|
|
6
|
+
parameters: [
|
|
7
|
+
'i32',
|
|
8
|
+
'buffer'
|
|
9
|
+
],
|
|
10
|
+
result: 'i32'
|
|
11
|
+
},
|
|
12
|
+
sysconf: {
|
|
13
|
+
parameters: [
|
|
14
|
+
'i32'
|
|
15
|
+
],
|
|
16
|
+
result: 'i64'
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
let clocks;
|
|
20
|
+
let times = new BigInt64Array(2);
|
|
21
|
+
export let processCpu = (pid)=>{
|
|
22
|
+
if (system.os != 'linux') throw new Error('Process CPU clocks need Linux');
|
|
23
|
+
clocks ??= library();
|
|
24
|
+
let ticks = Number(clocks.symbols.sysconf(2));
|
|
25
|
+
let seen = new Set();
|
|
26
|
+
let read = (id)=>{
|
|
27
|
+
if (seen.has(id)) return 0;
|
|
28
|
+
seen.add(id);
|
|
29
|
+
let stat;
|
|
30
|
+
try {
|
|
31
|
+
stat = io.readTextFileSync(`/proc/${id}/stat`);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (io.isNotFound(error)) return 0;
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
let fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
|
|
37
|
+
let result = clocks.symbols.clock_gettime(~id << 3 | 2, times);
|
|
38
|
+
let own = result == 0 ? Number(times[0]) * 1000 + Number(times[1]) / 1e6 : (Number(fields[11]) + Number(fields[12])) * 1000 / ticks;
|
|
39
|
+
let children = new Set();
|
|
40
|
+
for (let task of io.readDirSync(`/proc/${id}/task`)){
|
|
41
|
+
try {
|
|
42
|
+
for (let child of io.readTextFileSync(`/proc/${id}/task/${task.name}/children`).trim().split(/\s+/).filter(Boolean))children.add(Number(child));
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (!io.isNotFound(error)) throw error;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return own + (Number(fields[13]) + Number(fields[14])) * 1000 / ticks + [
|
|
48
|
+
...children
|
|
49
|
+
].reduce((total, child)=>total + read(child), 0);
|
|
50
|
+
};
|
|
51
|
+
return read(pid);
|
|
52
|
+
};
|
|
53
|
+
//# sourceMappingURL=process.js.map
|
package/process.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.js","sources":["./process.ts"],"names":[],"mappings":"AAAA,YAAY,6BAAkC;AAC9C,YAAY,wCAA6C;AACzD;8EAC8E,GAC9E,IAAI,UAAU,IACZ,KAAK,MAAM,CAAC,aAAa;IACvB,eAAe;MAAE,YAAY;QAAC;QAAO;OAAS;MAAE,QAAQ;IAAM;IAC9D,SAAS;MAAE,YAAY;QAAC;OAAM;MAAE,QAAQ;IAAM;EAChD;AACF,IAAI;AACJ,IAAI,QAAQ,IAAI,cAAc;AAE9B,OAAO,IAAI,aAAa,CAAC;EACvB,IAAI,OAAO,EAAE,IAAI,SAAS,MAAM,IAAI,MAAM;EAC1C,WAAW;EACX,IAAI,QAAQ,OAAO,OAAO,OAAO,CAAC,OAAO,CAAC;EAC1C,IAAI,OAAO,IAAI;EACf,IAAI,OAAO,CAAC;IACV,IAAI,KAAK,GAAG,CAAC,KAAK,OAAO;IACzB,KAAK,GAAG,CAAC;IACT,IAAI;IACJ,IAAI;MACF,OAAO,GAAG,gBAAgB,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC;IAC/C,EAAE,OAAO,OAAO;MACd,IAAI,GAAG,UAAU,CAAC,QAAQ,OAAO;MACjC,MAAM;IACR;IACA,IAAI,SAAS,KAAK,KAAK,CAAC,KAAK,WAAW,CAAC,OAAO,GAAG,KAAK,CAAC;IACzD,IAAI,SAAS,OAAQ,OAAO,CAAC,aAAa,CAAC,AAAC,CAAC,MAAM,IAAK,GAAG;IAC3D,IAAI,MAAM,UAAU,IAChB,OAAO,KAAK,CAAC,EAAE,IAAI,OAAO,OAAO,KAAK,CAAC,EAAE,IAAI,MAC7C,CAAC,OAAO,MAAM,CAAC,GAAG,IAAI,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,OAAO;IACvD,IAAI,WAAW,IAAI;IACnB,KAAK,IAAI,QAAQ,GAAG,WAAW,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,EAAG;MACnD,IAAI;QACF,KACE,IAAI,SAAS,GAAG,gBAAgB,CAC9B,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,KAAK,IAAI,CAAC,SAAS,CAAC,EACxC,IAAI,GAAG,KAAK,CAAC,OAAO,MAAM,CAAC,SAC7B,SAAS,GAAG,CAAC,OAAO;MACxB,EAAE,OAAO,OAAO;QACd,IAAI,CAAC,GAAG,UAAU,CAAC,QAAQ,MAAM;MACnC;IACF;IACA,OAAO,MAAM,CAAC,OAAO,MAAM,CAAC,GAAG,IAAI,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,OAAO,QAC9D;SAAI;KAAS,CAAC,MAAM,CAAC,CAAC,OAAO,QAAU,QAAQ,KAAK,QAAQ;EAChE;EACA,OAAO,KAAK;AACd,EAAC"}
|
package/process.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as io from '@yaks/file/io'
|
|
2
|
+
import * as system from '@yaks/process/system'
|
|
3
|
+
/** CPU used by an external process tree. Process CPU clocks include every
|
|
4
|
+
* thread at nanosecond precision; proc supplies descendants and reaped work. */
|
|
5
|
+
let library = () =>
|
|
6
|
+
Deno.dlopen('libc.so.6', {
|
|
7
|
+
clock_gettime: { parameters: ['i32', 'buffer'], result: 'i32' },
|
|
8
|
+
sysconf: { parameters: ['i32'], result: 'i64' },
|
|
9
|
+
})
|
|
10
|
+
let clocks: ReturnType<typeof library> | undefined
|
|
11
|
+
let times = new BigInt64Array(2)
|
|
12
|
+
|
|
13
|
+
export let processCpu = (pid: number): number => {
|
|
14
|
+
if (system.os != 'linux') throw new Error('Process CPU clocks need Linux')
|
|
15
|
+
clocks ??= library()
|
|
16
|
+
let ticks = Number(clocks.symbols.sysconf(2))
|
|
17
|
+
let seen = new Set<number>()
|
|
18
|
+
let read = (id: number): number => {
|
|
19
|
+
if (seen.has(id)) return 0
|
|
20
|
+
seen.add(id)
|
|
21
|
+
let stat: string
|
|
22
|
+
try {
|
|
23
|
+
stat = io.readTextFileSync(`/proc/${id}/stat`)
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (io.isNotFound(error)) return 0
|
|
26
|
+
throw error
|
|
27
|
+
}
|
|
28
|
+
let fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ')
|
|
29
|
+
let result = clocks!.symbols.clock_gettime((~id << 3) | 2, times)
|
|
30
|
+
let own = result == 0
|
|
31
|
+
? Number(times[0]) * 1000 + Number(times[1]) / 1e6
|
|
32
|
+
: (Number(fields[11]) + Number(fields[12])) * 1000 / ticks
|
|
33
|
+
let children = new Set<number>()
|
|
34
|
+
for (let task of io.readDirSync(`/proc/${id}/task`)) {
|
|
35
|
+
try {
|
|
36
|
+
for (
|
|
37
|
+
let child of io.readTextFileSync(
|
|
38
|
+
`/proc/${id}/task/${task.name}/children`,
|
|
39
|
+
).trim().split(/\s+/).filter(Boolean)
|
|
40
|
+
) children.add(Number(child))
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (!io.isNotFound(error)) throw error
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return own + (Number(fields[13]) + Number(fields[14])) * 1000 / ticks +
|
|
46
|
+
[...children].reduce((total, child) => total + read(child), 0)
|
|
47
|
+
}
|
|
48
|
+
return read(pid)
|
|
49
|
+
}
|