overleaf/services/web/scripts/lezer-latex/benchmark.mjs
Tim Down 7f37ba737c Move source editor out of module (#12457)
* Update Copybara options in preparation for open-sourcing the source editor

* Move files

* Update paths

* Remove source-editor module and checks for its existence

* Explicitly mention CM6 license in files that contain code adapted from CM6

GitOrigin-RevId: 89b7cc2b409db01ad103198ccbd1b126ab56349b
2023-04-13 08:40:56 +00:00

66 lines
1.7 KiB
JavaScript

import { parser } from '../../frontend/js/features/source-editor/lezer-latex/latex.mjs'
import * as fs from 'fs'
import * as path from 'path'
import { fileURLToPath } from 'url'
import minimist from 'minimist'
const argv = minimist(process.argv.slice(2))
const NUMBER_OF_OPS = argv.ops || 100
const CSV_OUTPUT = argv.csv || false
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const examplesDir = path.join(
__dirname,
'../../test/unit/src/lezer-latex/examples'
)
const strictParser = parser.configure({ strict: true }) // throw exception for invalid documents
if (!fs.existsSync(examplesDir)) {
console.error('No examples directory')
process.exit()
}
function dumpParserStats(parser) {
console.log('Parser size:')
console.dir({
states: parser.states.length,
data: parser.data.length,
goto: parser.goto.length,
})
}
dumpParserStats(strictParser)
const folder = examplesDir
for (const file of fs.readdirSync(folder).sort()) {
if (!/\.tex$/.test(file)) continue
const name = /^[^.]*/.exec(file)[0]
const content = fs.readFileSync(path.join(folder, file), 'utf8')
benchmark(name, content)
}
function benchmark(name, content) {
let timeSum = 0
try {
for (let i = 0; i < NUMBER_OF_OPS; ++i) {
const startTime = performance.now()
strictParser.parse(content)
const endTime = performance.now()
timeSum += endTime - startTime
}
const avgTime = timeSum / NUMBER_OF_OPS
if (CSV_OUTPUT) {
console.log(`${name},${avgTime.toFixed(2)},${content.length}`)
} else {
console.log(
`${name.padEnd(20)} time to run (ms):\t ${avgTime.toFixed(2)}`
)
}
} catch (error) {
console.error(`${name.padEnd(20)} ${error}`)
}
}