2022-10-12 06:17:24 -04:00
|
|
|
import { Transaction } from '@codemirror/state'
|
2022-10-20 09:11:07 -04:00
|
|
|
import { EditorView } from '@codemirror/view'
|
2022-10-28 06:10:38 -04:00
|
|
|
import { round } from 'lodash'
|
|
|
|
import grammarlyExtensionPresent from '../shared/utils/grammarly'
|
2022-11-29 05:09:36 -05:00
|
|
|
import getMeta from '../utils/meta'
|
2022-10-12 06:17:24 -04:00
|
|
|
|
|
|
|
const TIMER_START_NAME = 'CM6-BeforeUpdate'
|
|
|
|
const TIMER_END_NAME = 'CM6-AfterUpdate'
|
2022-11-01 06:37:00 -04:00
|
|
|
const TIMER_DOM_UPDATE_NAME = 'CM6-DomUpdate'
|
2022-10-12 06:17:24 -04:00
|
|
|
const TIMER_MEASURE_NAME = 'CM6-Update'
|
2022-11-10 07:12:12 -05:00
|
|
|
const TIMER_KEYPRESS_MEASURE_NAME = 'CM6-Keypress-Measure'
|
2022-10-12 06:17:24 -04:00
|
|
|
|
|
|
|
let latestDocLength = 0
|
2022-10-28 06:10:38 -04:00
|
|
|
const sessionStart = Date.now()
|
2022-10-12 06:17:24 -04:00
|
|
|
|
2022-11-01 06:37:00 -04:00
|
|
|
let performanceOptionsSupport = false
|
2022-10-18 04:53:18 -04:00
|
|
|
|
2022-11-01 06:37:00 -04:00
|
|
|
// Check that performance.mark and performance.measure accept an options object
|
2022-10-18 04:53:18 -04:00
|
|
|
try {
|
2022-11-01 06:37:00 -04:00
|
|
|
const testMarkName = 'featureTestMark'
|
|
|
|
performance.mark(testMarkName, {
|
|
|
|
startTime: performance.now(),
|
|
|
|
detail: { test: 1 },
|
|
|
|
})
|
|
|
|
performance.clearMarks(testMarkName)
|
|
|
|
|
|
|
|
const testMeasureName = 'featureTestMeasure'
|
|
|
|
performance.measure(testMeasureName, {
|
|
|
|
start: performance.now(),
|
|
|
|
detail: { test: 1 },
|
|
|
|
})
|
2022-10-18 04:53:18 -04:00
|
|
|
performance.clearMeasures(testMeasureName)
|
2022-11-01 06:37:00 -04:00
|
|
|
|
|
|
|
performanceOptionsSupport = true
|
2022-10-18 04:53:18 -04:00
|
|
|
} catch (e) {}
|
|
|
|
|
2022-10-28 06:10:38 -04:00
|
|
|
let performanceMemorySupport = false
|
|
|
|
|
|
|
|
function measureMemoryUsage() {
|
|
|
|
// @ts-ignore
|
|
|
|
return performance.memory.usedJSHeapSize
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ('memory' in window.performance) {
|
|
|
|
measureMemoryUsage()
|
|
|
|
performanceMemorySupport = true
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
|
2022-11-29 05:09:36 -05:00
|
|
|
let performanceLongtaskSupported = false
|
|
|
|
let longTaskSinceLastReportCount = 0
|
|
|
|
|
|
|
|
// Detect support for long task monitoring
|
|
|
|
try {
|
|
|
|
if (PerformanceObserver.supportedEntryTypes.includes('longtask')) {
|
|
|
|
performanceLongtaskSupported = true
|
|
|
|
|
|
|
|
// Register observer for long task notifications
|
|
|
|
const observer = new PerformanceObserver(list => {
|
|
|
|
longTaskSinceLastReportCount += list.getEntries().length
|
|
|
|
})
|
|
|
|
observer.observe({ entryTypes: ['longtask'] })
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
|
2022-10-20 09:11:07 -04:00
|
|
|
function isInputOrDelete(userEventType: string | undefined) {
|
|
|
|
return (
|
|
|
|
!!userEventType && ['input', 'delete'].includes(userEventType.split('.')[0])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-01 06:37:00 -04:00
|
|
|
// "keypress" is not strictly accurate; what we really mean is a user-initiated
|
|
|
|
// event that either inserts or deletes exactly one character. This corresponds
|
|
|
|
// to CM6 user event types input.type, delete.forward or delete.backward
|
|
|
|
function isKeypress(userEventType: string | undefined) {
|
|
|
|
return (
|
|
|
|
!!userEventType &&
|
|
|
|
['input.type', 'delete.forward', 'delete.backward'].includes(userEventType)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-15 08:26:41 -05:00
|
|
|
export function dispatchTimer(): {
|
2023-09-14 04:20:23 -04:00
|
|
|
start: (trs: readonly Transaction[]) => void
|
|
|
|
end: (trs: readonly Transaction[], view: EditorView) => void
|
2022-11-15 08:26:41 -05:00
|
|
|
} {
|
|
|
|
if (!performanceOptionsSupport) {
|
|
|
|
return { start: () => {}, end: () => {} }
|
|
|
|
}
|
|
|
|
|
2022-10-20 09:11:07 -04:00
|
|
|
let userEventsSinceDomUpdateCount = 0
|
2022-11-01 06:37:00 -04:00
|
|
|
let keypressesSinceDomUpdateCount = 0
|
2022-11-10 07:12:12 -05:00
|
|
|
const unpaintedKeypressStartTimes: number[] = []
|
2022-10-20 09:11:07 -04:00
|
|
|
|
2023-09-14 04:20:23 -04:00
|
|
|
const start = (trs: readonly Transaction[]) => {
|
|
|
|
const keypressStart = performance.now()
|
2022-11-10 07:12:12 -05:00
|
|
|
|
2023-09-14 04:20:23 -04:00
|
|
|
trs.forEach(tr => {
|
|
|
|
const userEventType = tr.annotation(Transaction.userEvent)
|
|
|
|
|
|
|
|
if (isKeypress(userEventType)) {
|
|
|
|
unpaintedKeypressStartTimes.push(keypressStart)
|
|
|
|
}
|
|
|
|
})
|
2022-11-10 07:12:12 -05:00
|
|
|
|
2022-11-15 08:26:41 -05:00
|
|
|
performance.mark(TIMER_START_NAME)
|
|
|
|
}
|
2022-10-12 06:17:24 -04:00
|
|
|
|
2023-09-14 04:20:23 -04:00
|
|
|
const end = (trs: readonly Transaction[], view: EditorView) => {
|
2022-10-12 06:17:24 -04:00
|
|
|
performance.mark(TIMER_END_NAME)
|
|
|
|
|
2023-09-14 04:20:23 -04:00
|
|
|
let anyInputOrDelete = false
|
2022-11-15 08:26:41 -05:00
|
|
|
|
2023-09-14 04:20:23 -04:00
|
|
|
trs.forEach(tr => {
|
|
|
|
const userEventType = tr.annotation(Transaction.userEvent)
|
2022-10-20 09:11:07 -04:00
|
|
|
|
2023-09-14 04:20:23 -04:00
|
|
|
if (isInputOrDelete(userEventType)) {
|
|
|
|
anyInputOrDelete = true
|
|
|
|
++userEventsSinceDomUpdateCount
|
2022-11-01 06:37:00 -04:00
|
|
|
|
2023-09-14 04:20:23 -04:00
|
|
|
if (isKeypress(userEventType)) {
|
|
|
|
++keypressesSinceDomUpdateCount
|
|
|
|
}
|
|
|
|
|
|
|
|
performance.measure(TIMER_MEASURE_NAME, {
|
|
|
|
start: TIMER_START_NAME,
|
|
|
|
end: TIMER_END_NAME,
|
|
|
|
detail: { userEventType, userEventsSinceDomUpdateCount },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-10-20 09:11:07 -04:00
|
|
|
|
2023-09-14 04:20:23 -04:00
|
|
|
if (anyInputOrDelete) {
|
2022-10-20 09:11:07 -04:00
|
|
|
// The `key` property ensures that the measurement task is only run once
|
|
|
|
// per measure phase
|
|
|
|
view.requestMeasure({
|
|
|
|
key: 'inputEventCounter',
|
|
|
|
read() {
|
2022-11-01 06:37:00 -04:00
|
|
|
performance.mark(TIMER_DOM_UPDATE_NAME, {
|
|
|
|
detail: { keypressesSinceDomUpdateCount },
|
|
|
|
})
|
2022-10-20 09:11:07 -04:00
|
|
|
userEventsSinceDomUpdateCount = 0
|
2022-11-01 06:37:00 -04:00
|
|
|
keypressesSinceDomUpdateCount = 0
|
2022-11-10 07:12:12 -05:00
|
|
|
|
|
|
|
const keypressEnd = performance.now()
|
|
|
|
|
|
|
|
for (const keypressStart of unpaintedKeypressStartTimes) {
|
|
|
|
performance.measure(TIMER_KEYPRESS_MEASURE_NAME, {
|
|
|
|
start: keypressStart,
|
|
|
|
end: keypressEnd,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
unpaintedKeypressStartTimes.length = 0
|
2022-10-20 09:11:07 -04:00
|
|
|
},
|
2022-10-18 04:53:18 -04:00
|
|
|
})
|
2022-10-12 06:17:24 -04:00
|
|
|
}
|
|
|
|
|
2023-09-14 04:20:23 -04:00
|
|
|
latestDocLength = trs[trs.length - 1].state.doc.length
|
2022-10-12 06:17:24 -04:00
|
|
|
}
|
2022-11-15 08:26:41 -05:00
|
|
|
|
|
|
|
return { start, end }
|
2022-10-12 06:17:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function calculateMean(durations: number[]) {
|
|
|
|
if (durations.length === 0) return 0
|
|
|
|
|
|
|
|
const sum = durations.reduce((acc, entry) => acc + entry, 0)
|
|
|
|
return sum / durations.length
|
|
|
|
}
|
|
|
|
|
|
|
|
function calculateMedian(sortedDurations: number[]) {
|
|
|
|
if (sortedDurations.length === 0) return 0
|
|
|
|
|
|
|
|
const middle = Math.floor(sortedDurations.length / 2)
|
|
|
|
|
|
|
|
if (sortedDurations.length % 2 === 0) {
|
|
|
|
return (sortedDurations[middle - 1] + sortedDurations[middle]) / 2
|
|
|
|
}
|
|
|
|
return sortedDurations[middle]
|
|
|
|
}
|
|
|
|
|
|
|
|
function calculate95thPercentile(sortedDurations: number[]) {
|
|
|
|
if (sortedDurations.length === 0) return 0
|
|
|
|
|
|
|
|
const index = Math.round((sortedDurations.length - 1) * 0.95)
|
|
|
|
return sortedDurations[index]
|
|
|
|
}
|
|
|
|
|
2022-10-20 09:11:07 -04:00
|
|
|
function calculateMax(numbers: number[]) {
|
|
|
|
return numbers.reduce((a, b) => Math.max(a, b), 0)
|
|
|
|
}
|
|
|
|
|
2023-01-25 05:13:45 -05:00
|
|
|
function clearCM6Perf(type: string) {
|
|
|
|
switch (type) {
|
|
|
|
case 'measure':
|
|
|
|
performance.clearMeasures(TIMER_MEASURE_NAME)
|
|
|
|
performance.clearMarks(TIMER_START_NAME)
|
|
|
|
performance.clearMarks(TIMER_END_NAME)
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'dom':
|
|
|
|
performance.clearMarks(TIMER_DOM_UPDATE_NAME)
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'keypress':
|
|
|
|
performance.clearMeasures(TIMER_KEYPRESS_MEASURE_NAME)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear performance measures and marks when switching between Source and Rich Text
|
|
|
|
window.addEventListener('editor:visual-switch', () => {
|
|
|
|
clearCM6Perf('measure')
|
|
|
|
clearCM6Perf('dom')
|
|
|
|
clearCM6Perf('keypress')
|
|
|
|
})
|
|
|
|
|
2022-10-12 06:17:24 -04:00
|
|
|
export function reportCM6Perf() {
|
|
|
|
// Get entries triggered by keystrokes
|
|
|
|
const cm6Entries = performance.getEntriesByName(
|
|
|
|
TIMER_MEASURE_NAME,
|
|
|
|
'measure'
|
|
|
|
) as PerformanceMeasure[]
|
|
|
|
|
2023-01-25 05:13:45 -05:00
|
|
|
clearCM6Perf('measure')
|
2022-11-01 06:37:00 -04:00
|
|
|
|
2022-10-20 09:11:07 -04:00
|
|
|
const inputEvents = cm6Entries.filter(({ detail }) =>
|
|
|
|
isInputOrDelete(detail.userEventType)
|
|
|
|
)
|
|
|
|
|
|
|
|
const inputDurations = inputEvents
|
2022-10-12 06:17:24 -04:00
|
|
|
.map(({ duration }) => duration)
|
|
|
|
.sort((a, b) => a - b)
|
|
|
|
|
2022-10-28 06:10:38 -04:00
|
|
|
const max = round(calculateMax(inputDurations), 2)
|
|
|
|
const mean = round(calculateMean(inputDurations), 2)
|
|
|
|
const median = round(calculateMedian(inputDurations), 2)
|
|
|
|
const ninetyFifthPercentile = round(
|
|
|
|
calculate95thPercentile(inputDurations),
|
|
|
|
2
|
|
|
|
)
|
2022-10-20 09:11:07 -04:00
|
|
|
const maxUserEventsBetweenDomUpdates = calculateMax(
|
|
|
|
inputEvents.map(e => e.detail.userEventsSinceDomUpdateCount)
|
|
|
|
)
|
2022-10-28 06:10:38 -04:00
|
|
|
const grammarly = grammarlyExtensionPresent()
|
|
|
|
const sessionLength = Math.floor((Date.now() - sessionStart) / 1000) // In seconds
|
2022-10-12 06:17:24 -04:00
|
|
|
|
2022-10-28 06:10:38 -04:00
|
|
|
const memory = performanceMemorySupport ? measureMemoryUsage() : null
|
|
|
|
|
2022-11-01 06:37:00 -04:00
|
|
|
// Get entries for keypress counts between DOM updates
|
|
|
|
const domUpdateEntries = performance.getEntriesByName(
|
|
|
|
TIMER_DOM_UPDATE_NAME,
|
|
|
|
'mark'
|
2022-11-10 07:12:12 -05:00
|
|
|
) as PerformanceMark[]
|
2022-11-01 06:37:00 -04:00
|
|
|
|
2023-01-25 05:13:45 -05:00
|
|
|
clearCM6Perf('dom')
|
2022-11-01 06:37:00 -04:00
|
|
|
|
|
|
|
let lags = 0
|
|
|
|
let nonLags = 0
|
|
|
|
let longestLag = 0
|
|
|
|
let totalKeypressCount = 0
|
|
|
|
|
|
|
|
for (const entry of domUpdateEntries) {
|
|
|
|
const keypressCount = entry.detail.keypressesSinceDomUpdateCount
|
|
|
|
if (keypressCount === 1) {
|
|
|
|
++nonLags
|
|
|
|
} else if (keypressCount > 1) {
|
|
|
|
++lags
|
|
|
|
}
|
|
|
|
if (keypressCount > longestLag) {
|
|
|
|
longestLag = keypressCount
|
|
|
|
}
|
|
|
|
totalKeypressCount += keypressCount
|
|
|
|
}
|
|
|
|
|
|
|
|
const meanLagsPerMeasure = round(lags / (lags + nonLags), 4)
|
|
|
|
const meanKeypressesPerMeasure = round(
|
|
|
|
totalKeypressCount / (lags + nonLags),
|
|
|
|
4
|
|
|
|
)
|
|
|
|
|
2022-11-10 07:12:12 -05:00
|
|
|
// Get entries triggered by keystrokes
|
|
|
|
const keypressPaintEntries = performance.getEntriesByName(
|
|
|
|
TIMER_KEYPRESS_MEASURE_NAME,
|
|
|
|
'measure'
|
|
|
|
) as PerformanceMeasure[]
|
|
|
|
|
|
|
|
const keypressPaintDurations = keypressPaintEntries.map(
|
|
|
|
({ duration }) => duration
|
|
|
|
)
|
|
|
|
|
|
|
|
const meanKeypressPaint = round(calculateMean(keypressPaintDurations), 2)
|
|
|
|
|
2023-01-25 05:13:45 -05:00
|
|
|
clearCM6Perf('keypress')
|
2022-11-10 07:12:12 -05:00
|
|
|
|
2022-11-29 05:09:36 -05:00
|
|
|
let longTasks = null
|
|
|
|
|
|
|
|
// Get long task entries (Chromium-based browsers only at time of writing)
|
|
|
|
if (performanceLongtaskSupported) {
|
|
|
|
longTasks = longTaskSinceLastReportCount
|
|
|
|
longTaskSinceLastReportCount = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
const release = getMeta('ol-ExposedSettings')?.sentryRelease || null
|
|
|
|
|
2022-10-12 06:17:24 -04:00
|
|
|
return {
|
|
|
|
max,
|
|
|
|
mean,
|
|
|
|
median,
|
|
|
|
ninetyFifthPercentile,
|
2022-10-20 09:11:07 -04:00
|
|
|
maxUserEventsBetweenDomUpdates,
|
2022-10-12 06:17:24 -04:00
|
|
|
docLength: latestDocLength,
|
|
|
|
numberOfEntries: inputDurations.length,
|
2022-10-28 06:10:38 -04:00
|
|
|
grammarly,
|
|
|
|
sessionLength,
|
|
|
|
memory,
|
2022-11-01 06:37:00 -04:00
|
|
|
lags,
|
|
|
|
nonLags,
|
|
|
|
longestLag,
|
|
|
|
meanLagsPerMeasure,
|
|
|
|
meanKeypressesPerMeasure,
|
2022-11-10 07:12:12 -05:00
|
|
|
meanKeypressPaint,
|
2022-11-29 05:09:36 -05:00
|
|
|
longTasks,
|
|
|
|
release,
|
2022-10-12 06:17:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window._reportCM6Perf = () => {
|
|
|
|
console.log(reportCM6Perf())
|
|
|
|
}
|