@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/runner.ts
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import * as io from '@yaks/file/io'
|
|
2
|
+
import * as system from '@yaks/process/system'
|
|
3
|
+
/** Run workloads sequentially, persist JSON before reporting, and accept one
|
|
4
|
+
* suite's baseline only on request. External collectors use the same ratchet
|
|
5
|
+
* and reporting path as timed callbacks. */
|
|
6
|
+
import { type Host, host, type Reporters, reporters } from './host.js'
|
|
7
|
+
import { dirname, resolve } from 'node:path'
|
|
8
|
+
import {
|
|
9
|
+
type Baseline,
|
|
10
|
+
type CollectedSuite,
|
|
11
|
+
insufficient,
|
|
12
|
+
median,
|
|
13
|
+
names,
|
|
14
|
+
type Result,
|
|
15
|
+
type Run,
|
|
16
|
+
sameNames,
|
|
17
|
+
samples,
|
|
18
|
+
type Suite,
|
|
19
|
+
workload,
|
|
20
|
+
} from './result.js'
|
|
21
|
+
import {
|
|
22
|
+
baseline,
|
|
23
|
+
best,
|
|
24
|
+
compare,
|
|
25
|
+
type Coverage,
|
|
26
|
+
covered,
|
|
27
|
+
incomparable,
|
|
28
|
+
loaded,
|
|
29
|
+
rebased,
|
|
30
|
+
spread,
|
|
31
|
+
tolerance,
|
|
32
|
+
validate,
|
|
33
|
+
} from './ratchet.js'
|
|
34
|
+
|
|
35
|
+
export type Files = {
|
|
36
|
+
read: (path: string) => Promise<string>
|
|
37
|
+
write: (path: string, value: unknown) => Promise<void>
|
|
38
|
+
}
|
|
39
|
+
/** An interrupted write never leaves a partial result or baseline. */
|
|
40
|
+
export let files: Files = {
|
|
41
|
+
read: (path) => io.readTextFile(path),
|
|
42
|
+
write: async (path, value) => {
|
|
43
|
+
let tmp = `${path}.${crypto.randomUUID()}.tmp`
|
|
44
|
+
try {
|
|
45
|
+
await io.mkdir(dirname(path), { recursive: true })
|
|
46
|
+
await io.writeTextFile(tmp, JSON.stringify(value, null, 2) + '\n')
|
|
47
|
+
await io.rename(tmp, path)
|
|
48
|
+
} catch (error) {
|
|
49
|
+
try {
|
|
50
|
+
await io.remove(tmp)
|
|
51
|
+
} catch (cleanup) {
|
|
52
|
+
if (!io.isNotFound(cleanup)) {
|
|
53
|
+
console.error('Benchmark temporary file cleanup failed', cleanup)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
throw error
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
export type Options = {
|
|
61
|
+
rounds?: number
|
|
62
|
+
/** Judge CPU time; wall readings remain in raw samples for diagnostics. */
|
|
63
|
+
figure?: 'cpu'
|
|
64
|
+
mode?: 'run' | 'check' | 'accept'
|
|
65
|
+
output: string
|
|
66
|
+
baseline?: string
|
|
67
|
+
/** Where a check keeps this box's own baseline while `baseline` was banked
|
|
68
|
+
* on another runtime or CPU. The first such check banks its run there, and
|
|
69
|
+
* later checks on this runtime and CPU compare with it. */
|
|
70
|
+
local?: string
|
|
71
|
+
/** Required for acceptance: the least slack any bench is banked with.
|
|
72
|
+
* Checks use the baseline's own. */
|
|
73
|
+
tolerance?: number
|
|
74
|
+
/** Measure the suite this many times over, keeping each bench's best median
|
|
75
|
+
* and the spread of every round between them (ratchet.ts `best`).
|
|
76
|
+
* Acceptance banks it with the slack that spread asks for (ratchet.ts
|
|
77
|
+
* `baseline`), and a check compares it, allowing that spread as well, so a
|
|
78
|
+
* check taken as its baseline was banked measures what it banked. */
|
|
79
|
+
runs?: number
|
|
80
|
+
/** Bank the measurement as it is, even a worse median than the baseline's:
|
|
81
|
+
* every bench's, or only the named benches'. Without it acceptance only
|
|
82
|
+
* moves a bench toward better, by more than the noise. */
|
|
83
|
+
slower?: boolean | readonly string[]
|
|
84
|
+
host?: () => Host | Promise<Host>
|
|
85
|
+
reporters?: Reporters
|
|
86
|
+
files?: Files
|
|
87
|
+
/** A box-wide flock, held for each round and publication, so queued suites
|
|
88
|
+
* can measure between rounds. Compatible with shell flock; never unlink it. */
|
|
89
|
+
lock?: string
|
|
90
|
+
open?: typeof io.open
|
|
91
|
+
/** Scoped observations may measure only part of a suite, including new work.
|
|
92
|
+
* Unbanked measurements are reported as measured; acceptance retains the
|
|
93
|
+
* other workloads in the suite's baseline. Exact coverage is the default. */
|
|
94
|
+
coverage?: Coverage
|
|
95
|
+
}
|
|
96
|
+
export class Regressed extends Error {
|
|
97
|
+
constructor(public result: Run) {
|
|
98
|
+
super(`${result.regressions.length} benchmarks exceeded tolerance`)
|
|
99
|
+
this.name = 'Regressed'
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export let run = async (
|
|
104
|
+
suite: Suite | CollectedSuite,
|
|
105
|
+
options: Options,
|
|
106
|
+
): Promise<Run> => {
|
|
107
|
+
try {
|
|
108
|
+
if (!options.lock) return await execute(suite, options)
|
|
109
|
+
using lock = await (options.open ?? io.open)(options.lock, {
|
|
110
|
+
create: true,
|
|
111
|
+
read: true,
|
|
112
|
+
write: true,
|
|
113
|
+
})
|
|
114
|
+
let exclusive: Exclusive = async (work) => {
|
|
115
|
+
await lock.lock(true)
|
|
116
|
+
try {
|
|
117
|
+
await lock.truncate(0)
|
|
118
|
+
await lock.write(new TextEncoder().encode(`${system.pid}\n`))
|
|
119
|
+
return await work()
|
|
120
|
+
} finally {
|
|
121
|
+
await lock.unlock()
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return await execute(suite, options, exclusive)
|
|
125
|
+
} finally {
|
|
126
|
+
await suite.close?.()
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
type Exclusive = <T>(work: () => Promise<T>) => Promise<T>
|
|
131
|
+
let direct: Exclusive = (work) => work()
|
|
132
|
+
|
|
133
|
+
let execute = async (
|
|
134
|
+
suite: Suite | CollectedSuite,
|
|
135
|
+
options: Options,
|
|
136
|
+
exclusive: Exclusive = direct,
|
|
137
|
+
): Promise<Run> => {
|
|
138
|
+
let rounds = options.rounds ?? 3
|
|
139
|
+
let mode = options.mode ?? 'run'
|
|
140
|
+
if (!Number.isSafeInteger(rounds) || rounds < 1) {
|
|
141
|
+
throw new Error('Expected a positive integer round count')
|
|
142
|
+
}
|
|
143
|
+
if (!['run', 'check', 'accept'].includes(mode)) {
|
|
144
|
+
throw new Error(`Unknown mode: ${mode}`)
|
|
145
|
+
}
|
|
146
|
+
if (options.coverage && !['exact', 'subset'].includes(options.coverage)) {
|
|
147
|
+
throw new Error(`Unknown benchmark coverage: ${options.coverage}`)
|
|
148
|
+
}
|
|
149
|
+
if (
|
|
150
|
+
typeof suite.name != 'string' || !suite.name ||
|
|
151
|
+
typeof suite.metric != 'string' || !suite.metric ||
|
|
152
|
+
!['string', 'number'].includes(typeof suite.workload)
|
|
153
|
+
) throw new Error('Invalid suite metadata')
|
|
154
|
+
workload(suite.workload)
|
|
155
|
+
names(suite.benches)
|
|
156
|
+
if (
|
|
157
|
+
!options.output ||
|
|
158
|
+
(options.baseline && resolve(options.output) == resolve(options.baseline))
|
|
159
|
+
) {
|
|
160
|
+
throw new Error('Result and baseline need separate paths')
|
|
161
|
+
}
|
|
162
|
+
if (options.baseline && !options.files) {
|
|
163
|
+
try {
|
|
164
|
+
let [output, base] = await Promise.all([
|
|
165
|
+
io.realPath(options.output),
|
|
166
|
+
io.realPath(options.baseline),
|
|
167
|
+
])
|
|
168
|
+
if (output == base) {
|
|
169
|
+
throw new Error('Result and baseline need separate paths')
|
|
170
|
+
}
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (!io.isNotFound(error)) throw error
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (mode != 'run' && !options.baseline) {
|
|
176
|
+
throw new Error('A check or acceptance needs a baseline path')
|
|
177
|
+
}
|
|
178
|
+
if (mode == 'accept') tolerance(options.tolerance!)
|
|
179
|
+
if (mode == 'check' && options.tolerance != null) {
|
|
180
|
+
throw new Error('Checks use the baseline tolerance')
|
|
181
|
+
}
|
|
182
|
+
let repeats = options.runs ?? 1
|
|
183
|
+
if (!Number.isSafeInteger(repeats) || repeats < 1) {
|
|
184
|
+
throw new Error('Expected a positive integer run count')
|
|
185
|
+
}
|
|
186
|
+
let store = options.files ?? files
|
|
187
|
+
let readBase = async (): Promise<Baseline | undefined> => {
|
|
188
|
+
let base: Baseline | undefined
|
|
189
|
+
if (mode != 'run') {
|
|
190
|
+
try {
|
|
191
|
+
base = JSON.parse(await store.read(options.baseline!))
|
|
192
|
+
} catch (error) {
|
|
193
|
+
if (mode == 'check' || !io.isNotFound(error)) {
|
|
194
|
+
throw error
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (mode == 'check' || base !== undefined) validate(base!)
|
|
198
|
+
}
|
|
199
|
+
return base
|
|
200
|
+
}
|
|
201
|
+
await readBase()
|
|
202
|
+
let context = await (options.host ?? host)()
|
|
203
|
+
// Only a check or an acceptance is judged, so only they sample the box.
|
|
204
|
+
let sampled = mode == 'run' || options.figure == 'cpu'
|
|
205
|
+
? { ...context, busy: undefined }
|
|
206
|
+
: context
|
|
207
|
+
let runs: Run[] = []
|
|
208
|
+
for (let i = 0; i < repeats; i++) {
|
|
209
|
+
runs.push(await measure(suite, rounds, sampled, exclusive, options.figure))
|
|
210
|
+
}
|
|
211
|
+
// A different acceptance may have banked while our rounds yielded. Read its
|
|
212
|
+
// baseline under the publication lock before comparing or moving the best.
|
|
213
|
+
let readLocal = async (): Promise<Baseline | undefined> => {
|
|
214
|
+
try {
|
|
215
|
+
let local = JSON.parse(await store.read(options.local!))
|
|
216
|
+
validate(local)
|
|
217
|
+
return local
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (!io.isNotFound(error)) throw error
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
let current = await exclusive(async () => {
|
|
223
|
+
let base = await readBase()
|
|
224
|
+
let current = best(runs)
|
|
225
|
+
// A baseline banked on another runtime or CPU is no reference for this
|
|
226
|
+
// box. A check judges by the one this box banked on its own, and without
|
|
227
|
+
// one the run is measured, not judged, and banks as that baseline.
|
|
228
|
+
let elsewhere = (base: Baseline | undefined) =>
|
|
229
|
+
base && ['runtime', 'cpu'].includes(incomparable(base, current) ?? '')
|
|
230
|
+
let banked = elsewhere(base) && mode == 'check' && options.local
|
|
231
|
+
? await readLocal()
|
|
232
|
+
: undefined
|
|
233
|
+
if (banked && !incomparable(banked, current)) base = banked
|
|
234
|
+
current.tolerance = mode == 'accept'
|
|
235
|
+
? options.tolerance!
|
|
236
|
+
: base?.tolerance ?? null
|
|
237
|
+
if (mode == 'accept' && !insufficient(current)) {
|
|
238
|
+
current.verdict = loaded(current) ? 'loaded' : 'accepted'
|
|
239
|
+
}
|
|
240
|
+
if (elsewhere(base) && mode == 'check' && !insufficient(current)) {
|
|
241
|
+
current.verdict = 'measured'
|
|
242
|
+
let bank = options.local && !loaded(current)
|
|
243
|
+
if (bank) await store.write(options.local!, rebased(base!, current))
|
|
244
|
+
console.error(
|
|
245
|
+
`${current.suite}: baseline banked on ${base!.runtime}, ${
|
|
246
|
+
base!.cpu
|
|
247
|
+
}; measured on ${current.runtime}, ${current.cpu}${
|
|
248
|
+
bank ? `, banked as its baseline here in ${options.local}` : ''
|
|
249
|
+
}`,
|
|
250
|
+
)
|
|
251
|
+
} else if (base && mode == 'check' && !insufficient(current)) {
|
|
252
|
+
current.regressions = compare(base, current, options.coverage)
|
|
253
|
+
current.verdict = current.regressions.length
|
|
254
|
+
? loaded(current) ? 'loaded' : 'regressed'
|
|
255
|
+
: covered(base, current)
|
|
256
|
+
? 'passed'
|
|
257
|
+
: 'measured'
|
|
258
|
+
}
|
|
259
|
+
// An exact acceptance banks the suite as measured: a baseline banked on
|
|
260
|
+
// another runtime or workload, or for benches the suite no longer has, is
|
|
261
|
+
// no reference for it.
|
|
262
|
+
let previous = !base
|
|
263
|
+
? undefined
|
|
264
|
+
: options.coverage == 'subset'
|
|
265
|
+
? base
|
|
266
|
+
: incomparable(base, current)
|
|
267
|
+
? undefined
|
|
268
|
+
: {
|
|
269
|
+
...base,
|
|
270
|
+
benches: base.benches.filter((b) =>
|
|
271
|
+
current.benches.some((c) => c.name == b.name)
|
|
272
|
+
),
|
|
273
|
+
}
|
|
274
|
+
await store.write(options.output, current)
|
|
275
|
+
// A loaded run is kept as measured; baseline() refuses to bank it.
|
|
276
|
+
if (mode == 'accept' && !insufficient(current)) {
|
|
277
|
+
await store.write(
|
|
278
|
+
options.baseline!,
|
|
279
|
+
baseline(runs, options.tolerance!, previous, options.slower),
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
return current
|
|
283
|
+
})
|
|
284
|
+
let report: Awaited<ReturnType<Reporters>> = []
|
|
285
|
+
try {
|
|
286
|
+
report = await (options.reporters ?? reporters)(context)
|
|
287
|
+
} catch (error) {
|
|
288
|
+
console.error('Benchmark reporter factory failed', error)
|
|
289
|
+
}
|
|
290
|
+
if (report.length) {
|
|
291
|
+
// Reporting owns an immutable snapshot, never the ratchet's mutable result.
|
|
292
|
+
let snapshot = freeze(JSON.parse(JSON.stringify(current)) as Run)
|
|
293
|
+
// One broken provider cannot keep another from receiving the durable run.
|
|
294
|
+
let deliveries = await Promise.allSettled(
|
|
295
|
+
report.map((send) => Promise.resolve().then(() => send(snapshot))),
|
|
296
|
+
)
|
|
297
|
+
for (let delivery of deliveries) {
|
|
298
|
+
if (delivery.status == 'rejected') {
|
|
299
|
+
console.error('Benchmark reporter failed', delivery.reason)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (current.verdict == 'regressed') throw new Regressed(current)
|
|
304
|
+
return current
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** One run: every round of every bench, aggregated, with the host's metadata.
|
|
308
|
+
* The ratchet's verdict is the caller's. */
|
|
309
|
+
let measure = async (
|
|
310
|
+
suite: Suite | CollectedSuite,
|
|
311
|
+
rounds: number,
|
|
312
|
+
context: Host,
|
|
313
|
+
exclusive: Exclusive,
|
|
314
|
+
figure?: 'cpu',
|
|
315
|
+
): Promise<Run> => {
|
|
316
|
+
let benches: Result[] = suite.benches.map((b) => ({
|
|
317
|
+
name: b.name,
|
|
318
|
+
unit: b.unit,
|
|
319
|
+
...(b.resolution !== undefined ? { resolution: b.resolution } : {}),
|
|
320
|
+
better: b.better ?? 'lower',
|
|
321
|
+
samples: [],
|
|
322
|
+
rounds: [],
|
|
323
|
+
median: 0,
|
|
324
|
+
spread: 0,
|
|
325
|
+
spans: null,
|
|
326
|
+
}))
|
|
327
|
+
// The box is sampled at rest before each round, inside the lock: the
|
|
328
|
+
// bench's own work, and what it asks of a server, never counts against it.
|
|
329
|
+
let busy: number[] = []
|
|
330
|
+
for (let round = 0; round < rounds; round++) {
|
|
331
|
+
await exclusive(async () => {
|
|
332
|
+
let use = await context.busy?.()
|
|
333
|
+
if (use !== undefined) busy.push(use)
|
|
334
|
+
let collected = 'collect' in suite
|
|
335
|
+
? await suite.collect(round)
|
|
336
|
+
: undefined
|
|
337
|
+
if (collected) {
|
|
338
|
+
sameNames(
|
|
339
|
+
suite.benches,
|
|
340
|
+
Object.keys(collected).map((name) => ({ name, unit: 'collected' })),
|
|
341
|
+
)
|
|
342
|
+
}
|
|
343
|
+
for (let i = 0; i < benches.length; i++) {
|
|
344
|
+
let b = benches[i]
|
|
345
|
+
let input = collected
|
|
346
|
+
? collected[b.name]
|
|
347
|
+
: await (suite as Suite).benches[i].sample(round)
|
|
348
|
+
let raw = samples(input)
|
|
349
|
+
if (figure && raw.some((s) => s.cpu === undefined)) {
|
|
350
|
+
throw new Error(`Missing CPU measurement: ${b.name}`)
|
|
351
|
+
}
|
|
352
|
+
let measured = (s: typeof raw[number]) => figure ? s.cpu! : s.value
|
|
353
|
+
b.samples.push(
|
|
354
|
+
...raw.map(({ spans: _spans, ...s }) => ({ ...s, round })),
|
|
355
|
+
)
|
|
356
|
+
let middle = [...raw].sort((a, b) =>
|
|
357
|
+
measured(a) - measured(b)
|
|
358
|
+
)[Math.floor(raw.length / 2)]
|
|
359
|
+
let cpu = raw.map((s) =>
|
|
360
|
+
s.cpu
|
|
361
|
+
)
|
|
362
|
+
b.rounds.push({
|
|
363
|
+
index: round,
|
|
364
|
+
median: median(raw.map(measured)),
|
|
365
|
+
...cpu.every((n) => n !== undefined)
|
|
366
|
+
? { cpu: median(cpu as number[]) }
|
|
367
|
+
: {},
|
|
368
|
+
spans: middle.spans ?? null,
|
|
369
|
+
})
|
|
370
|
+
}
|
|
371
|
+
})
|
|
372
|
+
}
|
|
373
|
+
for (let b of benches) {
|
|
374
|
+
b.median = median(b.rounds.map((r) => r.median))
|
|
375
|
+
b.spread = spread(b.rounds.map((r) => r.median), b.better)
|
|
376
|
+
let middle = [...b.rounds].sort((a, b) =>
|
|
377
|
+
a.median - b.median
|
|
378
|
+
)[Math.floor(rounds / 2)]
|
|
379
|
+
b.spans = middle.spans
|
|
380
|
+
}
|
|
381
|
+
return {
|
|
382
|
+
version: 1,
|
|
383
|
+
suite: suite.name,
|
|
384
|
+
metric: figure ? `${suite.metric}/cpu` : suite.metric,
|
|
385
|
+
workload: suite.workload,
|
|
386
|
+
at: new Date().toISOString(),
|
|
387
|
+
commit: context.commit,
|
|
388
|
+
runtime: context.runtime,
|
|
389
|
+
cpu: context.cpu,
|
|
390
|
+
load: [...context.load],
|
|
391
|
+
busy: busy.length ? Math.max(...busy) : null,
|
|
392
|
+
rounds,
|
|
393
|
+
benches,
|
|
394
|
+
tolerance: null,
|
|
395
|
+
verdict: insufficient({ benches }) ? 'inconclusive' : 'measured',
|
|
396
|
+
regressions: [],
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
let freeze = <T>(input: T): T => {
|
|
401
|
+
if (input && typeof input == 'object') {
|
|
402
|
+
Object.freeze(input)
|
|
403
|
+
for (let value of Object.values(input)) freeze(value)
|
|
404
|
+
}
|
|
405
|
+
return input
|
|
406
|
+
}
|
package/rusage.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// getrusage's two leading timevals on 64-bit Linux and Darwin. Darwin uses
|
|
2
|
+
// a 32-bit suseconds_t followed by padding; Linux uses a 64-bit long.
|
|
3
|
+
// Apple declares these in xnu/bsd/sys/_types/_timeval.h and sys/_types.h.
|
|
4
|
+
// `thread` is the clock_gettime(2) id of the calling thread's CPU clock,
|
|
5
|
+
// CLOCK_THREAD_CPUTIME_ID: Linux's linux/time.h, Darwin's sys/_types/_clock_id.h.
|
|
6
|
+
export let layouts = {
|
|
7
|
+
linux: {
|
|
8
|
+
library: 'libc.so.6',
|
|
9
|
+
micros: 64,
|
|
10
|
+
thread: 3
|
|
11
|
+
},
|
|
12
|
+
darwin: {
|
|
13
|
+
library: '/usr/lib/libSystem.B.dylib',
|
|
14
|
+
micros: 32,
|
|
15
|
+
thread: 16
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
/** User plus system CPU time, in milliseconds, from struct rusage's
|
|
19
|
+
* leading two timevals. Both supported architectures are little endian. */ export let milliseconds = (usage, micros)=>{
|
|
20
|
+
let seconds = usage.getBigInt64(0, true) + usage.getBigInt64(16, true);
|
|
21
|
+
let fraction = (at)=>micros == 32 ? usage.getInt32(at, true) : Number(usage.getBigInt64(at, true));
|
|
22
|
+
return Number(seconds) * 1000 + (fraction(8) + fraction(24)) / 1000;
|
|
23
|
+
};
|
|
24
|
+
/** A struct timespec, two 64-bit fields on both systems, in milliseconds. */ export let nanos = (time)=>Number(time.getBigInt64(0, true)) * 1000 + Number(time.getBigInt64(8, true)) / 1e6;
|
|
25
|
+
//# sourceMappingURL=rusage.js.map
|
package/rusage.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rusage.js","sources":["./rusage.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,sEAAsE;AACtE,0EAA0E;AAC1E,yEAAyE;AACzE,kFAAkF;AAClF,OAAO,IAAI,UAEP;EACF,OAAO;IAAE,SAAS;IAAa,QAAQ;IAAI,QAAQ;EAAE;EACrD,QAAQ;IAAE,SAAS;IAA8B,QAAQ;IAAI,QAAQ;EAAG;AAC1E,EAAC;AAED;yEACyE,GACzE,OAAO,IAAI,eAAe,CAAC,OAAiB;EAC1C,IAAI,UAAU,MAAM,WAAW,CAAC,GAAG,QAAQ,MAAM,WAAW,CAAC,IAAI;EACjE,IAAI,WAAW,CAAC,KACd,UAAU,KACN,MAAM,QAAQ,CAAC,IAAI,QACnB,OAAO,MAAM,WAAW,CAAC,IAAI;EACnC,OAAO,OAAO,WAAW,OAAO,CAAC,SAAS,KAAK,SAAS,GAAG,IAAI;AACjE,EAAC;AAED,2EAA2E,GAC3E,OAAO,IAAI,QAAQ,CAAC,OAClB,OAAO,KAAK,WAAW,CAAC,GAAG,SAAS,OACpC,OAAO,KAAK,WAAW,CAAC,GAAG,SAAS,IAAG"}
|
package/rusage.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// getrusage's two leading timevals on 64-bit Linux and Darwin. Darwin uses
|
|
2
|
+
// a 32-bit suseconds_t followed by padding; Linux uses a 64-bit long.
|
|
3
|
+
// Apple declares these in xnu/bsd/sys/_types/_timeval.h and sys/_types.h.
|
|
4
|
+
// `thread` is the clock_gettime(2) id of the calling thread's CPU clock,
|
|
5
|
+
// CLOCK_THREAD_CPUTIME_ID: Linux's linux/time.h, Darwin's sys/_types/_clock_id.h.
|
|
6
|
+
export let layouts: Partial<
|
|
7
|
+
Record<string, { library: string; micros: 32 | 64; thread: number }>
|
|
8
|
+
> = {
|
|
9
|
+
linux: { library: 'libc.so.6', micros: 64, thread: 3 },
|
|
10
|
+
darwin: { library: '/usr/lib/libSystem.B.dylib', micros: 32, thread: 16 },
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** User plus system CPU time, in milliseconds, from struct rusage's
|
|
14
|
+
* leading two timevals. Both supported architectures are little endian. */
|
|
15
|
+
export let milliseconds = (usage: DataView, micros: 32 | 64): number => {
|
|
16
|
+
let seconds = usage.getBigInt64(0, true) + usage.getBigInt64(16, true)
|
|
17
|
+
let fraction = (at: number) =>
|
|
18
|
+
micros == 32
|
|
19
|
+
? usage.getInt32(at, true)
|
|
20
|
+
: Number(usage.getBigInt64(at, true))
|
|
21
|
+
return Number(seconds) * 1000 + (fraction(8) + fraction(24)) / 1000
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A struct timespec, two 64-bit fields on both systems, in milliseconds. */
|
|
25
|
+
export let nanos = (time: DataView): number =>
|
|
26
|
+
Number(time.getBigInt64(0, true)) * 1000 +
|
|
27
|
+
Number(time.getBigInt64(8, true)) / 1e6
|