-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstress-check.js
More file actions
149 lines (137 loc) · 4.57 KB
/
Copy pathstress-check.js
File metadata and controls
149 lines (137 loc) · 4.57 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
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
const {
buildTraversalStructure,
createCompilation,
layoutTraversalStructure,
projectTraversal
} = require("../pgx-core");
const root = path.resolve(__dirname, "..");
const profile = JSON.parse(fs.readFileSync(path.join(root, "pgx-profile.json"), "utf8"));
const source = fs.readFileSync(path.join(root, "stress-showcase.pgx"), "utf8");
const compilation = createCompilation({
source,
sourceId: "stress-showcase.pgx",
profile
});
function fail(message) {
throw new Error(message);
}
const compileErrors = compilation.diagnostics.filter((entry) => entry.severity === "error");
if (compileErrors.length > 0) {
fail(compileErrors.map((entry) => entry.message).join("\n"));
}
const declarations = new Map(
compilation.document.nodes.map((node) => [node.pointer, node])
);
const registry = new Map(
compilation.profile.nodes.map((node) => [node.pointer, node])
);
if (registry.size !== compilation.profile.nodes.length) {
fail("The profile contains duplicate pointer identities.");
}
if (declarations.size !== compilation.document.nodes.length) {
fail("The showcase contains duplicate pointer identities.");
}
if (registry.size !== declarations.size) {
fail(`Profile has ${registry.size} nodes; showcase has ${declarations.size}.`);
}
for (const [pointer, node] of registry) {
const declaration = declarations.get(pointer);
if (!declaration) {
fail(`Showcase is missing ${pointer}.`);
}
if (declaration.title !== node.title || declaration.description !== node.description) {
fail(`Profile/showcase drift at ${pointer}.`);
}
}
const unresolved = compilation.validateReferences({
layer: "profile",
layers: ["profile"]
});
if (unresolved.length > 0) {
fail(unresolved.map((entry) => entry.message).join("\n"));
}
const cycles = compilation.findCycles({ layer: "profile", layers: ["profile"] });
if (cycles.length > 0) {
fail(`Profile graph contains ${cycles.length} expansion cycle(s).`);
}
function resolveRoot() {
return projectTraversal(compilation, "[(A109)]", { line: 0 }, {
display: "title",
inlineDepth: Infinity,
layers: ["profile"],
unresolved: "error",
cycle: "error",
maxDepth: 32,
maxPointerVisits: 100000,
maxExpandedNodes: 100000,
maxOutputCharacters: 1000000
});
}
const first = resolveRoot();
if (!first.ok) {
fail(first.diagnostics.map((entry) => entry.message).join("\n"));
}
const resolvedSha256 = crypto.createHash("sha256").update(first.text).digest("hex");
const expectedSha256 = "4b50c20186f1bff148f1957cca7ddfe067b3e7ce56e40907c716f68b8eaae9a1";
if (resolvedSha256 !== expectedSha256) {
fail(`A109 projection geometry drifted: ${resolvedSha256}.`);
}
const stressNode = declarations.get("A109");
const stressStructure = buildTraversalStructure(
compilation,
stressNode.description,
{ line: stressNode.lineNumber },
{
baseOffset: stressNode.descriptionSpan.start,
layers: ["document", "profile"]
}
);
const stressGeometry = layoutTraversalStructure(stressStructure);
if (
stressStructure.pointerCount !== 12 ||
stressStructure.groupCount !== 4 ||
stressStructure.depth !== 1
) {
fail("A109 authored traversal structure drifted.");
}
if (stressGeometry.nodes.length !== 17 || stressGeometry.edges.length !== 16) {
fail("A109 mini-view geometry drifted.");
}
const iterations = 1000;
const start = process.hrtime.bigint();
for (let index = 0; index < iterations; index += 1) {
const projection = resolveRoot();
if (!projection.ok) {
fail("Repeated stress projection failed.");
}
}
const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6;
console.log(
JSON.stringify(
{
schemaProfile: compilation.schemaProfile,
nodes: registry.size,
traversalReferences: compilation.references({ layer: "profile" }).length,
graphEdges: compilation.graphView({ layer: "profile", layers: ["profile"] }).edges.length,
stressRoot: "A109",
authoredPointers: stressStructure.pointerCount,
authoredGroups: stressStructure.groupCount,
authoredDepth: stressStructure.depth,
miniViewNodes: stressGeometry.nodes.length,
miniViewEdges: stressGeometry.edges.length,
resolvedCharacters: first.text.length,
resolvedSha256,
pointerVisits: first.stats.pointerVisits,
expandedConstruals: first.stats.expandedNodes,
maximumRecursiveDepth: first.stats.maximumDepth,
benchmarkIterations: iterations,
benchmarkMilliseconds: Number(elapsedMs.toFixed(2)),
averageMilliseconds: Number((elapsedMs / iterations).toFixed(4))
},
null,
2
)
);