Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

TypeScript · 116 lines · 8.5 KiB · at 503e839

// The on-call micro benchmark, rebuilt from Rebasely docs/v2/tsus-pilot.md (its scripts were not// committed): jobFor over a realistic page of 1,000 events and the dedupe test over the jobs it// yields, in three versions: before the pilot (plain .ts), the pilot (TSUS v1, wrapper objects),// and TSUS v2 (Option is T | undefined). A differential check first proves all three agree.////   node bench/oncall/run.ts            (node 22.6+ strips the types itself)import * as old from "./old.ts";import * as v1 from "./v1.ts";import * as v2 from "./v2/rules.ts";// @ts-expect-error -- compiled JS without declarationsimport { Some as v1Some, __tsus_match_lit } from "./v1/_runtime/index.js";// @ts-expect-error -- compiled JS without declarationsimport { fromNullable as v1FromNullable } from "./v1/_runtime/boundary.js";import { matchLiteral, fromNullable as v2FromNullable } from "@tsus/v2";import type { ApiEvent, HandledEntry, Job, WatchConfig } from "./types.ts";// A seeded generator (mulberry32), so every run sees the same events.const rng = (seed: number) => () => {  let t = (seed += 0x6d2b79f5);  t = Math.imul(t ^ (t >>> 15), t | 1);  t ^= t + Math.imul(t ^ (t >>> 7), t | 61);  return ((t ^ (t >>> 14)) >>> 0) / 4294967296;};const pick = <T,>(r: () => number, xs: readonly T[]): T => xs[Math.floor(r() * xs.length)] as T;const KINDS = ["alert_opened", "alert_opened", "alert_opened", "alert_opened", "alert_severity_rose", "alert_resolved", "alert_resolved", "alert_resolved", "abuse_flag", "milestone", "heartbeat"];const PREFIXES = ["deploy:", "d1:", "queue:", "probe:", "oncall:", "oncall:drill:", "bug_report:", "ux_issues:", "billing:"];const CLASSES = ["critical", "page", "ticket", "ticket", "ticket", "direct"];const ENVS = ["production", "production", "production", "staging", null, ""];const makeEvents = (n: number, seed: number): ApiEvent[] => {  const r = rng(seed);  return Array.from({ length: n }, (_, i): ApiEvent => {    const kind = pick(r, KINDS);    const roll = r();    const data =      roll < 0.05 ? null : roll < 0.08 ? "{not json" : roll < 0.1 ? "" : JSON.stringify(kind === "abuse_flag" ? { subject_id: r() < 0.8 ? `org_${Math.floor(r() * 40)}` : 7, reason: "spam" } : { class: r() < 0.9 ? pick(r, CLASSES) : 3, severity: "error", title: `thing ${i} broke`, body: "x".repeat(Math.floor(r() * 200)) });    return { id: 1000 + i, at: 1_760_000_000_000 + i * 1000, kind, key: r() < 0.05 ? null : r() < 0.03 ? "" : `${pick(r, PREFIXES)}${Math.floor(r() * 60)}`, environment: pick(r, ENVS), actor: null, data };  });};const cfg: WatchConfig = { environments: ["production"], dedupeHours: 6 };const NOW = 1_760_000_500_000;const norm = (j: unknown) => JSON.stringify(j, (_k, v) => (v === undefined ? null : v));// 1. Differential: the three versions agree on 20,000 events, and on the dedupe test.const many = makeEvents(20_000, 7);let disagreements = 0;for (const e of many) {  const a = norm(old.jobFor(e, cfg, NOW));  if (a !== norm(v1.jobFor(e, cfg, NOW)) || a !== norm(v2.jobFor(e, cfg, NOW)) || a !== norm(v2.jobForSwitch(e, cfg, NOW))) disagreements += 1;}const page = makeEvents(1000, 42);const jobs = page.map((e) => old.jobFor(e, cfg, NOW)).filter((j): j is Job => j !== null);const handled: Record<string, HandledEntry> = {};const hr = rng(3);for (const j of jobs) if (hr() < 0.5) handled[j.key] = { at: NOW - Math.floor(hr() * 10) * 3_600_000, incident: hr() < 0.5 ? null : NOW - 4_000_000 };const v2Jobs = page.map((e) => v2.jobFor(e, cfg, NOW)).filter((j): j is v2.Job => j !== undefined);for (let i = 0; i < jobs.length; i += 1) {  const j = jobs[i] as Job;  const k = v2Jobs[i] as v2.Job;  const a = old.isDuplicate(handled, j, NOW, 6);  if (a !== v1.isDuplicate(handled, j, NOW, 6) || a !== v2.isDuplicate(v2FromNullable(handled[k.key]), k.incident, NOW, 6)) disagreements += 1;}// 2. Timing: median of 7 runs, each long enough to be stable.let sink = 0;const time = (label: string, ops: number, f: () => number): number => {  for (let i = 0; i < 3; i += 1) sink += f();  const runs: number[] = [];  for (let r = 0; r < 7; r += 1) {    const t0 = process.hrtime.bigint();    for (let i = 0; i < ops; i += 1) sink += f();    runs.push(Number(process.hrtime.bigint() - t0) / ops);  }  runs.sort((a, b) => a - b);  const median = runs[3] as number;  console.log(`${label.padEnd(62)} median ${median.toFixed(1).padStart(10)} ns/op  (min ${(runs[0] as number).toFixed(1)}, max ${(runs[6] as number).toFixed(1)})`);  return median;};const pageOld = () => { let n = 0; for (const e of page) if (old.jobFor(e, cfg, NOW) !== null) n += 1; return n; };const pageV1 = () => { let n = 0; for (const e of page) if (v1.jobFor(e, cfg, NOW) !== null) n += 1; return n; };const pageV2 = () => { let n = 0; for (const e of page) if (v2.jobFor(e, cfg, NOW) !== undefined) n += 1; return n; };const pageV2s = () => { let n = 0; for (const e of page) if (v2.jobForSwitch(e, cfg, NOW) !== undefined) n += 1; return n; };const dedupeOld = () => { let n = 0; for (const j of jobs) if (old.isDuplicate(handled, j, NOW, 6)) n += 1; return n; };const dedupeV1 = () => { let n = 0; for (const j of jobs) if (v1.isDuplicate(handled, j, NOW, 6)) n += 1; return n; };const dedupeV2 = () => { let n = 0; for (const j of v2Jobs) if (v2.isDuplicate(v2FromNullable(handled[j.key]), j.incident, NOW, 6)) n += 1; return n; };console.log(`events: page of ${page.length}, ${jobs.length} jobs; differential over ${many.length} events and ${jobs.length} dedupe tests: ${disagreements} disagreements`);const o1 = time("before (plain .ts) jobFor, one page of 1000 events", 300, pageOld);const p1 = time("v1 (pilot) jobFor: classify in tsus + .ts adapter", 300, pageV1);const q1 = time("v2 jobFor: Option is T | undefined, no adapter", 300, pageV2);const s1 = time("v2 jobFor, rankOf as an exhaustive switch", 300, pageV2s);console.log(`  -> per event: before ${(o1 / 1000).toFixed(0)} ns, v1 ${(p1 / 1000).toFixed(0)} ns (${(p1 / o1).toFixed(2)}x), v2 ${(q1 / 1000).toFixed(0)} ns (${(q1 / o1).toFixed(2)}x), v2 with switch ${(s1 / 1000).toFixed(0)} ns (${(s1 / o1).toFixed(2)}x)`);const o2 = time(`before dedupe test (inline), ${jobs.length} jobs`, 20_000, dedupeOld);const p2 = time(`v1 isDuplicate(fromNullable(...)), ${jobs.length} jobs`, 20_000, dedupeV1);const q2 = time(`v2 isDuplicate(fromNullable(...)), ${jobs.length} jobs`, 20_000, dedupeV2);console.log(`  -> per job: before ${(o2 / jobs.length).toFixed(1)} ns, v1 ${(p2 / jobs.length).toFixed(1)} ns (${(p2 / o2).toFixed(2)}x), v2 ${(q2 / jobs.length).toFixed(1)} ns (${(q2 / o2).toFixed(2)}x)`);console.log("Primitives (1000 per op):");const xs = Array.from({ length: 1000 }, (_, i) => (i % 3 === 0 ? null : i));const cls = Array.from({ length: 1000 }, (_, i) => (["critical", "page", "ticket", "direct"] as const)[i % 4] as "critical" | "page" | "ticket" | "direct");time("plain x ?? 0", 20_000, () => { let n = 0; for (const x of xs) n += x ?? 0; return n; });time("v1 fromNullable(x).unwrapOr(0) (Some: object + 8 closures)", 20_000, () => { let n = 0; for (const x of xs) n += v1FromNullable(x).unwrapOr(0); return n; });time("v1 Some(x).value", 20_000, () => { let n = 0; for (const x of xs) n += v1Some(x ?? 0).value; return n; });time("v2 fromNullable(x) ?? 0 (no object)", 20_000, () => { let n = 0; for (const x of xs) n += v2FromNullable(x) ?? 0; return n; });const RANK: Record<string, number> = { critical: 0, page: 1, ticket: 2, direct: 3 };time("plain RANK[cls] ?? 2 (lookup table)", 20_000, () => { let n = 0; for (const c of cls) n += RANK[c] ?? 2; return n; });time("plain switch (cls)", 20_000, () => { let n = 0; for (const c of cls) { switch (c) { case "critical": n += 0; break; case "page": n += 1; break; case "ticket": n += 2; break; case "direct": n += 3; break; } } return n; });time("v1 match macro, literal arms (__tsus_match_lit)", 20_000, () => { let n = 0; for (const c of cls) n += __tsus_match_lit(c)([["critical", () => 0], ["page", () => 1], ["ticket", () => 2], ["direct", () => 3]], () => 2); return n; });time("v2 matchLiteral, arms inline", 20_000, () => { let n = 0; for (const c of cls) n += matchLiteral(c, { critical: () => 0, page: () => 1, ticket: () => 2, direct: () => 3 }); return n; });const ARMS = { critical: () => 0, page: () => 1, ticket: () => 2, direct: () => 3 } as const;time("v2 matchLiteral, arms hoisted to a const", 20_000, () => { let n = 0; for (const c of cls) n += matchLiteral(c, ARMS); return n; });console.log(`(sink ${sink % 7})`);console.log(`node ${process.version}, ${process.platform} ${process.arch}`);