|
| 1 | +import * as path from 'path'; |
| 2 | +import { performance } from 'perf_hooks'; |
| 3 | +import ts from 'typescript'; |
| 4 | +import { Position, Range } from 'vscode-languageserver'; |
| 5 | +import { Document, DocumentManager } from '../../../src/lib/documents'; |
| 6 | +import { LSConfigManager } from '../../../src/ls-config'; |
| 7 | +import { TypeScriptPlugin } from '../../../src/plugins'; |
| 8 | +import { pathToUrl } from '../../../src/utils'; |
| 9 | + |
| 10 | +describe('TypeScript Plugin Performance Tests', () => { |
| 11 | + function setup(filename: string) { |
| 12 | + const docManager = new DocumentManager(() => document); |
| 13 | + const testDir = path.join(__dirname, 'testfiles'); |
| 14 | + const filePath = path.join(testDir, filename); |
| 15 | + const uri = pathToUrl(filePath); |
| 16 | + const document = new Document(uri, ts.sys.readFile(filePath) || ''); |
| 17 | + const pluginManager = new LSConfigManager(); |
| 18 | + const plugin = new TypeScriptPlugin(docManager, pluginManager, [pathToUrl(testDir)]); |
| 19 | + docManager.openDocument({ uri, text: document.getText() }); |
| 20 | + const updateDocument = (newText: string) => |
| 21 | + docManager.updateDocument({ uri, version: 1 }, [ |
| 22 | + { range: Range.create(Position.create(9, 0), Position.create(9, 0)), text: newText } |
| 23 | + ]); |
| 24 | + return { plugin, document, updateDocument }; |
| 25 | + } |
| 26 | + |
| 27 | + it('should be fast enough', async () => { |
| 28 | + const { document, plugin, updateDocument } = setup('performance'); |
| 29 | + |
| 30 | + const start = performance.now(); |
| 31 | + for (let i = 0; i < 1000; i++) { |
| 32 | + await plugin.doHover(document, Position.create(1, 15)); |
| 33 | + await plugin.getDiagnostics(document); |
| 34 | + await plugin.findReferences(document, Position.create(1, 15), { |
| 35 | + includeDeclaration: true |
| 36 | + }); |
| 37 | + await plugin.getDocumentSymbols(document); |
| 38 | + await plugin.getSemanticTokens(document); |
| 39 | + await plugin.prepareRename(document, Position.create(1, 15)); |
| 40 | + updateDocument('function asd() {}\n'); |
| 41 | + } |
| 42 | + const end = performance.now(); |
| 43 | + |
| 44 | + console.log(`Performance test took ${end - start}ms`); |
| 45 | + }).timeout(10000); |
| 46 | +}); |
0 commit comments