|
| 1 | +import { stats, parseMb, checkRegressions, BenchmarkResult } from "./benchmark-utils"; |
| 2 | + |
| 3 | +// ── stats() ────────────────────────────────────────────────────── |
| 4 | + |
| 5 | +describe("stats()", () => { |
| 6 | + it("throws on empty array", () => { |
| 7 | + expect(() => stats([])).toThrow("stats() requires at least one value"); |
| 8 | + }); |
| 9 | + |
| 10 | + it("handles single element", () => { |
| 11 | + const result = stats([42]); |
| 12 | + expect(result).toEqual({ mean: 42, median: 42, p95: 42, p99: 42 }); |
| 13 | + }); |
| 14 | + |
| 15 | + it("handles two elements", () => { |
| 16 | + const result = stats([10, 20]); |
| 17 | + expect(result.mean).toBe(15); |
| 18 | + expect(result.median).toBe(20); // floor(2/2) = index 1 |
| 19 | + expect(result.p95).toBe(20); |
| 20 | + expect(result.p99).toBe(20); |
| 21 | + }); |
| 22 | + |
| 23 | + it("handles odd count", () => { |
| 24 | + const result = stats([3, 1, 2]); |
| 25 | + // sorted: [1, 2, 3] |
| 26 | + expect(result.mean).toBe(2); |
| 27 | + expect(result.median).toBe(2); // floor(3/2) = index 1 |
| 28 | + expect(result.p95).toBe(3); // floor(3*0.95)=2, index 2 |
| 29 | + expect(result.p99).toBe(3); // floor(3*0.99)=2, index 2 |
| 30 | + }); |
| 31 | + |
| 32 | + it("handles even count", () => { |
| 33 | + const result = stats([4, 2, 1, 3]); |
| 34 | + // sorted: [1, 2, 3, 4] |
| 35 | + expect(result.mean).toBe(3); // Math.round(10/4) = 3 (2.5 rounds to 3) |
| 36 | + expect(result.median).toBe(3); // floor(4/2) = index 2 |
| 37 | + expect(result.p95).toBe(4); // floor(4*0.95)=3 |
| 38 | + expect(result.p99).toBe(4); // floor(4*0.99)=3 |
| 39 | + }); |
| 40 | + |
| 41 | + it("handles all same values", () => { |
| 42 | + const result = stats([7, 7, 7, 7, 7]); |
| 43 | + expect(result).toEqual({ mean: 7, median: 7, p95: 7, p99: 7 }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("rounds mean correctly", () => { |
| 47 | + // 1 + 2 + 3 = 6 / 3 = 2, no rounding needed |
| 48 | + expect(stats([1, 2, 3]).mean).toBe(2); |
| 49 | + // 1 + 2 = 3 / 2 = 1.5, rounds to 2 |
| 50 | + expect(stats([1, 2]).mean).toBe(2); |
| 51 | + // 1 + 2 + 4 = 7 / 3 = 2.333... rounds to 2 |
| 52 | + expect(stats([1, 2, 4]).mean).toBe(2); |
| 53 | + }); |
| 54 | + |
| 55 | + it("does not mutate input array", () => { |
| 56 | + const input = [5, 3, 1, 4, 2]; |
| 57 | + const copy = [...input]; |
| 58 | + stats(input); |
| 59 | + expect(input).toEqual(copy); |
| 60 | + }); |
| 61 | + |
| 62 | + it("handles large array with correct percentiles", () => { |
| 63 | + // 100 values: 1..100 |
| 64 | + const values = Array.from({ length: 100 }, (_, i) => i + 1); |
| 65 | + const result = stats(values); |
| 66 | + expect(result.mean).toBe(51); // Math.round(5050/100) |
| 67 | + expect(result.median).toBe(51); // floor(100/2)=50, value at index 50 = 51 |
| 68 | + expect(result.p95).toBe(96); // floor(100*0.95)=95, value at index 95 = 96 |
| 69 | + expect(result.p99).toBe(100); // floor(100*0.99)=99, value at index 99 = 100 |
| 70 | + }); |
| 71 | + |
| 72 | + it("handles negative values", () => { |
| 73 | + const result = stats([-10, -5, 0, 5, 10]); |
| 74 | + expect(result.mean).toBe(0); |
| 75 | + expect(result.median).toBe(0); |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +// ── parseMb() ──────────────────────────────────────────────────── |
| 80 | + |
| 81 | +describe("parseMb()", () => { |
| 82 | + it("parses MiB values", () => { |
| 83 | + expect(parseMb("123.4MiB / 7.773GiB")).toBe(123.4); |
| 84 | + }); |
| 85 | + |
| 86 | + it("parses GiB values", () => { |
| 87 | + expect(parseMb("2GiB / 8GiB")).toBe(2048); |
| 88 | + }); |
| 89 | + |
| 90 | + it("parses KiB values", () => { |
| 91 | + expect(parseMb("512KiB / 8GiB")).toBe(0.5); |
| 92 | + }); |
| 93 | + |
| 94 | + it("parses zero-valued MiB input", () => { |
| 95 | + expect(parseMb("0MiB")).toBe(0); |
| 96 | + }); |
| 97 | + |
| 98 | + it("returns 0 for unrecognized or empty format", () => { |
| 99 | + expect(parseMb("unknown")).toBe(0); |
| 100 | + expect(parseMb("")).toBe(0); |
| 101 | + }); |
| 102 | + |
| 103 | + it("is case insensitive", () => { |
| 104 | + expect(parseMb("100mib")).toBe(100); |
| 105 | + expect(parseMb("1gib")).toBe(1024); |
| 106 | + expect(parseMb("1024kib")).toBe(1); |
| 107 | + }); |
| 108 | + |
| 109 | + it("handles decimal values", () => { |
| 110 | + expect(parseMb("1.5GiB / 8GiB")).toBe(1536); |
| 111 | + expect(parseMb("0.5MiB / 8GiB")).toBe(0.5); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +// ── checkRegressions() ────────────────────────────────────────── |
| 116 | + |
| 117 | +describe("checkRegressions()", () => { |
| 118 | + const thresholds: Record<string, { target: number; critical: number }> = { |
| 119 | + container_startup_cold: { target: 15000, critical: 20000 }, |
| 120 | + squid_https_latency: { target: 100, critical: 200 }, |
| 121 | + memory_footprint_mb: { target: 500, critical: 1024 }, |
| 122 | + }; |
| 123 | + |
| 124 | + function makeResult(metric: string, p95: number, unit = "ms"): BenchmarkResult { |
| 125 | + return { metric, unit, values: [p95], mean: p95, median: p95, p95, p99: p95 }; |
| 126 | + } |
| 127 | + |
| 128 | + it("returns empty array when all within thresholds", () => { |
| 129 | + const results = [ |
| 130 | + makeResult("container_startup_cold", 19000), |
| 131 | + makeResult("squid_https_latency", 150), |
| 132 | + makeResult("memory_footprint_mb", 800, "MB"), |
| 133 | + ]; |
| 134 | + expect(checkRegressions(results, thresholds)).toEqual([]); |
| 135 | + }); |
| 136 | + |
| 137 | + it("detects single regression", () => { |
| 138 | + const results = [ |
| 139 | + makeResult("container_startup_cold", 25000), |
| 140 | + ]; |
| 141 | + const regressions = checkRegressions(results, thresholds); |
| 142 | + expect(regressions).toHaveLength(1); |
| 143 | + expect(regressions[0]).toContain("container_startup_cold"); |
| 144 | + expect(regressions[0]).toContain("p95=25000"); |
| 145 | + expect(regressions[0]).toContain("critical threshold of 20000"); |
| 146 | + }); |
| 147 | + |
| 148 | + it("detects multiple regressions", () => { |
| 149 | + const results = [ |
| 150 | + makeResult("container_startup_cold", 25000), |
| 151 | + makeResult("squid_https_latency", 300), |
| 152 | + ]; |
| 153 | + const regressions = checkRegressions(results, thresholds); |
| 154 | + expect(regressions).toHaveLength(2); |
| 155 | + }); |
| 156 | + |
| 157 | + it("ignores metrics without thresholds", () => { |
| 158 | + const results = [ |
| 159 | + makeResult("unknown_metric", 999999), |
| 160 | + ]; |
| 161 | + expect(checkRegressions(results, thresholds)).toEqual([]); |
| 162 | + }); |
| 163 | + |
| 164 | + it("p95 exactly at critical is not a regression", () => { |
| 165 | + const results = [ |
| 166 | + makeResult("container_startup_cold", 20000), |
| 167 | + ]; |
| 168 | + expect(checkRegressions(results, thresholds)).toEqual([]); |
| 169 | + }); |
| 170 | + |
| 171 | + it("p95 one unit above critical is a regression", () => { |
| 172 | + const results = [ |
| 173 | + makeResult("container_startup_cold", 20001), |
| 174 | + ]; |
| 175 | + expect(checkRegressions(results, thresholds)).toHaveLength(1); |
| 176 | + }); |
| 177 | + |
| 178 | + it("returns empty array for empty results", () => { |
| 179 | + expect(checkRegressions([], thresholds)).toEqual([]); |
| 180 | + }); |
| 181 | + |
| 182 | + it("returns empty array for empty thresholds", () => { |
| 183 | + const results = [makeResult("container_startup_cold", 99999)]; |
| 184 | + expect(checkRegressions(results, {})).toEqual([]); |
| 185 | + }); |
| 186 | + |
| 187 | + it("includes unit in regression message", () => { |
| 188 | + const results = [makeResult("memory_footprint_mb", 2000, "MB")]; |
| 189 | + const regressions = checkRegressions(results, thresholds); |
| 190 | + expect(regressions[0]).toContain("MB"); |
| 191 | + }); |
| 192 | +}); |
0 commit comments