ace-performance.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { round } from 'lodash'
  2. import grammarlyExtensionPresent from '../../shared/utils/grammarly'
  3. import getMeta from '../../utils/meta'
  4. const TIMER_DOM_UPDATE_NAME = 'Ace-DomUpdate'
  5. const TIMER_MEASURE_NAME = 'Ace-Keypress-Measure'
  6. const sessionStart = Date.now()
  7. let performanceOptionsSupport = false
  8. // Check that performance.mark and performance.measure accept an options object
  9. try {
  10. const testMarkName = 'featureTestMark'
  11. performance.mark(testMarkName, {
  12. startTime: performance.now(),
  13. detail: { test: 1 },
  14. })
  15. performance.clearMarks(testMarkName)
  16. const testMeasureName = 'featureTestMeasure'
  17. performance.measure(testMeasureName, {
  18. start: performance.now(),
  19. detail: { test: 1 },
  20. })
  21. performance.clearMeasures(testMeasureName)
  22. performanceOptionsSupport = true
  23. } catch (e) {}
  24. let performanceMemorySupport = false
  25. function measureMemoryUsage() {
  26. // @ts-ignore
  27. return performance.memory.usedJSHeapSize
  28. }
  29. try {
  30. if ('memory' in window.performance) {
  31. measureMemoryUsage()
  32. performanceMemorySupport = true
  33. }
  34. } catch (e) {}
  35. let keypressesSinceDomUpdateCount = 0
  36. const unpaintedKeypressStartTimes: number[] = []
  37. let animationFrameRequest: number | null = null
  38. function timeInputToRender() {
  39. if (!performanceOptionsSupport) return
  40. ++keypressesSinceDomUpdateCount
  41. unpaintedKeypressStartTimes.push(performance.now())
  42. if (!animationFrameRequest) {
  43. animationFrameRequest = window.requestAnimationFrame(() => {
  44. animationFrameRequest = null
  45. performance.mark(TIMER_DOM_UPDATE_NAME, {
  46. detail: { keypressesSinceDomUpdateCount },
  47. })
  48. keypressesSinceDomUpdateCount = 0
  49. const keypressEnd = performance.now()
  50. for (const keypressStart of unpaintedKeypressStartTimes) {
  51. performance.measure(TIMER_MEASURE_NAME, {
  52. start: keypressStart,
  53. end: keypressEnd,
  54. })
  55. }
  56. unpaintedKeypressStartTimes.length = 0
  57. })
  58. }
  59. }
  60. export function initAcePerfListener(textareaEl: HTMLTextAreaElement) {
  61. textareaEl?.addEventListener('beforeinput', timeInputToRender)
  62. }
  63. export function tearDownAcePerfListener(textareaEl: HTMLTextAreaElement) {
  64. textareaEl?.removeEventListener('beforeinput', timeInputToRender)
  65. }
  66. function calculateMean(durations: number[]) {
  67. if (durations.length === 0) return 0
  68. const sum = durations.reduce((acc, entry) => acc + entry, 0)
  69. return sum / durations.length
  70. }
  71. export function reportAcePerf() {
  72. const durations = performance
  73. .getEntriesByName(TIMER_MEASURE_NAME, 'measure')
  74. .map(({ duration }) => duration)
  75. performance.clearMeasures(TIMER_MEASURE_NAME)
  76. const meanKeypressPaint = round(calculateMean(durations), 2)
  77. const grammarly = grammarlyExtensionPresent()
  78. const sessionLength = Math.floor((Date.now() - sessionStart) / 1000) // In seconds
  79. const memory = performanceMemorySupport ? measureMemoryUsage() : null
  80. // Get entries for keypress counts between DOM updates
  81. const domUpdateEntries = performance.getEntriesByName(
  82. TIMER_DOM_UPDATE_NAME,
  83. 'mark'
  84. ) as PerformanceMark[]
  85. performance.clearMarks(TIMER_DOM_UPDATE_NAME)
  86. let lags = 0
  87. let nonLags = 0
  88. let longestLag = 0
  89. let totalKeypressCount = 0
  90. for (const entry of domUpdateEntries) {
  91. const keypressCount = entry.detail.keypressesSinceDomUpdateCount
  92. if (keypressCount === 1) {
  93. ++nonLags
  94. } else if (keypressCount > 1) {
  95. ++lags
  96. }
  97. if (keypressCount > longestLag) {
  98. longestLag = keypressCount
  99. }
  100. totalKeypressCount += keypressCount
  101. }
  102. const meanLagsPerMeasure = round(lags / (lags + nonLags), 4)
  103. const meanKeypressesPerMeasure = round(
  104. totalKeypressCount / (lags + nonLags),
  105. 4
  106. )
  107. const release = getMeta('ol-ExposedSettings')?.sentryRelease || null
  108. return {
  109. numberOfEntries: durations.length,
  110. meanKeypressPaint,
  111. grammarly,
  112. sessionLength,
  113. memory,
  114. lags,
  115. nonLags,
  116. longestLag,
  117. meanLagsPerMeasure,
  118. meanKeypressesPerMeasure,
  119. release,
  120. }
  121. }
  122. window._reportAcePerf = () => {
  123. console.log(reportAcePerf())
  124. }