ContentCacheMetrics.js 3.6 KB

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