-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcte-layout.bench.ts
More file actions
138 lines (128 loc) · 3.58 KB
/
Copy pathcte-layout.bench.ts
File metadata and controls
138 lines (128 loc) · 3.58 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
import { bench, describe } from "vitest";
import {
analyzeSqlCteLayout,
MAX_CTE_DECLARATIONS,
MAX_CTE_DEPTH,
MAX_CTE_FRAMES,
type SqlCteLayoutDialect,
visibleSqlCtesAt,
} from "../cte-layout.js";
import { DUCKDB_SQL_LEXICAL_PROFILE } from "../lexical.js";
import { createIdentitySqlSource } from "../source.js";
import {
buildSqlStatementIndex,
type ExactSqlStatementSlot,
} from "../statement-index.js";
const dialect: SqlCteLayoutDialect = {
classifyIdentifierToken: (rawIdentifier, quoted) => ({
status: "identifier",
value: {
component: { quoted, value: rawIdentifier },
},
}),
compareCteIdentifiers: (left, right) =>
left.value.toLowerCase() === right.value.toLowerCase()
? "equal"
: "distinct",
grammar: {
declaredColumns: true,
materialization: true,
maximumDeclarationsPerFrame: MAX_CTE_DECLARATIONS,
recursive: true,
},
lexicalProfile: DUCKDB_SQL_LEXICAL_PROFILE,
};
function fixture(text: string): {
readonly source: ReturnType<typeof createIdentitySqlSource>;
readonly slot: ExactSqlStatementSlot;
} {
const source = createIdentitySqlSource(text);
const slot = buildSqlStatementIndex(
source.analysisText,
dialect.lexicalProfile,
).slots[0];
if (!slot || slot.boundaryQuality !== "exact") {
throw new Error("CTE benchmark fixture requires an exact statement");
}
return { slot, source };
}
const tenKibibytes = 10 * 1_024;
const ordinaryPrefix = "SELECT ";
const ordinary = `${ordinaryPrefix}${"x,".repeat(
Math.floor((tenKibibytes - ordinaryPrefix.length - 1) / 2),
)}x`;
const ordinaryText = `${ordinary}${" ".repeat(
tenKibibytes - ordinary.length,
)}`;
const ordinaryFixture = fixture(ordinaryText);
const declarationHeavyText = `WITH ${Array.from(
{ length: MAX_CTE_DECLARATIONS },
(_, index) => `c${index} AS (SELECT ${index})`,
).join(", ")} SELECT * FROM c255`;
const declarationHeavyFixture = fixture(declarationHeavyText);
const depthHeavyText = Array.from(
{ length: MAX_CTE_DEPTH },
(_, index) => index,
).reduce(
(body, index) =>
`WITH c${index} AS (${body}) SELECT * FROM c${index}`,
"SELECT 1",
);
const depthHeavyFixture = fixture(depthHeavyText);
const bareFrameHeavyText = `SELECT ${Array.from(
{ length: MAX_CTE_FRAMES },
() => "(WITH)",
).join(",")}`;
const bareFrameHeavyFixture = fixture(bareFrameHeavyText);
const depthLayout = analyzeSqlCteLayout(
depthHeavyFixture.source,
depthHeavyFixture.slot,
dialect,
);
if (depthLayout.status !== "ready") {
throw new Error("Depth benchmark requires a ready CTE layout");
}
const projectedLayout = analyzeSqlCteLayout(
declarationHeavyFixture.source,
declarationHeavyFixture.slot,
dialect,
);
if (projectedLayout.status === "unavailable") {
throw new Error("CTE projection benchmark requires a layout");
}
describe("CTE layout", () => {
bench("ordinary 10 KiB statement", () => {
analyzeSqlCteLayout(
ordinaryFixture.source,
ordinaryFixture.slot,
dialect,
);
});
bench("256 declarations", () => {
analyzeSqlCteLayout(
declarationHeavyFixture.source,
declarationHeavyFixture.slot,
dialect,
);
});
bench("128-depth nested CTE", () => {
analyzeSqlCteLayout(
depthHeavyFixture.source,
depthHeavyFixture.slot,
dialect,
);
});
bench("256 sequential incomplete frames", () => {
analyzeSqlCteLayout(
bareFrameHeavyFixture.source,
bareFrameHeavyFixture.slot,
dialect,
);
});
bench("cached 256-declaration projection", () => {
visibleSqlCtesAt(
projectedLayout,
declarationHeavyText.length - 1,
);
});
});