ContentCacheMetrics.js 3.3 KB

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