test-incremental-parser.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { parser } from '../../frontend/js/features/source-editor/lezer-latex/latex.mjs'
  2. import * as fs from 'fs'
  3. import * as path from 'path'
  4. import { fileURLToPath } from 'url'
  5. import { TreeFragment } from '@lezer/common'
  6. import minimist from 'minimist'
  7. const argv = minimist(process.argv.slice(2))
  8. const NUMBER_OF_OPS = argv.ops || 1000
  9. const CSV_OUTPUT = argv.csv || false
  10. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  11. const examplesDir = path.join(
  12. __dirname,
  13. '../../test/unit/src/LezerLatex/examples'
  14. )
  15. const folder = examplesDir
  16. for (const file of fs.readdirSync(folder).sort()) {
  17. if (!/\.tex$/.test(file)) continue
  18. const name = /^[^.]*/.exec(file)[0]
  19. const content = fs.readFileSync(path.join(folder, file), 'utf8')
  20. runPerformanceTests(name, content)
  21. }
  22. function runPerformanceTests(name, content) {
  23. const insertEnd = writeTextAt(
  24. content,
  25. content.length,
  26. content.substring(0, NUMBER_OF_OPS)
  27. )
  28. const insertBeginning = writeTextAt(
  29. content,
  30. 0,
  31. content.substring(0, NUMBER_OF_OPS)
  32. )
  33. const insertMiddle = writeTextAt(
  34. content,
  35. Math.floor(content.length / 2),
  36. content.substring(0, NUMBER_OF_OPS)
  37. )
  38. const randomDelete = randomDeletions(content, NUMBER_OF_OPS)
  39. const middleDelete = deletionsFromMiddle(content, NUMBER_OF_OPS)
  40. const randomInsert = randomInsertions(content, NUMBER_OF_OPS)
  41. if (CSV_OUTPUT) {
  42. console.log(
  43. [
  44. name,
  45. insertBeginning.average,
  46. insertMiddle.average,
  47. insertEnd.average,
  48. randomInsert.average,
  49. randomDelete.average,
  50. middleDelete.average,
  51. content.length,
  52. ].join(',')
  53. )
  54. } else {
  55. console.log({
  56. name,
  57. insertAtEnd: insertEnd.average,
  58. insertAtBeginning: insertBeginning.average,
  59. insertAtMiddle: insertMiddle.average,
  60. randomDelete: randomDelete.average,
  61. middleDelete: middleDelete.average,
  62. randomInsert: randomInsert.average,
  63. docLength: content.length,
  64. })
  65. }
  66. }
  67. function timedChanges(document, changes, changeFn) {
  68. let totalParseTime = 0
  69. // Do a fresh parse to get TreeFragments
  70. const initialTree = parser.parse(document)
  71. let fragments = TreeFragment.addTree(initialTree)
  72. let currentDoc = document
  73. for (let i = 0; i < changes; ++i) {
  74. const change = changeFn(currentDoc, i)
  75. currentDoc = change.text
  76. // Do a timed parse
  77. const start = performance.now()
  78. fragments = TreeFragment.applyChanges(fragments, [change.range])
  79. const tree = parser.parse(currentDoc, fragments)
  80. fragments = TreeFragment.addTree(tree, fragments)
  81. const end = performance.now()
  82. totalParseTime += end - start
  83. }
  84. return {
  85. total: totalParseTime,
  86. average: totalParseTime / changes,
  87. ops: changes,
  88. fragments: fragments.length,
  89. }
  90. }
  91. // Write and parse after every character insertion
  92. function writeTextAt(document, position, text) {
  93. return timedChanges(document, text.length, (currentDoc, index) =>
  94. insertAt(currentDoc, position + index, text[index])
  95. )
  96. }
  97. function randomInsertions(document, num) {
  98. return timedChanges(document, num, currentDoc =>
  99. insertAt(currentDoc, Math.floor(Math.random() * currentDoc.length), 'a')
  100. )
  101. }
  102. function randomDeletions(document, num) {
  103. return timedChanges(document, num, currentDoc =>
  104. deleteAt(currentDoc, Math.floor(Math.random() * currentDoc.length), 1)
  105. )
  106. }
  107. function deletionsFromMiddle(document, num) {
  108. const deletionPoint = Math.floor(document.length / 2)
  109. const deletions = Math.min(num, deletionPoint - 1)
  110. return timedChanges(document, deletions, (currentDoc, index) =>
  111. deleteAt(currentDoc, deletionPoint - index, 1)
  112. )
  113. }
  114. function insertAt(document, position, text) {
  115. const start = document.substring(0, position)
  116. const end = document.substring(position)
  117. return {
  118. text: start + text + end,
  119. range: {
  120. fromA: position,
  121. toA: position,
  122. fromB: position,
  123. toB: position + text.length,
  124. },
  125. }
  126. }
  127. function deleteAt(document, position, length = 1) {
  128. const start = document.substring(0, position)
  129. const end = document.substring(position + length)
  130. return {
  131. text: start + end,
  132. range: {
  133. fromA: position,
  134. toA: position + length,
  135. fromB: position,
  136. toB: position,
  137. },
  138. }
  139. }