-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy path@bench-common.ts
More file actions
221 lines (192 loc) · 6.96 KB
/
Copy path@bench-common.ts
File metadata and controls
221 lines (192 loc) · 6.96 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
import path from "path";
import fs from "fs/promises";
import * as fsSync from "fs";
import {BenchmarkRun, BenchResult} from "./@results";
export const ROOT = path.join(__dirname, '../../..')
const STACK_NAME = "DataFusionDistributedBenchmarks"
const CDK_OUTPUT_FILE = path.join(__dirname, "..", ".cdk-outputs.json")
function normalizeBucketUri(bucket: string): string {
const withoutProtocol = bucket.replace(/^s3:\/\//, "").replace(/\/+$/, "")
return `s3://${withoutProtocol}`
}
function getBucketFromLocalOutputs(): string | undefined {
// Prefer local deploy metadata over live AWS lookups to keep benchmark scripts deterministic
// and independent from shell/profile/region drift.
try {
const raw = fsSync.readFileSync(CDK_OUTPUT_FILE, "utf-8")
const outputs = JSON.parse(raw) as Record<string, Record<string, unknown>>
const value = outputs[STACK_NAME]?.["BenchmarkBucketName"]
if (typeof value === "string" && value.trim() !== "") {
return normalizeBucketUri(value)
}
} catch {
// Ignore read/parse issues and fall back to env-based configuration.
}
return undefined
}
function resolveBucketUri(): string {
// Keep resolution local-first to avoid live AWS calls during benchmark script startup.
// Resolution order:
// 1) explicit env override
// 2) local CDK outputs produced by `npm run deploy -- --outputs-file ...`
const fromEnv = process.env.BENCHMARK_BUCKET
if (fromEnv) {
return normalizeBucketUri(fromEnv)
}
const fromLocalOutput = getBucketFromLocalOutputs()
if (fromLocalOutput) {
return fromLocalOutput
}
throw new Error(
"Could not resolve benchmark bucket. Set BENCHMARK_BUCKET or deploy DataFusionDistributedBenchmarks with --outputs-file .cdk-outputs.json."
)
}
let resolvedBucketUri: string | undefined
export function getBucketUri(): string {
// Resolve once per process; bucket does not change during a single benchmark invocation.
if (!resolvedBucketUri) {
resolvedBucketUri = resolveBucketUri()
}
return resolvedBucketUri
}
export interface TableSpec {
schema: string
name: string
s3Path: string
}
export interface ExecuteQueryResult {
rowCount: number,
plan: string
elapsed: number
tasks: number
statsQErrorP50?: number
statsQErrorP95?: number
}
export interface BenchmarkRunner {
createTables(s3Paths: TableSpec[]): Promise<void>;
executeQuery(query: string): Promise<ExecuteQueryResult>;
}
async function tablePathsForDataset(dataset: string): Promise<TableSpec[]> {
const datasetPath = path.join(ROOT, "benchmarks", "data", dataset)
const bucketUri = getBucketUri()
const result: TableSpec[] = []
for (const entryName of await fs.readdir(datasetPath)) {
const dir = path.join(datasetPath, entryName)
if (await isDirWithAllParquetFiles(dir)) {
result.push({
name: entryName,
schema: dataset,
s3Path: `${bucketUri}/${dataset}/${entryName}/`
})
}
}
return result
}
async function isDirWithAllParquetFiles(dir: string): Promise<boolean> {
let readDir
try {
readDir = await fs.readdir(dir)
} catch (e) {
return false
}
for (const file of readDir) {
if (!file.endsWith(".parquet")) {
return false
}
}
return true
}
async function queriesForDataset(dataset: string): Promise<{ id: string, sql: string }[]> {
const datasetSuffix = dataset.split("_")[0]
const queriesPath = path.join(ROOT, "testdata", datasetSuffix, "queries")
const queries = []
for (const fileName of await fs.readdir(queriesPath)) {
const sql = await fs.readFile(path.join(queriesPath, fileName), 'utf-8');
queries.push({ id: fileName.replace(".sql", ""), sql })
}
queries.sort((a, b) => numericId(a.id) > numericId(b.id) ? 1 : -1)
return queries
}
function numericId(queryName: string): number {
return parseInt([...queryName.matchAll(/(\d+)/g)][0][0])
}
export async function runBenchmark(
runner: BenchmarkRunner,
options: {
dataset: string
engine: string,
iterations: number;
queries: string[];
debug: boolean;
warmup: boolean;
}
) {
const { dataset, engine, iterations, queries, warmup, debug } = options;
const benchmarkRun = new BenchmarkRun(dataset, engine)
console.log("Creating tables...");
const s3Paths = await tablePathsForDataset(dataset)
await runner.createTables(s3Paths);
for (const { id, sql } of await queriesForDataset(dataset)) {
if (queries.length > 0 && !queries.includes(id)) {
continue;
}
const result = new BenchResult(dataset, engine, id)
if (warmup) {
console.log(`Warming up query ${id}...`)
try {
await runner.executeQuery(sql);
} catch (e: any) {
result.iterations.push({
elapsed: 0,
rowCount: 0,
error: e.toString(),
plan: "",
tasks: 0
})
console.error(`Query ${id} failed: ${e.toString()}`)
continue
}
}
for (let i = 0; i < iterations; i++) {
let response
try {
response = await runner.executeQuery(sql);
} catch (e: any) {
result.iterations.push({
elapsed: 0,
rowCount: 0,
error: e.toString(),
plan: "",
tasks: 0
})
console.error(`Query ${id} failed: ${e.toString()}`)
break
}
if (debug) {
console.log(response.plan)
}
result.iterations.push({
elapsed: response.elapsed,
rowCount: response.rowCount,
plan: response.plan,
tasks: response.tasks,
statsQErrorP50: response.statsQErrorP50,
statsQErrorP95: response.statsQErrorP95,
})
if (response.statsQErrorP50 !== undefined && response.statsQErrorP95 !== undefined) {
console.log(
`Query ${id} iteration ${i} took ${Math.round(response.elapsed)} ms, stats q-error P50 ${response.statsQErrorP50.toFixed(2)}x, P95 ${response.statsQErrorP95.toFixed(2)}x and returned ${response.rowCount} rows`
);
} else {
console.log(
`Query ${id} iteration ${i} took ${Math.round(response.elapsed)} ms and returned ${response.rowCount} rows`
);
}
}
console.log(`Query ${id} p50 time: ${result.p50()} ms`);
benchmarkRun.results.push(result)
}
// Write results and compare
benchmarkRun.compareWithPrevious()
benchmarkRun.store()
}