overleaf/services/web/frontend/js/ide/pdf/controllers/PdfJsMetrics.js
Jakob Ackermann 5988e03dac Merge pull request #4108 from overleaf/jpa-lean-pdf-caching-metrics
[frontend] PdfJsMetrics: submit a subset of data points, flatten nested

GitOrigin-RevId: a98ede58f5823020a4c78f46b9800369185f1336
2021-06-02 02:05:43 +00:00

83 lines
2.3 KiB
JavaScript

import { v4 as uuid } from 'uuid'
import { sendMBSampled } from '../../../infrastructure/event-tracking'
const pdfJsMetrics = {
id: uuid(),
epoch: Date.now(),
totalBandwidth: 0,
}
const SAMPLING_RATE = 0.01
export function trackPdfDownload(response, compileTimeClientE2E) {
const { serviceWorkerMetrics, stats, timings } = response
const t0 = performance.now()
let bandwidth = 0
function firstRenderDone({ timePDFFetched, timePDFRendered }) {
const latencyFetch = timePDFFetched - t0
// The renderer does not yield in case the browser tab is hidden.
// It will yield when the browser tab is visible again.
// This will skew our performance metrics for rendering!
const latencyRender = timePDFRendered - timePDFFetched
done({ latencyFetch, latencyRender })
}
function updateConsumedBandwidth(bytes) {
pdfJsMetrics.totalBandwidth += bytes - bandwidth
bandwidth = bytes
}
let done
const onFirstRenderDone = new Promise(resolve => {
done = resolve
})
// Submit latency along with compile context.
onFirstRenderDone.then(({ latencyFetch, latencyRender }) => {
submitCompileMetrics({
latencyFetch,
latencyRender,
compileTimeClientE2E,
stats,
timings,
})
})
// Submit bandwidth counter separate from compile context.
submitPDFBandwidth({ pdfJsMetrics, serviceWorkerMetrics })
return {
firstRenderDone,
updateConsumedBandwidth,
}
}
function submitCompileMetrics(metrics) {
let {
latencyFetch,
latencyRender,
compileTimeClientE2E,
stats,
timings,
} = metrics
stats = stats || {}
timings = timings || {}
const leanMetrics = {
latencyFetch,
latencyRender,
pdfSize: stats['pdf-size'],
compileTimeClientE2E,
compileTimeServerE2E: timings.compileE2E,
}
sl_console.log('/event/compile-metrics', JSON.stringify(metrics))
sendMBSampled('compile-metrics', leanMetrics, SAMPLING_RATE)
}
function submitPDFBandwidth(metrics) {
const metricsFlat = {}
Object.entries(metrics).forEach(([section, items]) => {
Object.entries(items).forEach(([key, value]) => {
metricsFlat[section + '_' + key] = value
})
})
sl_console.log('/event/pdf-bandwidth', JSON.stringify(metrics))
sendMBSampled('pdf-bandwidth', metricsFlat, SAMPLING_RATE)
}