Repository navigation
Expand file tree
/
Copy pathbenchmark-node-identity.ts
More file actions
347 lines (330 loc) · 11.1 KB
/
Copy pathbenchmark-node-identity.ts
File metadata and controls
347 lines (330 loc) · 11.1 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* Reproducible, headless measurements for public NodeState identity operations.
*
* Run with: pnpm exec tsx scripts/benchmark-node-identity.ts
* The values are diagnostic medians, not pass/fail timing assertions. Run each
* revision on the same machine and avoid other CPU-heavy tasks while comparing.
*/
import { execFileSync } from 'node:child_process';
import { createHash } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { performance } from 'node:perf_hooks';
import {
$createParagraphNode,
$createTextNode,
$getNodeByKey,
$getRoot,
$isTextNode,
createEditor,
type LexicalEditor,
RootNode,
} from 'lexical';
import {
$getNodeById,
$getNodeId,
$normalizeNodeIds,
$setNodeId,
} from '../src/plugins/common/node/node-id';
import { registerNodeIdentityPolicy } from '../src/plugins/common/node/node-identity-policy';
import { NodeIdentityService } from '../src/plugins/common/service/node-identity-service';
import { liteXmlIdentityPolicy } from '../src/plugins/litexml/node/identity-policy';
const SIZES = [100, 1000, 5000];
const LOOKUPS = 1000;
const EDITS = 20;
const WARMUPS = 2;
const SAMPLES = 3;
const POLICIES = ['default', 'liteXml'] as const;
type PolicyMode = (typeof POLICIES)[number];
let checksum = 0;
let serial = 0;
type Metric = { medianMs: number; samplesMs: number[] };
type Measurement = {
policy: PolicyMode;
paragraphs: number;
nodes: number;
construction: Metric;
repeatedTailLookupOneRead: Metric;
repeatedTailLookupSeparateReads: Metric;
repeatedTailLookupActiveUpdate: Metric;
singleTextEdit: Metric;
repeatedTextEdits: Metric;
repeatedIdEdits: Metric;
batchedIdEdits: Metric;
serviceColdTailLookup: Metric;
serviceWarmTailLookups: Metric;
};
function median(samples: number[]): number {
const sorted = [...samples].sort((a, b) => a - b);
return sorted[Math.floor(sorted.length / 2)];
}
function measure(run: () => void, warmups = WARMUPS, samples = SAMPLES): Metric {
const times: number[] = [];
for (let index = -warmups; index < samples; index++) {
const start = performance.now();
run();
const elapsed = performance.now() - start;
if (index >= 0) times.push(Number(elapsed.toFixed(3)));
}
return { medianMs: median(times), samplesMs: times };
}
function measureElapsed(run: () => number, warmups = WARMUPS, samples = SAMPLES): Metric {
const times: number[] = [];
for (let index = -warmups; index < samples; index++) {
const elapsed = run();
if (index >= 0) times.push(Number(elapsed.toFixed(3)));
}
return { medianMs: median(times), samplesMs: times };
}
function readCommitted<T>(editor: LexicalEditor, reader: () => T): T {
return editor.getEditorState().read(reader, { editor });
}
function newEditor(policy: PolicyMode): LexicalEditor {
const editor = createEditor({
namespace: 'node-identity-benchmark',
onError(error) {
throw error;
},
});
if (policy === 'liteXml') registerNodeIdentityPolicy(editor, liteXmlIdentityPolicy);
editor.registerNodeTransform(RootNode, $normalizeNodeIds);
return editor;
}
function createDocument(
paragraphs: number,
policy: PolicyMode,
): {
editor: LexicalEditor;
firstTextKey: string;
tailTextKey: string;
tailTextId: string;
textKeys: string[];
} {
const editor = newEditor(policy);
const textKeys: string[] = [];
editor.update(
() => {
const root = $getRoot();
for (let index = 0; index < paragraphs; index++) {
const paragraph = $createParagraphNode();
const text = $createTextNode(`paragraph ${index}`);
paragraph.append(text);
root.append(paragraph);
textKeys.push(text.getKey());
}
},
{ discrete: true },
);
const tailTextKey = textKeys.at(-1)!;
const tailTextId = readCommitted(editor, () => {
const node = $getNodeByKey(tailTextKey);
if (!$isTextNode(node)) throw new Error('Tail text is missing after construction.');
const id = $getNodeId(node);
if (!id) throw new Error('Root transform did not assign a public ID.');
return id;
});
return { editor, firstTextKey: textKeys[0], tailTextKey, tailTextId, textKeys };
}
function updateText(editor: LexicalEditor, textKey: string): void {
const value = `edited ${++serial}`;
editor.update(
() => {
const node = $getNodeByKey(textKey);
if (!$isTextNode(node)) throw new Error(`Text node ${textKey} went missing.`);
node.setTextContent(value);
},
{ discrete: true },
);
}
function updateId(editor: LexicalEditor, textKey: string): void {
const id = `benchmark-id-${++serial}`;
editor.update(
() => {
const node = $getNodeByKey(textKey);
if (!$isTextNode(node)) throw new Error(`Text node ${textKey} went missing.`);
$setNodeId(node, id);
},
{ discrete: true },
);
}
function benchmark(paragraphs: number, policy: PolicyMode): Measurement {
let document: ReturnType<typeof createDocument> | undefined;
const construction = measure(
() => {
document = createDocument(paragraphs, policy);
},
1,
SAMPLES,
);
if (!document) throw new Error('Document construction failed.');
const { editor, firstTextKey, tailTextId, textKeys } = document;
const editKeys = textKeys.slice(0, EDITS);
const initialEditIds = readCommitted(editor, () =>
editKeys.map((key) => {
const node = $getNodeByKey(key);
if (!$isTextNode(node)) throw new Error(`Text node ${key} went missing.`);
const id = $getNodeId(node);
if (!id) throw new Error(`Text node ${key} has no public ID.`);
return id;
}),
);
const repeatedTailLookupOneRead = measure(() => {
readCommitted(editor, () => {
for (let index = 0; index < LOOKUPS; index++) {
const node = $getNodeById(tailTextId);
if (!node) throw new Error('Tail lookup failed.');
checksum += node.getKey().length;
}
});
});
const repeatedTailLookupSeparateReads = measure(() => {
for (let index = 0; index < LOOKUPS; index++) {
readCommitted(editor, () => {
const node = $getNodeById(tailTextId);
if (!node) throw new Error('Tail lookup failed.');
checksum += node.getKey().length;
});
}
});
const repeatedTailLookupActiveUpdate = measureElapsed(() => {
let elapsed = 0;
editor.update(
() => {
const first = $getNodeByKey(firstTextKey);
if (!$isTextNode(first)) throw new Error('First text node went missing.');
const activeId = `benchmark-active-${++serial}`;
$setNodeId(first, activeId);
if ($getNodeById(activeId)?.getKey() !== firstTextKey) {
throw new Error('Fresh ID is invisible inside the active update.');
}
const start = performance.now();
for (let index = 0; index < LOOKUPS; index++) {
const node = $getNodeById(tailTextId);
if (!node) throw new Error('Tail lookup failed inside an active update.');
checksum += node.getKey().length;
}
elapsed = performance.now() - start;
},
{ discrete: true },
);
return elapsed;
});
const singleTextEdit = measure(() => updateText(editor, firstTextKey));
const repeatedTextEdits = measure(
() => {
for (const key of editKeys) updateText(editor, key);
},
1,
SAMPLES,
);
const repeatedIdEdits = measure(
() => {
for (const key of editKeys) updateId(editor, key);
},
1,
SAMPLES,
);
const batchedIdEdits = measure(() => {
editor.update(
() => {
for (const key of editKeys) {
const node = $getNodeByKey(key);
if (!$isTextNode(node)) throw new Error(`Text node ${key} went missing.`);
$setNodeId(node, `benchmark-id-${++serial}`);
}
},
{ discrete: true },
);
});
readCommitted(editor, () => {
for (let index = 0; index < editKeys.length; index++) {
const key = editKeys[index];
const node = $getNodeByKey(key);
if (!$isTextNode(node)) throw new Error(`Text node ${key} went missing after ID edits.`);
const id = $getNodeId(node);
if (!id || $getNodeById(id)?.getKey() !== key) {
throw new Error(`Current ID of text node ${key} cannot be resolved.`);
}
if ($getNodeById(initialEditIds[index])) {
throw new Error(`Original ID of text node ${key} is stale.`);
}
}
});
const service = new NodeIdentityService();
const unbind = service.bindEditor(editor);
const serviceColdTailLookup = measureElapsed(() => {
updateText(editor, firstTextKey);
const start = performance.now();
const snapshot = service.getNodeById(tailTextId);
const elapsed = performance.now() - start;
if (snapshot?.id !== tailTextId) throw new Error('Cold service lookup failed.');
checksum += snapshot.textContent.length;
return elapsed;
});
const serviceWarmTailLookups = measure(() => {
for (let index = 0; index < LOOKUPS; index++) {
const snapshot = service.getNodeById(tailTextId);
if (snapshot?.id !== tailTextId) throw new Error('Warm service lookup failed.');
checksum += snapshot.textContent.length;
}
});
unbind();
return {
policy,
paragraphs,
nodes: paragraphs * 2,
construction,
repeatedTailLookupOneRead,
repeatedTailLookupSeparateReads,
repeatedTailLookupActiveUpdate,
singleTextEdit,
repeatedTextEdits,
repeatedIdEdits,
batchedIdEdits,
serviceColdTailLookup,
serviceWarmTailLookups,
};
}
const commit = execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim();
const source = readFileSync(new URL('../src/plugins/common/node/node-id.ts', import.meta.url));
const sourceHash = createHash('sha256').update(source).digest('hex');
const policyRegistrySource = readFileSync(
new URL('../src/plugins/common/node/node-identity-policy.ts', import.meta.url),
);
const policyRegistrySourceHash = createHash('sha256').update(policyRegistrySource).digest('hex');
const policySource = readFileSync(
new URL('../src/plugins/litexml/node/identity-policy.ts', import.meta.url),
);
const policySourceHash = createHash('sha256').update(policySource).digest('hex');
const serviceContractSource = readFileSync(
new URL('../src/plugins/common/service/i-node-identity-service.ts', import.meta.url),
);
const serviceContractSourceHash = createHash('sha256').update(serviceContractSource).digest('hex');
const serviceSource = readFileSync(
new URL('../src/plugins/common/service/node-identity-service.ts', import.meta.url),
);
const serviceSourceHash = createHash('sha256').update(serviceSource).digest('hex');
const benchmarkSource = readFileSync(new URL(import.meta.url));
const benchmarkSourceHash = createHash('sha256').update(benchmarkSource).digest('hex');
const output = {
commit,
sourceHash,
policyRegistrySourceHash,
policySourceHash,
serviceContractSourceHash,
serviceSourceHash,
benchmarkSourceHash,
node: process.version,
platform: process.platform,
arch: process.arch,
parameters: {
sizes: SIZES,
policies: POLICIES,
lookups: LOOKUPS,
edits: EDITS,
warmups: WARMUPS,
samples: SAMPLES,
},
measurements: POLICIES.flatMap((policy) => SIZES.map((size) => benchmark(size, policy))),
checksum,
};
process.stdout.write(`${JSON.stringify(output, null, 2)}\n`);