ContentCacheMetrics.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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'], 1, 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. 1,
  61. request.metricsOpts
  62. )
  63. // How does the hashing scale to pdf size in MB?
  64. Metrics.timing(
  65. 'compute-pdf-caching-relative-to-pdf-size',
  66. timings['compute-pdf-caching'] / (stats['pdf-size'] / ONE_MB),
  67. 1,
  68. request.metricsOpts
  69. )
  70. if (stats['pdf-caching-total-ranges-size']) {
  71. // How does the hashing scale to total ranges size in MB?
  72. Metrics.timing(
  73. 'compute-pdf-caching-relative-to-total-ranges-size',
  74. timings['compute-pdf-caching'] /
  75. (stats['pdf-caching-total-ranges-size'] / ONE_MB),
  76. 1,
  77. request.metricsOpts
  78. )
  79. // How fast is the hashing per range on average?
  80. Metrics.timing(
  81. 'compute-pdf-caching-relative-to-ranges-count',
  82. timings['compute-pdf-caching'] / stats['pdf-caching-n-ranges'],
  83. 1,
  84. request.metricsOpts
  85. )
  86. // How many ranges are new?
  87. Metrics.summary(
  88. 'new-pdf-ranges-relative-to-total-ranges',
  89. (stats['pdf-caching-n-new-ranges'] / stats['pdf-caching-n-ranges']) * 100,
  90. 1,
  91. request.metricsOpts
  92. )
  93. }
  94. // How much content is cacheable?
  95. Metrics.summary(
  96. 'cacheable-ranges-to-pdf-size',
  97. (stats['pdf-caching-total-ranges-size'] / stats['pdf-size']) * 100,
  98. 1,
  99. request.metricsOpts
  100. )
  101. const sizeWhenDownloadedInFull =
  102. // All of the pdf
  103. stats['pdf-size'] -
  104. // These ranges are potentially cached.
  105. stats['pdf-caching-total-ranges-size'] +
  106. // These ranges are not cached.
  107. stats['pdf-caching-new-ranges-size']
  108. // How much bandwidth can we save when downloading the pdf in full?
  109. Metrics.summary(
  110. 'pdf-bandwidth-savings',
  111. 100 - (sizeWhenDownloadedInFull / stats['pdf-size']) * 100,
  112. 1,
  113. request.metricsOpts
  114. )
  115. // How much bandwidth will the pdf incur when downloaded in full?
  116. Metrics.summary(
  117. 'pdf-bandwidth',
  118. sizeWhenDownloadedInFull,
  119. 1,
  120. request.metricsOpts
  121. )
  122. // How much space do the ranges use?
  123. // This will accumulate the ranges size over time, skipping already written ranges.
  124. Metrics.summary(
  125. 'pdf-ranges-disk-size',
  126. stats['pdf-caching-new-ranges-size'] - stats['pdf-caching-reclaimed-space'],
  127. 1,
  128. request.metricsOpts
  129. )
  130. }
  131. module.exports = {
  132. emitPdfStats,
  133. }