|
| 1 | +import { |
| 2 | + asyncSafety, |
| 3 | + type ActionDescriptor, |
| 4 | + type ScopeType, |
| 5 | + type SimpleScopeTypeType, |
| 6 | +} from "@cursorless/common"; |
| 7 | +import { openNewEditor, runCursorlessCommand } from "@cursorless/vscode-common"; |
| 8 | +import assert from "assert"; |
| 9 | +import * as vscode from "vscode"; |
| 10 | +import { endToEndTestSetup } from "../endToEndTestSetup"; |
| 11 | + |
| 12 | +const obj = Object.fromEntries( |
| 13 | + new Array(100) |
| 14 | + .fill("") |
| 15 | + .map((_, i) => [ |
| 16 | + i.toString(), |
| 17 | + Object.fromEntries( |
| 18 | + new Array(100).fill("").map((_, i) => [i.toString(), "value"]), |
| 19 | + ), |
| 20 | + ]), |
| 21 | +); |
| 22 | +const content = JSON.stringify(obj, null, 2); |
| 23 | +const numLines = content.split("\n").length; |
| 24 | + |
| 25 | +suite(`Performance: ${numLines} lines JSON`, async function () { |
| 26 | + endToEndTestSetup(this); |
| 27 | + |
| 28 | + this.beforeEach(function () { |
| 29 | + // console.log("before"); |
| 30 | + // console.log(a.name); |
| 31 | + console.log(this.test?.title); |
| 32 | + console.log(this.test?.id); |
| 33 | + }); |
| 34 | + |
| 35 | + this.afterAll(() => { |
| 36 | + console.log("Done!"); |
| 37 | + }); |
| 38 | + |
| 39 | + const scopeTypeTypes: Partial<Record<SimpleScopeTypeType, number>> = { |
| 40 | + character: 100, |
| 41 | + word: 100, |
| 42 | + token: 100, |
| 43 | + identifier: 100, |
| 44 | + line: 100, |
| 45 | + sentence: 100, |
| 46 | + paragraph: 100, |
| 47 | + // boundedParagraph: 100, |
| 48 | + document: 100, |
| 49 | + nonWhitespaceSequence: 100, |
| 50 | + // boundedNonWhitespaceSequence: 300, |
| 51 | + // string: 100, |
| 52 | + map: 100, |
| 53 | + // collectionKey: 300, |
| 54 | + // value: 15000, |
| 55 | + }; |
| 56 | + |
| 57 | + for (const [scopeTypeType, threshold] of Object.entries(scopeTypeTypes)) { |
| 58 | + test( |
| 59 | + `Select ${scopeTypeType}`, |
| 60 | + asyncSafety(() => |
| 61 | + selectScopeType( |
| 62 | + { type: scopeTypeType as SimpleScopeTypeType }, |
| 63 | + threshold, |
| 64 | + ), |
| 65 | + ), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + // test( |
| 70 | + // "Select any surrounding pair", |
| 71 | + // asyncSafety(() => |
| 72 | + // selectScopeType({ type: "surroundingPair", delimiter: "any" }, 300), |
| 73 | + // ), |
| 74 | + // ); |
| 75 | + |
| 76 | + test( |
| 77 | + "Remove token", |
| 78 | + asyncSafety(() => removeToken()), |
| 79 | + ); |
| 80 | +}); |
| 81 | + |
| 82 | +async function removeToken() { |
| 83 | + await testPerformance(100, { |
| 84 | + name: "remove", |
| 85 | + target: { |
| 86 | + type: "primitive", |
| 87 | + modifiers: [{ type: "containingScope", scopeType: { type: "token" } }], |
| 88 | + }, |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +async function selectScopeType(scopeType: ScopeType, threshold: number) { |
| 93 | + await testPerformance(threshold, { |
| 94 | + name: "setSelection", |
| 95 | + target: { |
| 96 | + type: "primitive", |
| 97 | + modifiers: [{ type: "containingScope", scopeType }], |
| 98 | + }, |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +async function testPerformance(threshold: number, action: ActionDescriptor) { |
| 103 | + const editor = await openNewEditor(content, { languageId: "json" }); |
| 104 | + const position = new vscode.Position(editor.document.lineCount - 3, 5); |
| 105 | + const selection = new vscode.Selection(position, position); |
| 106 | + editor.selections = [selection]; |
| 107 | + editor.revealRange(selection); |
| 108 | + |
| 109 | + const start = performance.now(); |
| 110 | + |
| 111 | + await runCursorlessCommand({ |
| 112 | + version: 7, |
| 113 | + usePrePhraseSnapshot: false, |
| 114 | + action, |
| 115 | + }); |
| 116 | + |
| 117 | + const duration = Math.round(performance.now() - start); |
| 118 | + |
| 119 | + console.debug(`\t${duration} ms`); |
| 120 | + |
| 121 | + assert.ok( |
| 122 | + duration < threshold, |
| 123 | + `Duration ${duration}ms exceeds threshold ${threshold}ms`, |
| 124 | + ); |
| 125 | +} |
0 commit comments