|
| 1 | +import { describe, bench } from "vitest" |
| 2 | +import { LezerBslParser } from "../src/parser/LezerBslParser" |
| 3 | +import * as fs from "node:fs" |
| 4 | +import * as path from "node:path" |
| 5 | +import { fileURLToPath } from "node:url" |
| 6 | + |
| 7 | +const benchmarksDir = path.dirname(fileURLToPath(import.meta.url)) |
| 8 | +const modulesDir = path.join(benchmarksDir, "modules") |
| 9 | + |
| 10 | +/** |
| 11 | + * Находит ближайшую строку в середине кода |
| 12 | + */ |
| 13 | +function findMiddleLine(code: string): number { |
| 14 | + const lines = code.split('\n') |
| 15 | + const middleLineIndex = Math.floor(lines.length / 2) |
| 16 | + let position = 0 |
| 17 | + for (let i = 0; i < middleLineIndex; i++) { |
| 18 | + position += lines[i].length + 1 // +1 для символа новой строки |
| 19 | + } |
| 20 | + return position |
| 21 | +} |
| 22 | + |
| 23 | +describe("Бенчмарки производительности парсера", () => { |
| 24 | + // Читаем все модули из каталога |
| 25 | + const moduleFiles = fs.readdirSync(modulesDir).filter(f => f.endsWith('.bsl')) |
| 26 | + |
| 27 | + if (moduleFiles.length === 0) { |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + for (const moduleFile of moduleFiles) { |
| 32 | + const modulePath = path.join(modulesDir, moduleFile) |
| 33 | + const moduleName = path.basename(moduleFile, '.bsl') |
| 34 | + const code = fs.readFileSync(modulePath, 'utf8') |
| 35 | + const codeSize = code.length |
| 36 | + |
| 37 | + describe(`Модуль: ${moduleName} (${codeSize} символов)`, () => { |
| 38 | + // Полный парсинг |
| 39 | + bench( |
| 40 | + `Полный парсинг: ${moduleName}`, |
| 41 | + () => { |
| 42 | + const parser = new LezerBslParser() |
| 43 | + parser.parse(code) |
| 44 | + }, |
| 45 | + { time: 500, iterations: 10 } |
| 46 | + ) |
| 47 | + |
| 48 | + // Инкрементальное обновление - вставка |
| 49 | + { |
| 50 | + let parserForInsert: LezerBslParser |
| 51 | + |
| 52 | + bench( |
| 53 | + `Инкрементальное обновление - вставка: ${moduleName}`, |
| 54 | + () => { |
| 55 | + |
| 56 | + // Измеряется только операция update |
| 57 | + const insertPosition = findMiddleLine(code) |
| 58 | + const codeToInsert = "\n // Вставленный комментарий для теста\n Перем Тест = 1;" |
| 59 | + const codeAfterInsert = code.slice(0, insertPosition) + codeToInsert + code.slice(insertPosition) |
| 60 | + |
| 61 | + parserForInsert.update(codeAfterInsert, [{ |
| 62 | + rangeOffset: insertPosition, |
| 63 | + rangeLength: 0, |
| 64 | + text: codeToInsert |
| 65 | + }]) |
| 66 | + }, |
| 67 | + { |
| 68 | + time: 500, |
| 69 | + iterations: 10, |
| 70 | + setup: () => { |
| 71 | + parserForInsert = new LezerBslParser() |
| 72 | + parserForInsert.parse(code) |
| 73 | + } |
| 74 | + } |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + // Инкрементальное обновление - удаление |
| 79 | + { |
| 80 | + let parserForDelete: LezerBslParser |
| 81 | + const insertPosition = findMiddleLine(code) |
| 82 | + const codeToInsert = "\n // Код для удаления\n Перем Временная = 999;" |
| 83 | + const codeWithExtra = code.slice(0, insertPosition) + codeToInsert + code.slice(insertPosition) |
| 84 | + |
| 85 | + bench( |
| 86 | + `Инкрементальное обновление - удаление: ${moduleName}`, |
| 87 | + () => { |
| 88 | + |
| 89 | + // Измеряется только операция update |
| 90 | + parserForDelete.update(code, [{ |
| 91 | + rangeOffset: insertPosition, |
| 92 | + rangeLength: codeToInsert.length, |
| 93 | + text: "" |
| 94 | + }]) |
| 95 | + }, |
| 96 | + { |
| 97 | + time: 500, |
| 98 | + iterations: 10, |
| 99 | + setup: () => { |
| 100 | + parserForDelete = new LezerBslParser() |
| 101 | + parserForDelete.parse(codeWithExtra) |
| 102 | + } |
| 103 | + } |
| 104 | + ) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +}) |
0 commit comments