-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbenchmark-incremental.ts
More file actions
197 lines (176 loc) · 7.23 KB
/
Copy pathbenchmark-incremental.ts
File metadata and controls
197 lines (176 loc) · 7.23 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
import Modelica from "@modelscript/modelica/parser";
import fs from "node:fs";
import path from "node:path";
import { performance } from "node:perf_hooks";
import Parser from "tree-sitter";
import { Context } from "../packages/core/src/compiler/context.js";
import { NodeFileSystem } from "../packages/core/tests/node-filesystem.js";
// Suppress excessive logging
const originalLog = console.log;
const originalError = console.error;
console.log = (...args) => {
if (typeof args[0] === "string" && args[0].includes("[DEBUG ARENA EVAL NAME]")) return;
originalLog(...args);
};
console.error = (...args) => {
if (typeof args[0] === "string" && args[0].includes("[Context] toUnified")) return;
originalError(...args);
};
const parser = new Parser();
parser.setLanguage(Modelica);
Context.registerParser(".mo", parser);
function indexToPoint(text: string, index: number) {
let row = 0;
let lastNewline = -1;
for (let i = 0; i < index; i++) {
if (text[i] === "\n") {
row++;
lastNewline = i;
}
}
return { row, column: index - lastNewline - 1 };
}
function computeTreeEdit(oldText: string, newText: string) {
const minLen = Math.min(oldText.length, newText.length);
let prefixLen = 0;
while (prefixLen < minLen && oldText[prefixLen] === newText[prefixLen]) {
prefixLen++;
}
let oldSuffix = oldText.length;
let newSuffix = newText.length;
while (oldSuffix > prefixLen && newSuffix > prefixLen && oldText[oldSuffix - 1] === newText[newSuffix - 1]) {
oldSuffix--;
newSuffix--;
}
return {
startIndex: prefixLen,
oldEndIndex: oldSuffix,
newEndIndex: newSuffix,
startPosition: indexToPoint(oldText, prefixLen),
oldEndPosition: indexToPoint(oldText, oldSuffix),
newEndPosition: indexToPoint(newText, newSuffix),
};
}
function loadIncremental(ctx: Context, parser: any, oldText: string, newText: string, uri: string) {
const oldTree = (ctx as any)._trees.get(uri);
const edit = computeTreeEdit(oldText, newText);
oldTree.edit(edit);
const newTree = parser.parse(newText, oldTree);
(ctx as any)._trees.set(uri, newTree);
ctx.workspaceIndex.markDirty(uri, () => newTree.rootNode as any, [
{
startByte: edit.startIndex,
endByte: edit.newEndIndex,
},
]);
const unified = ctx.workspaceIndex.toUnified();
ctx.queryEngine.updateIndex(unified);
}
function generateHeatConduction1D(n: number): string {
let code = `model HeatConduction1D_${n}\n`;
code += ` parameter Integer N = ${n};\n`;
code += ` parameter Real L = 1.0;\n`;
code += ` parameter Real dx = L / N;\n`;
code += ` parameter Real alpha = 1e-4;\n\n`;
code += ` Real T[N] (start=zeros(N));\n\n`;
code += `equation\n`;
code += ` der(T[1]) = alpha * (100.0 - 2.0*T[1] + T[2]) / (dx^2);\n`;
for (let i = 2; i <= n - 1; i++) {
code += ` der(T[${i}]) = alpha * (T[${i - 1}] - 2.0*T[${i}] + T[${i + 1}]) / (dx^2);\n`;
}
if (n > 1) {
code += ` der(T[${n}]) = alpha * (T[${n - 1}] - T[${n}]) / (dx^2);\n`;
}
code += ` annotation(\n`;
code += ` experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 0.1)\n`;
code += ` );\n`;
code += `end HeatConduction1D_${n};`;
return code;
}
async function run() {
let Ns = [10, 100, 1000, 10000, 100000];
const args = process.argv.slice(2);
if (args.length > 0) {
const singleN = parseInt(args[0], 10);
if (!isNaN(singleN)) {
Ns = [singleN];
}
}
const results = {
n_equations: Ns,
cold_start: [] as number[],
incremental_lexical: [] as number[],
incremental_modifier: [] as number[],
incremental_equation: [] as number[],
incremental_structural: [] as number[],
};
for (const N of Ns) {
originalLog(`\nRunning benchmark for N = ${N}...`);
const sourceCode = generateHeatConduction1D(N);
const className = `HeatConduction1D_${N}`;
// Cold Start
const ctx = new Context(new NodeFileSystem());
const t0 = performance.now();
ctx.load(sourceCode, "benchmark.mo");
await ctx.queryEngine.runAllLintsAsync("benchmark.mo");
const coldStartTime = performance.now() - t0;
results.cold_start.push(Math.round(coldStartTime));
originalLog(` Cold Start: ${Math.round(coldStartTime)} ms (Revision: ${ctx.workspaceIndex.structuralRevision})`);
// Lexical Mutation (rename parameter L to L_new)
let mutatedLexical = sourceCode.replace("parameter Real L = 1.0;", "parameter Real L_new = 1.0;");
mutatedLexical = mutatedLexical.replace(/ L /g, " L_new ");
const t1 = performance.now();
loadIncremental(ctx, parser, sourceCode, mutatedLexical, "benchmark.mo");
await ctx.queryEngine.runAllLintsAsync("benchmark.mo");
const lexicalTime = performance.now() - t1;
results.incremental_lexical.push(Math.round(lexicalTime));
originalLog(
` Lexical Mutation: ${Math.round(lexicalTime)} ms (Revision: ${ctx.workspaceIndex.structuralRevision})`,
);
// Modifier Mutation (change start=zeros(N) to start=ones(N))
const mutatedModifier = mutatedLexical.replace("start=zeros(N)", "start=ones(N)");
const t2 = performance.now();
loadIncremental(ctx, parser, mutatedLexical, mutatedModifier, "benchmark.mo");
await ctx.queryEngine.runAllLintsAsync("benchmark.mo");
const modifierTime = performance.now() - t2;
results.incremental_modifier.push(Math.round(modifierTime));
originalLog(
` Modifier Mutation: ${Math.round(modifierTime)} ms (Revision: ${ctx.workspaceIndex.structuralRevision})`,
);
// Equation Mutation (change 100.0 to 200.0 in the first equation)
const mutatedEquation = mutatedModifier.replace("100.0 - 2.0*T[1]", "200.0 - 2.0*T[1]");
const t3 = performance.now();
loadIncremental(ctx, parser, mutatedModifier, mutatedEquation, "benchmark.mo");
await ctx.queryEngine.runAllLintsAsync("benchmark.mo");
const equationTime = performance.now() - t3;
results.incremental_equation.push(Math.round(equationTime));
originalLog(
` Equation Mutation: ${Math.round(equationTime)} ms (Revision: ${ctx.workspaceIndex.structuralRevision})`,
);
// Structural Mutation (add cross-coupling algebraic loop: T[1] = T[N])
const mutatedStructural = mutatedEquation.replace("equation\n", "equation\n T[1] = T[N]; // structural loop\n");
const t4 = performance.now();
loadIncremental(ctx, parser, mutatedEquation, mutatedStructural, "benchmark.mo");
await ctx.queryEngine.runAllLintsAsync("benchmark.mo");
const structuralTime = performance.now() - t4;
results.incremental_structural.push(Math.round(structuralTime));
originalLog(
` Structural Mutation: ${Math.round(structuralTime)} ms (Revision: ${ctx.workspaceIndex.structuralRevision})`,
);
// Attempt GC
Context.gcBetweenPhases();
}
const outPath = path.resolve("./results/measurements.json");
if (!fs.existsSync(path.dirname(outPath))) {
fs.mkdirSync(path.dirname(outPath), { recursive: true });
}
// Read existing json to merge data instead of overwriting the whole file
let existingData: any = {};
if (fs.existsSync(outPath)) {
existingData = JSON.parse(fs.readFileSync(outPath, "utf8"));
}
existingData.incremental_latency = results;
fs.writeFileSync(outPath, JSON.stringify(existingData, null, 2));
originalLog(`\nResults written to ${outPath}`);
}
run().catch(console.error);