Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

server-metrics.test.ts

158 lines5.3 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 compareMetrics,
5 heapGrowth,
6 parsePsCpuTime,
7 parseRunSelection,
8 slope,
9 type ServerMetrics,
10} from "./server-metrics-report.ts";
11​
12type Quiet = { cpuPct: number; wakeupsPerSec: number | null; rssPeakMB: number };
13const meta = { commit: "abc", platform: "linux-x64", noAgentScope: true };
14const quietRuns = (...runs: Quiet[]) =>
15 ({
16 meta,
17 runs: runs.map((quiet) => ({ quiet: { ...quiet, rssMB: 500, heapAfterGcMB: 200 } })),
18 }) satisfies ServerMetrics;
19const base: Quiet = { cpuPct: 0.3, wakeupsPerSec: 7, rssPeakMB: 500 };
20const cpuRuns = (...values: number[]) =>
21 quietRuns(...values.map((cpuPct) => ({ ...base, cpuPct })));
22​
23describe("compareMetrics", () => {
24 it.each([
25 ["a CPU jump past both the percentage and the floor", { cpuPct: 18.6 }, ["quiet.cpuPct"], []],
26 ["a large relative change under the 1 point CPU floor", { cpuPct: 0.9 }, [], []],
27 ["RSS growth under the 15 MB floor", { rssPeakMB: 512 }, [], []],
28 ["RSS growth past it", { rssPeakMB: 585 }, ["quiet.rssPeakMB"], []],
29 [
30 "an improvement past the floors",
31 { cpuPct: 0.1, wakeupsPerSec: 7, rssPeakMB: 400 },
32 [],
33 ["quiet.rssPeakMB"],
34 ],
35 ])("one run each: %s", (_name, change, regressed, improved) => {
36 const result = compareMetrics(quietRuns(base), quietRuns({ ...base, ...change }));
37 expect(result.regressed).toEqual(regressed);
38 expect(result.improved).toEqual(improved);
39 });
40​
41 it("shows a metric missing on one side as n/a, not as a change", () => {
42 const before = quietRuns({ ...base, wakeupsPerSec: null });
43 const result = compareMetrics(before, quietRuns({ ...base, wakeupsPerSec: 50 }));
44 expect(result.regressed).toEqual([]);
45 expect(result.table).toContain("| quiet.wakeupsPerSec | – | 50 | n/a | | |");
46 });
47​
48 it("flags repeated runs only when the whole interval clears the limit", () => {
49 const steady = cpuRuns(26.1, 27.4, 25.8);
50 const fixed = cpuRuns(2.2, 2.5, 2.1);
51 expect(compareMetrics(steady, fixed).improved).toContain("quiet.cpuPct");
52 expect(compareMetrics(fixed, steady).regressed).toContain("quiet.cpuPct");
53 // The medians differ by 2.8 points, past the floor, but a resampled median can match.
54 expect(compareMetrics(fixed, cpuRuns(2.2, 5, 9)).regressed).toEqual([]);
55 });
56​
57 it("counts how many metrics both sides share", () => {
58 const startupOnly: ServerMetrics = { meta, runs: [{ startup: { readyMs: 9_000 } }] };
59 expect(compareMetrics(startupOnly, quietRuns(base)).compared).toBe(0);
60 expect(compareMetrics(quietRuns(base), quietRuns(base)).compared).toBe(5);
61 });
62});
63​
64describe("heapGrowth", () => {
65 it("lists only constructors that grew, small growth included", () => {
66 const before = new Map([
67 ["Shrank", { count: 10, bytes: 5_000_000 }],
68 ["Flat", { count: 3, bytes: 1_000 }],
69 ["Grew", { count: 1, bytes: 1_000 }],
70 ]);
71 const after = new Map([
72 ["Shrank", { count: 5, bytes: 1_000_000 }],
73 ["Flat", { count: 3, bytes: 1_000 }],
74 ["Grew", { count: 2, bytes: 1_500 }],
75 ["New", { count: 4, bytes: 3_000_000 }],
76 ]);
77 expect(heapGrowth(before, after)).toEqual([
78 { constructor: "New", count: 4, MB: 3 },
79 { constructor: "Grew", count: 1, MB: 0 },
80 ]);
81 });
82});
83​
84describe("slope", () => {
85 it("fits the cost per thread", () => {
86 const points: Array<readonly [number, number]> = [
87 [0, 175],
88 [5, 180],
89 [10, 185],
90 [20, 195],
91 ];
92 expect(slope(points)).toBeCloseTo(1);
93 });
94});
95​
96describe("parsePsCpuTime", () => {
97 it.each([
98 ["0:00.00", 0],
99 ["1:02.50", 62.5],
100 ["1:00:00.00", 3600],
101 ["2-01:00:00", 2 * 86_400 + 3600],
102 ["1-02:03", Number.NaN],
103 ["::", Number.NaN],
104 ["", Number.NaN],
105 ["1:2:3:4", Number.NaN],
106 ])("%s", (text, seconds) => {
107 expect(parsePsCpuTime(text)).toBe(seconds);
108 });
109});
110​
111describe("parseRunSelection", () => {
112 const scenarios = ["rest", "quiet", "streaming"];
113 it.each([
114 [
115 "the default thread counts",
116 { only: "", threads: "0,5,10,20", soak: 0 },
117 { only: [], threads: [0, 5, 10, 20] },
118 ],
119 [
120 "descending thread counts",
121 { only: "", threads: "5,20,10", soak: 0 },
122 { error: "--threads takes ascending counts such as 0,5,10,20" },
123 ],
124 [
125 "an empty thread count",
126 { only: "", threads: "0,,10", soak: 0 },
127 { error: "--threads takes ascending counts such as 0,5,10,20" },
128 ],
129 [
130 "named scenarios",
131 { only: "rest,quiet", threads: "0", soak: 0 },
132 { only: ["rest", "quiet"], threads: [0] },
133 ],
134 [
135 "an unknown scenario",
136 { only: "rest,idle", threads: "0", soak: 0 },
137 { error: "unknown scenario: idle" },
138 ],
139 ["a soak alone", { only: "", threads: "0", soak: 10 }, { only: ["none"], threads: [0] }],
140 [
141 "a soak with scenarios",
142 { only: "rest", threads: "0", soak: 10 },
143 { only: ["rest"], threads: [0] },
144 ],
145 [
146 "a negative soak",
147 { only: "", threads: "0", soak: -1 },
148 { error: "--soak takes minutes, 0 for none" },
149 ],
150 [
151 "an infinite soak",
152 { only: "", threads: "0", soak: Number.POSITIVE_INFINITY },
153 { error: "--soak takes minutes, 0 for none" },
154 ],
155 ])("%s", (_name, input, expected) => {
156 expect(parseRunSelection({ ...input, scenarios })).toEqual(expected);
157 });
158});