-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy path@results.ts
More file actions
332 lines (289 loc) · 11 KB
/
Copy path@results.ts
File metadata and controls
332 lines (289 loc) · 11 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import * as fs from 'fs';
import * as path from 'path';
import { z } from "zod";
// Assuming DATA_PATH is defined elsewhere or passed as parameter
export const DATA_PATH = path.join(__dirname, '../../data');
export const RESULTS_DIR = ".results-remote"
// Interface for a single iteration of a benchmark query
export interface QueryIter {
plan: string;
rowCount: number;
elapsed: number; // Duration in milliseconds
tasks: number;
statsQErrorP50?: number;
statsQErrorP95?: number;
error?: string;
}
// Class for collecting benchmark run data
export class BenchmarkRun {
startTime: number;
dataset: string;
engine: string;
results: BenchResult[];
constructor(dataset: string, engine: string) {
this.dataset = dataset;
this.engine = engine;
this.startTime = Math.floor(Date.now() / 1000); // Unix timestamp in seconds
this.results = [];
}
loadPrevious(): BenchmarkRun | null {
const previousPath = path.join(DATA_PATH, this.dataset, `previous-remote.json`);
try {
const prevData = fs.readFileSync(previousPath, 'utf-8');
const prevOutput = JSON.parse(prevData) as BenchmarkRun;
// Create new instance and load results
const instance = new BenchmarkRun(prevOutput.dataset, prevOutput.engine);
instance.startTime = prevOutput.startTime;
instance.loadResults();
return instance;
} catch {
return null;
}
}
loadResults(): void {
this.results = BenchResult.loadMany(this.dataset, this.engine);
}
// Write data as JSON into output path if it exists
store(): void {
const outputPath = path.join(DATA_PATH, this.dataset, `previous-remote.json`);
// Ensure directory exists
const dir = path.dirname(outputPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// Custom serialization to handle results
const toSerialize = {
...this,
results: [] // Empty array for serialization
};
const json = JSON.stringify(toSerialize, null, 2);
fs.writeFileSync(outputPath, json);
// Store individual results
for (const result of this.results) {
result.store();
}
}
compare(other: BenchmarkRun): void {
console.log(`=== Comparing ${this.dataset} results from engine '${other.engine}' [prev] with '${this.engine}' [new] ===`);
let totalTimePrev = 0
let totalTimeNew = 0
const statsQErrorP50Prev: number[] = []
const statsQErrorP50New: number[] = []
const statsQErrorP95Prev: number[] = []
const statsQErrorP95New: number[] = []
for (const query of this.results) {
const prevQuery = other.results.find(v => v.id === query.id);
if (!prevQuery) {
continue;
}
const timePrev = prevQuery.representativeTime()
const timeNew = query.representativeTime()
if (timePrev !== undefined && timeNew !== undefined) {
totalTimePrev += timePrev
totalTimeNew += timeNew
statsQErrorP50Prev.push(...prevQuery.iterations.flatMap(iter =>
iter.statsQErrorP50 === undefined ? [] : [iter.statsQErrorP50]
))
statsQErrorP50New.push(...query.iterations.flatMap(iter =>
iter.statsQErrorP50 === undefined ? [] : [iter.statsQErrorP50]
))
statsQErrorP95Prev.push(...prevQuery.iterations.flatMap(iter =>
iter.statsQErrorP95 === undefined ? [] : [iter.statsQErrorP95]
))
statsQErrorP95New.push(...query.iterations.flatMap(iter =>
iter.statsQErrorP95 === undefined ? [] : [iter.statsQErrorP95]
))
}
query.compare(prevQuery);
}
let f, tag, emoji
if (totalTimeNew < totalTimePrev) {
f = totalTimePrev / totalTimeNew;
tag = "faster";
emoji = f > 1.2 ? "✅" : "✔";
} else {
f = totalTimeNew / totalTimePrev;
tag = "slower";
emoji = f > 1.2 ? "❌" : "✖";
}
console.log(
`${"TOTAL".padStart(8)}: prev=${totalTimePrev.toString()} ms, new=${totalTimeNew.toString()} ms, diff=${f.toFixed(2)} ${tag} ${emoji}`
);
printQErrorComparison("QERR P50", statsQErrorP50Prev, statsQErrorP50New)
printQErrorComparison("QERR P95", statsQErrorP95Prev, statsQErrorP95New)
}
compareWithPrevious(): void {
const previous = this.loadPrevious();
if (!previous) {
return;
}
this.compare(previous)
}
}
// Class for a single benchmark case
export class BenchResult {
id: string;
dataset: string;
engine: string;
iterations: QueryIter[];
constructor(dataset: string, engine: string, id: string) {
this.dataset = dataset;
this.engine = engine;
this.id = id;
this.iterations = [];
}
// Median (p50) of the successful iteration latencies in ms. The median is robust to
// warmup/GC/noise outliers, and — unlike a mean or a sum — it does not grow with the number
// of iterations, so runs done with different `-i` values stay comparable.
p50(): number {
const xs = this.iterations
.filter(iter => !iter.error)
.map(iter => iter.elapsed)
.sort((a, b) => a - b);
if (xs.length === 0) {
return 0;
}
const mid = Math.floor(xs.length / 2);
const median = xs.length % 2 ? xs[mid] : (xs[mid - 1] + xs[mid]) / 2;
return Math.round(median);
}
// Representative single-run time used to aggregate a suite TOTAL. Returns undefined when any
// iteration errored, so the query is dropped from the total. Because it is a per-query p50
// (not a sum of all iterations), the TOTAL is independent of the iteration count: a run with
// `-i 3` and a run with `-i 5` produce comparable totals.
representativeTime(): undefined | number {
if (this.iterations.some(iter => iter.error)) {
return undefined;
}
return this.p50();
}
compare(prevQuery: BenchResult): void {
const prevErr = prevQuery.iterations.find(v => v.error)?.error;
const newErr = this.iterations.find(v => v.error)?.error;
if (prevErr && !newErr) {
console.log(`${this.id}: Previously failed, but now succeeded 🟠`);
return;
}
if (!prevErr && newErr) {
console.log(`${this.id}: Previously succeeded, but now failed ❌`);
return;
}
if (prevErr && newErr) {
console.log(`${this.id}: Previously failed, and now also failed ❌`);
return;
}
const p50Prev = prevQuery.p50();
const p50 = this.p50();
let f: number;
let tag: string;
let emoji: string;
if (p50 < p50Prev) {
f = p50Prev / p50;
tag = "faster";
emoji = f > 1.2 ? "✅" : "✔";
} else {
f = p50 / p50Prev;
tag = "slower";
emoji = f > 1.2 ? "❌" : "✖";
}
console.log(
`${this.id.padStart(8)}: prev=${p50Prev.toString().padStart(4)} ms, new=${p50.toString().padStart(4)} ms, diff=${f.toFixed(2)} ${tag} ${emoji}`
);
}
store(): void {
const filePath = path.join(
DATA_PATH,
this.dataset,
RESULTS_DIR,
this.engine,
`${this.id}.json`
);
// Ensure directory exists
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const json = JSON.stringify(this, null, 2);
fs.writeFileSync(filePath, json);
}
static load(dataset: string, engine: string, id: string): BenchResult | null {
const filePath = path.join(
DATA_PATH,
dataset,
RESULTS_DIR,
engine,
`${id}.json`
);
try {
const parser = z.object({
dataset: z.string(),
engine: z.string(),
id: z.string(),
iterations: z.object({
rowCount: z.number(),
elapsed: z.number(),
error: z.string().optional(),
plan: z.string(),
tasks: z.number().default(0),
statsQErrorP50: z.number().optional(),
statsQErrorP95: z.number().optional(),
}).array(),
})
const data = fs.readFileSync(filePath, 'utf-8');
const parsed = parser.parse(JSON.parse(data))
const result = new BenchResult(
parsed.dataset,
parsed.engine,
parsed.id
)
result.iterations = parsed.iterations
return result;
} catch {
return null;
}
}
static loadMany(dataset: string, engine: string): BenchResult[] {
const resultsDir = path.join(DATA_PATH, dataset, RESULTS_DIR, engine);
const results: BenchResult[] = [];
try {
const files = fs.readdirSync(resultsDir);
for (const fileName of files) {
if (!fileName.endsWith('.json')) {
continue;
}
const id = fileName.slice(0, -5); // Remove .json extension
const result = BenchResult.load(dataset, engine, id);
if (result) {
results.push(result);
}
}
} catch {
// Directory doesn't exist or can't be read
return results;
}
results.sort((a, b) => numericId(a.id) > numericId(b.id) ? 1 : -1)
return results;
}
}
function numericId(queryName: string): number {
return parseInt([...queryName.matchAll(/(\d+)/g)][0][0])
}
function printQErrorComparison(label: string, prev: number[], next: number[]): void {
const prevValue = median(prev)
const nextValue = median(next)
if (prevValue !== undefined && nextValue !== undefined) {
console.log(`${label.padStart(8)}: prev=${prevValue.toFixed(2)}x, new=${nextValue.toFixed(2)}x`)
} else if (prevValue !== undefined) {
console.log(`${label.padStart(8)}: prev=${prevValue.toFixed(2)}x, new=n/a`)
} else if (nextValue !== undefined) {
console.log(`${label.padStart(8)}: prev=n/a, new=${nextValue.toFixed(2)}x`)
}
}
function median(values: number[]): number | undefined {
if (values.length === 0) {
return undefined
}
const sorted = [...values].sort((a, b) => a - b)
const mid = Math.floor(sorted.length / 2)
return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2
}