-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbenchmark.ts
More file actions
50 lines (41 loc) · 1.57 KB
/
Copy pathbenchmark.ts
File metadata and controls
50 lines (41 loc) · 1.57 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
// Throughput of the core operations. Run with: npx tsx benchmark/benchmark.ts
//
// Reports how fast the library fits a posterior and produces forecasts, so the
// cost of running predictive maintenance on a large fleet is clear.
import {
fitExponential,
fitWeibull,
forecast,
mulberry32,
sampleWeibull,
} from "../src/index.js";
import type { SurvivalObservation } from "../src/index.js";
const DATASET_SIZE = 2000;
const FIT_ITERATIONS = 200;
const FORECAST_ITERATIONS = 2000;
function simulate(seed: number): SurvivalObservation[] {
const rnd = mulberry32(seed);
return Array.from({ length: DATASET_SIZE }, () => ({ duration: sampleWeibull(rnd, 2, 50), censored: false }));
}
function timed(label: string, iterations: number, run: (index: number) => void): void {
const start = performance.now();
for (let i = 0; i < iterations; i += 1) run(i);
const elapsedMs = performance.now() - start;
const perSecond = (iterations / elapsedMs) * 1000;
console.log(
`${label}: ${iterations} iterations in ${elapsedMs.toFixed(1)} ms ` +
`(${perSecond.toFixed(0)} per second, ${(elapsedMs / iterations).toFixed(3)} ms each)`,
);
}
const dataset = simulate(1);
console.log(`Dataset: ${DATASET_SIZE} observations\n`);
timed("Exponential fit", FIT_ITERATIONS, () => {
fitExponential(dataset);
});
timed("Weibull fit (60-point shape grid)", FIT_ITERATIONS, () => {
fitWeibull(dataset);
});
const weibullModel = fitWeibull(dataset);
timed("Weibull forecast (3 horizons)", FORECAST_ITERATIONS, (i) => {
forecast(weibullModel, { age: i % 50, horizons: [6, 24, 72] });
});