cm6-performance.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Transaction } from '@codemirror/state'
  2. const TIMER_START_NAME = 'CM6-BeforeUpdate'
  3. const TIMER_END_NAME = 'CM6-AfterUpdate'
  4. const TIMER_MEASURE_NAME = 'CM6-Update'
  5. let latestDocLength = 0
  6. let performanceMeasureOptionsSupport = false
  7. // Check that performance.measure accepts an options object
  8. try {
  9. const testMeasureName = 'featureTest'
  10. performance.measure(testMeasureName, { start: performance.now() })
  11. performance.clearMeasures(testMeasureName)
  12. performanceMeasureOptionsSupport = true
  13. } catch (e) {}
  14. export function timedDispatch(dispatchFn: (tr: Transaction) => void) {
  15. return (tr: Transaction) => {
  16. if (!performanceMeasureOptionsSupport) {
  17. dispatchFn(tr)
  18. return
  19. }
  20. performance.mark(TIMER_START_NAME)
  21. dispatchFn(tr)
  22. performance.mark(TIMER_END_NAME)
  23. const userEventType = tr.annotation(Transaction.userEvent)
  24. if (userEventType) {
  25. performance.measure(TIMER_MEASURE_NAME, {
  26. start: TIMER_START_NAME,
  27. end: TIMER_END_NAME,
  28. detail: { userEventType },
  29. })
  30. }
  31. latestDocLength = tr.state.doc.length
  32. }
  33. }
  34. function calculateMean(durations: number[]) {
  35. if (durations.length === 0) return 0
  36. const sum = durations.reduce((acc, entry) => acc + entry, 0)
  37. return sum / durations.length
  38. }
  39. function calculateMedian(sortedDurations: number[]) {
  40. if (sortedDurations.length === 0) return 0
  41. const middle = Math.floor(sortedDurations.length / 2)
  42. if (sortedDurations.length % 2 === 0) {
  43. return (sortedDurations[middle - 1] + sortedDurations[middle]) / 2
  44. }
  45. return sortedDurations[middle]
  46. }
  47. function calculate95thPercentile(sortedDurations: number[]) {
  48. if (sortedDurations.length === 0) return 0
  49. const index = Math.round((sortedDurations.length - 1) * 0.95)
  50. return sortedDurations[index]
  51. }
  52. export function reportCM6Perf() {
  53. // Get entries triggered by keystrokes
  54. const cm6Entries = performance.getEntriesByName(
  55. TIMER_MEASURE_NAME,
  56. 'measure'
  57. ) as PerformanceMeasure[]
  58. const inputDurations = cm6Entries
  59. .filter(({ detail }) =>
  60. ['input', 'delete'].includes(detail.userEventType.split('.')[0])
  61. )
  62. .map(({ duration }) => duration)
  63. .sort((a, b) => a - b)
  64. const max = inputDurations.reduce((a, b) => Math.max(a, b), 0)
  65. const mean = calculateMean(inputDurations)
  66. const median = calculateMedian(inputDurations)
  67. const ninetyFifthPercentile = calculate95thPercentile(inputDurations)
  68. performance.clearMeasures(TIMER_MEASURE_NAME)
  69. return {
  70. max,
  71. mean,
  72. median,
  73. ninetyFifthPercentile,
  74. docLength: latestDocLength,
  75. numberOfEntries: inputDurations.length,
  76. }
  77. }
  78. window._reportCM6Perf = () => {
  79. console.log(reportCM6Perf())
  80. }