ContentCacheMetrics.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const logger = require('@overleaf/logger')
  2. const Metrics = require('./Metrics')
  3. const os = require('os')
  4. let CACHED_LOAD = {
  5. expires: -1,
  6. load: [0, 0, 0],
  7. }
  8. function getSystemLoad() {
  9. if (CACHED_LOAD.expires < Date.now()) {
  10. CACHED_LOAD = {
  11. expires: Date.now() + 10 * 1000,
  12. load: os.loadavg(),
  13. }
  14. }
  15. return CACHED_LOAD.load
  16. }
  17. const ONE_MB = 1024 * 1024
  18. function emitPdfStats(stats, timings, request) {
  19. if (stats['pdf-caching-timed-out']) {
  20. Metrics.inc('pdf-caching-timed-out', 1, request.metricsOpts)
  21. }
  22. if (stats['pdf-caching-queue-limit-reached']) {
  23. Metrics.inc('pdf-caching-queue-limit-reached', 1, request.metricsOpts)
  24. }
  25. if (timings['compute-pdf-caching']) {
  26. emitPdfCachingStats(stats, timings, request)
  27. } else {
  28. // How much bandwidth will the pdf incur when downloaded in full?
  29. Metrics.summary('pdf-bandwidth', stats['pdf-size'], request.metricsOpts)
  30. }
  31. }
  32. function emitPdfCachingStats(stats, timings, request) {
  33. if (!stats['pdf-size']) return // double check
  34. // How much extra time did we spent in PDF.js?
  35. Metrics.timing(
  36. 'compute-pdf-caching',
  37. timings['compute-pdf-caching'],
  38. 1,
  39. request.metricsOpts
  40. )
  41. // How large is the overhead of hashing up-front?
  42. const fraction =
  43. timings.compileE2E - timings['compute-pdf-caching'] !== 0
  44. ? timings.compileE2E /
  45. (timings.compileE2E - timings['compute-pdf-caching'])
  46. : 1
  47. if (fraction > 1.5 && timings.compileE2E > 10 * 1000) {
  48. logger.warn(
  49. {
  50. stats,
  51. timings,
  52. load: getSystemLoad(),
  53. },
  54. 'slow pdf caching'
  55. )
  56. }
  57. Metrics.summary(
  58. 'overhead-compute-pdf-ranges',
  59. fraction * 100 - 100,
  60. request.metricsOpts
  61. )
  62. // How does the hashing scale to pdf size in MB?
  63. Metrics.timing(
  64. 'compute-pdf-caching-relative-to-pdf-size',
  65. timings['compute-pdf-caching'] / (stats['pdf-size'] / ONE_MB),
  66. 1,
  67. request.metricsOpts
  68. )
  69. if (stats['pdf-caching-total-ranges-size']) {
  70. // How does the hashing scale to total ranges size in MB?
  71. Metrics.timing(
  72. 'compute-pdf-caching-relative-to-total-ranges-size',
  73. timings['compute-pdf-caching'] /
  74. (stats['pdf-caching-total-ranges-size'] / ONE_MB),
  75. 1,
  76. request.metricsOpts
  77. )
  78. // How fast is the hashing per range on average?
  79. Metrics.timing(
  80. 'compute-pdf-caching-relative-to-ranges-count',
  81. timings['compute-pdf-caching'] / stats['pdf-caching-n-ranges'],
  82. 1,
  83. request.metricsOpts
  84. )
  85. // How many ranges are new?
  86. Metrics.summary(
  87. 'new-pdf-ranges-relative-to-total-ranges',
  88. (stats['pdf-caching-n-new-ranges'] / stats['pdf-caching-n-ranges']) * 100,
  89. request.metricsOpts
  90. )
  91. }
  92. // How much content is cacheable?
  93. Metrics.summary(
  94. 'cacheable-ranges-to-pdf-size',
  95. (stats['pdf-caching-total-ranges-size'] / stats['pdf-size']) * 100,
  96. request.metricsOpts
  97. )
  98. const sizeWhenDownloadedInFull =
  99. // All of the pdf
  100. stats['pdf-size'] -
  101. // These ranges are potentially cached.
  102. stats['pdf-caching-total-ranges-size'] +
  103. // These ranges are not cached.
  104. stats['pdf-caching-new-ranges-size']
  105. // How much bandwidth can we save when downloading the pdf in full?
  106. Metrics.summary(
  107. 'pdf-bandwidth-savings',
  108. 100 - (sizeWhenDownloadedInFull / stats['pdf-size']) * 100,
  109. request.metricsOpts
  110. )
  111. // How much bandwidth will the pdf incur when downloaded in full?
  112. Metrics.summary(
  113. 'pdf-bandwidth',
  114. sizeWhenDownloadedInFull,
  115. request.metricsOpts
  116. )
  117. // How much space do the ranges use?
  118. // This will accumulate the ranges size over time, skipping already written ranges.
  119. Metrics.summary(
  120. 'pdf-ranges-disk-size',
  121. stats['pdf-caching-new-ranges-size'] - stats['pdf-caching-reclaimed-space'],
  122. request.metricsOpts
  123. )
  124. }
  125. module.exports = {
  126. emitPdfStats,
  127. }