-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathperformance.vscode.test.ts
140 lines (122 loc) · 3.91 KB
/
performance.vscode.test.ts
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
import {
asyncSafety,
type ActionDescriptor,
type ScopeType,
type SimpleScopeTypeType,
} from "@cursorless/common";
import { openNewEditor, runCursorlessCommand } from "@cursorless/vscode-common";
import assert from "assert";
import * as vscode from "vscode";
import { endToEndTestSetup } from "../endToEndTestSetup";
const testData = generateTestData();
const numLines = testData.split("\n").length;
suite(`Performance: ${numLines} lines JSON`, async function () {
endToEndTestSetup(this);
let previousTitle = "";
this.beforeEach(function () {
const title = this.currentTest!.title;
if (title !== previousTitle) {
console.log(` ${title}`);
previousTitle = title;
}
});
const textBasedThreshold = 100;
const parseTreeThreshold = 500;
const surroundingPairThreshold = 30000;
test(
"Remove token",
asyncSafety(() => removeToken(textBasedThreshold)),
);
const fixtures: [SimpleScopeTypeType | ScopeType, number][] = [
// Text based
["character", textBasedThreshold],
["word", textBasedThreshold],
["token", textBasedThreshold],
["identifier", textBasedThreshold],
["line", textBasedThreshold],
["sentence", textBasedThreshold],
["paragraph", textBasedThreshold],
["document", textBasedThreshold],
["nonWhitespaceSequence", textBasedThreshold],
// Parse tree based
["string", parseTreeThreshold],
["map", parseTreeThreshold],
["collectionKey", parseTreeThreshold],
["value", parseTreeThreshold],
// Text based, but utilizes surrounding pair
["boundedParagraph", surroundingPairThreshold],
["boundedNonWhitespaceSequence", surroundingPairThreshold],
["collectionItem", surroundingPairThreshold],
// Surrounding pair
[{ type: "surroundingPair", delimiter: "any" }, surroundingPairThreshold],
[
{ type: "surroundingPair", delimiter: "curlyBrackets" },
surroundingPairThreshold,
],
];
for (const [scope, threshold] of fixtures) {
const [scopeType, title] = getScopeTypeAndTitle(scope);
test(
`Select ${title}`,
asyncSafety(() => selectScopeType(scopeType, threshold)),
);
}
});
async function removeToken(threshold: number) {
await testPerformance(threshold, {
name: "remove",
target: {
type: "primitive",
modifiers: [{ type: "containingScope", scopeType: { type: "token" } }],
},
});
}
async function selectScopeType(scopeType: ScopeType, threshold: number) {
await testPerformance(threshold, {
name: "setSelection",
target: {
type: "primitive",
modifiers: [{ type: "containingScope", scopeType }],
},
});
}
async function testPerformance(threshold: number, action: ActionDescriptor) {
const editor = await openNewEditor(testData, { languageId: "json" });
const position = new vscode.Position(editor.document.lineCount - 3, 5);
const selection = new vscode.Selection(position, position);
editor.selections = [selection];
editor.revealRange(selection);
const start = performance.now();
await runCursorlessCommand({
version: 7,
usePrePhraseSnapshot: false,
action,
});
const duration = Math.round(performance.now() - start);
console.log(` ${duration} ms`);
assert.ok(
duration < threshold,
`Duration ${duration}ms exceeds threshold ${threshold}ms`,
);
}
function getScopeTypeAndTitle(
scope: SimpleScopeTypeType | ScopeType,
): [ScopeType, string] {
if (typeof scope === "string") {
return [{ type: scope }, scope];
}
switch (scope.type) {
case "surroundingPair":
return [scope, `${scope.type}.${scope.delimiter}`];
}
throw Error(`Unexpected scope type: ${scope.type}`);
}
function generateTestData(): string {
const value = Object.fromEntries(
new Array(100).fill("").map((_, i) => [i.toString(), "value"]),
);
const obj = Object.fromEntries(
new Array(100).fill("").map((_, i) => [i.toString(), value]),
);
return JSON.stringify(obj, null, 2);
}