metrics.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { v4 as uuid } from 'uuid'
  2. import { sendMB } from '../../../infrastructure/event-tracking'
  3. import getMeta from '../../../utils/meta'
  4. // VERSION should get incremented when making changes to caching behavior or
  5. // adjusting metrics collection.
  6. // Keep in sync with the service worker.
  7. const VERSION = 2
  8. const pdfJsMetrics = {
  9. version: VERSION,
  10. id: uuid(),
  11. epoch: Date.now(),
  12. totalBandwidth: 0,
  13. }
  14. const SAMPLING_RATE = 0.01
  15. export function trackPdfDownload(response, compileTimeClientE2E) {
  16. const { serviceWorkerMetrics, stats, timings } = response
  17. const t0 = performance.now()
  18. let bandwidth = 0
  19. const deliveryLatencies = {
  20. compileTimeClientE2E,
  21. compileTimeServerE2E: timings?.compileE2E,
  22. }
  23. function firstRenderDone({ timePDFFetched, timePDFRendered }) {
  24. const latencyFetch = Math.ceil(timePDFFetched - t0)
  25. deliveryLatencies.latencyFetch = latencyFetch
  26. // The renderer does not yield in case the browser tab is hidden.
  27. // It will yield when the browser tab is visible again.
  28. // This will skew our performance metrics for rendering!
  29. // We are omitting the render time in case we detect this state.
  30. let latencyRender
  31. if (timePDFRendered) {
  32. latencyRender = Math.ceil(timePDFRendered - timePDFFetched)
  33. deliveryLatencies.latencyRender = latencyRender
  34. }
  35. done({ latencyFetch, latencyRender })
  36. }
  37. function updateConsumedBandwidth(bytes) {
  38. pdfJsMetrics.totalBandwidth += bytes - bandwidth
  39. bandwidth = bytes
  40. }
  41. let done
  42. const onFirstRenderDone = new Promise(resolve => {
  43. done = resolve
  44. })
  45. if (getMeta('ol-trackPdfDownload')) {
  46. // Submit latency along with compile context.
  47. onFirstRenderDone.then(({ latencyFetch, latencyRender }) => {
  48. submitCompileMetrics({
  49. latencyFetch,
  50. latencyRender,
  51. compileTimeClientE2E,
  52. stats,
  53. timings,
  54. })
  55. })
  56. // Submit bandwidth counter separate from compile context.
  57. submitPDFBandwidth({ pdfJsMetrics, serviceWorkerMetrics })
  58. }
  59. return {
  60. deliveryLatencies,
  61. firstRenderDone,
  62. updateConsumedBandwidth,
  63. }
  64. }
  65. function submitCompileMetrics(metrics) {
  66. const { latencyFetch, latencyRender, compileTimeClientE2E } = metrics
  67. const leanMetrics = {
  68. version: VERSION,
  69. latencyFetch,
  70. latencyRender,
  71. compileTimeClientE2E,
  72. }
  73. sl_console.log('/event/compile-metrics', JSON.stringify(metrics))
  74. sendMB('compile-metrics-v5', leanMetrics, SAMPLING_RATE)
  75. }
  76. function submitPDFBandwidth(metrics) {
  77. const metricsFlat = {}
  78. Object.entries(metrics).forEach(([section, items]) => {
  79. if (!items) return
  80. Object.entries(items).forEach(([key, value]) => {
  81. metricsFlat[section + '_' + key] = value
  82. })
  83. })
  84. const leanMetrics = {}
  85. Object.entries(metricsFlat).forEach(([metric, value]) => {
  86. if (
  87. [
  88. 'serviceWorkerMetrics_id',
  89. 'serviceWorkerMetrics_cachedBytes',
  90. 'serviceWorkerMetrics_fetchedBytes',
  91. 'serviceWorkerMetrics_requestedBytes',
  92. 'serviceWorkerMetrics_version',
  93. 'serviceWorkerMetrics_epoch',
  94. ].includes(metric)
  95. ) {
  96. leanMetrics[metric] = value
  97. }
  98. })
  99. if (Object.entries(leanMetrics).length === 0) {
  100. return
  101. }
  102. sl_console.log('/event/pdf-bandwidth', JSON.stringify(metrics))
  103. sendMB('pdf-bandwidth-v5', leanMetrics, SAMPLING_RATE)
  104. }