2022-03-01 10:09:36 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2021-05-13 09:07:54 -04:00
|
|
|
const Metrics = require('./Metrics')
|
2021-06-23 04:15:05 -04:00
|
|
|
const os = require('os')
|
|
|
|
|
|
|
|
let CACHED_LOAD = {
|
|
|
|
expires: -1,
|
2021-07-13 07:04:48 -04:00
|
|
|
load: [0, 0, 0],
|
2021-06-23 04:15:05 -04:00
|
|
|
}
|
|
|
|
function getSystemLoad() {
|
|
|
|
if (CACHED_LOAD.expires < Date.now()) {
|
|
|
|
CACHED_LOAD = {
|
|
|
|
expires: Date.now() + 10 * 1000,
|
2021-07-13 07:04:48 -04:00
|
|
|
load: os.loadavg(),
|
2021-06-23 04:15:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return CACHED_LOAD.load
|
|
|
|
}
|
2021-05-13 09:07:54 -04:00
|
|
|
|
|
|
|
const ONE_MB = 1024 * 1024
|
|
|
|
|
2022-03-01 10:09:55 -05:00
|
|
|
function emitPdfStats(stats, timings, request) {
|
2021-06-23 09:14:28 -04:00
|
|
|
if (stats['pdf-caching-timed-out']) {
|
2022-03-01 10:09:55 -05:00
|
|
|
Metrics.inc('pdf-caching-timed-out', 1, request.metricsOpts)
|
2021-06-23 09:14:28 -04:00
|
|
|
}
|
2021-08-24 05:22:30 -04:00
|
|
|
if (stats['pdf-caching-queue-limit-reached']) {
|
2022-03-01 10:09:55 -05:00
|
|
|
Metrics.inc('pdf-caching-queue-limit-reached', 1, request.metricsOpts)
|
2021-08-24 05:22:30 -04:00
|
|
|
}
|
2021-05-13 09:07:54 -04:00
|
|
|
if (timings['compute-pdf-caching']) {
|
2022-03-01 10:09:55 -05:00
|
|
|
emitPdfCachingStats(stats, timings, request)
|
2021-05-13 09:07:54 -04:00
|
|
|
} else {
|
|
|
|
// How much bandwidth will the pdf incur when downloaded in full?
|
2022-03-01 10:09:55 -05:00
|
|
|
Metrics.summary('pdf-bandwidth', stats['pdf-size'], 1, request.metricsOpts)
|
2021-05-13 09:07:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 10:09:55 -05:00
|
|
|
function emitPdfCachingStats(stats, timings, request) {
|
2021-05-13 09:07:54 -04:00
|
|
|
if (!stats['pdf-size']) return // double check
|
|
|
|
|
2021-06-23 06:28:31 -04:00
|
|
|
// How much extra time did we spent in PDF.js?
|
2022-03-01 10:09:55 -05:00
|
|
|
Metrics.timing(
|
|
|
|
'compute-pdf-caching',
|
|
|
|
timings['compute-pdf-caching'],
|
|
|
|
1,
|
|
|
|
request.metricsOpts
|
|
|
|
)
|
2021-06-23 06:28:31 -04:00
|
|
|
|
2021-05-13 09:07:54 -04:00
|
|
|
// How large is the overhead of hashing up-front?
|
|
|
|
const fraction =
|
|
|
|
timings.compileE2E - timings['compute-pdf-caching'] !== 0
|
|
|
|
? timings.compileE2E /
|
|
|
|
(timings.compileE2E - timings['compute-pdf-caching'])
|
|
|
|
: 1
|
2021-06-23 06:27:19 -04:00
|
|
|
if (fraction > 1.5 && timings.compileE2E > 10 * 1000) {
|
2021-06-23 04:15:05 -04:00
|
|
|
logger.warn(
|
|
|
|
{
|
|
|
|
stats,
|
|
|
|
timings,
|
2021-07-13 07:04:48 -04:00
|
|
|
load: getSystemLoad(),
|
2021-06-23 04:15:05 -04:00
|
|
|
},
|
|
|
|
'slow pdf caching'
|
|
|
|
)
|
|
|
|
}
|
2022-03-01 10:09:55 -05:00
|
|
|
Metrics.summary(
|
|
|
|
'overhead-compute-pdf-ranges',
|
|
|
|
fraction * 100 - 100,
|
|
|
|
1,
|
|
|
|
request.metricsOpts
|
|
|
|
)
|
2021-05-13 09:07:54 -04:00
|
|
|
|
|
|
|
// How does the hashing scale to pdf size in MB?
|
|
|
|
Metrics.timing(
|
|
|
|
'compute-pdf-caching-relative-to-pdf-size',
|
2022-03-01 10:09:55 -05:00
|
|
|
timings['compute-pdf-caching'] / (stats['pdf-size'] / ONE_MB),
|
|
|
|
1,
|
|
|
|
request.metricsOpts
|
2021-05-13 09:07:54 -04:00
|
|
|
)
|
|
|
|
if (stats['pdf-caching-total-ranges-size']) {
|
|
|
|
// How does the hashing scale to total ranges size in MB?
|
|
|
|
Metrics.timing(
|
|
|
|
'compute-pdf-caching-relative-to-total-ranges-size',
|
|
|
|
timings['compute-pdf-caching'] /
|
2022-03-01 10:09:55 -05:00
|
|
|
(stats['pdf-caching-total-ranges-size'] / ONE_MB),
|
|
|
|
1,
|
|
|
|
request.metricsOpts
|
2021-05-13 09:07:54 -04:00
|
|
|
)
|
|
|
|
// How fast is the hashing per range on average?
|
|
|
|
Metrics.timing(
|
|
|
|
'compute-pdf-caching-relative-to-ranges-count',
|
2022-03-01 10:09:55 -05:00
|
|
|
timings['compute-pdf-caching'] / stats['pdf-caching-n-ranges'],
|
|
|
|
1,
|
|
|
|
request.metricsOpts
|
2021-05-13 09:07:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// How many ranges are new?
|
|
|
|
Metrics.summary(
|
|
|
|
'new-pdf-ranges-relative-to-total-ranges',
|
2022-03-01 10:09:55 -05:00
|
|
|
(stats['pdf-caching-n-new-ranges'] / stats['pdf-caching-n-ranges']) * 100,
|
|
|
|
1,
|
|
|
|
request.metricsOpts
|
2021-05-13 09:07:54 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// How much content is cacheable?
|
|
|
|
Metrics.summary(
|
|
|
|
'cacheable-ranges-to-pdf-size',
|
2022-03-01 10:09:55 -05:00
|
|
|
(stats['pdf-caching-total-ranges-size'] / stats['pdf-size']) * 100,
|
|
|
|
1,
|
|
|
|
request.metricsOpts
|
2021-05-13 09:07:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const sizeWhenDownloadedInFull =
|
|
|
|
// All of the pdf
|
|
|
|
stats['pdf-size'] -
|
|
|
|
// These ranges are potentially cached.
|
|
|
|
stats['pdf-caching-total-ranges-size'] +
|
|
|
|
// These ranges are not cached.
|
|
|
|
stats['pdf-caching-new-ranges-size']
|
|
|
|
|
|
|
|
// How much bandwidth can we save when downloading the pdf in full?
|
|
|
|
Metrics.summary(
|
|
|
|
'pdf-bandwidth-savings',
|
2022-03-01 10:09:55 -05:00
|
|
|
100 - (sizeWhenDownloadedInFull / stats['pdf-size']) * 100,
|
|
|
|
1,
|
|
|
|
request.metricsOpts
|
2021-05-13 09:07:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// How much bandwidth will the pdf incur when downloaded in full?
|
2022-03-01 10:09:55 -05:00
|
|
|
Metrics.summary(
|
|
|
|
'pdf-bandwidth',
|
|
|
|
sizeWhenDownloadedInFull,
|
|
|
|
1,
|
|
|
|
request.metricsOpts
|
|
|
|
)
|
2021-05-13 09:07:54 -04:00
|
|
|
|
|
|
|
// How much space do the ranges use?
|
|
|
|
// This will accumulate the ranges size over time, skipping already written ranges.
|
2021-05-18 13:06:15 -04:00
|
|
|
Metrics.summary(
|
|
|
|
'pdf-ranges-disk-size',
|
2022-03-01 10:09:55 -05:00
|
|
|
stats['pdf-caching-new-ranges-size'] - stats['pdf-caching-reclaimed-space'],
|
|
|
|
1,
|
|
|
|
request.metricsOpts
|
2021-05-18 13:06:15 -04:00
|
|
|
)
|
2021-05-13 09:07:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2021-07-13 07:04:48 -04:00
|
|
|
emitPdfStats,
|
2021-05-13 09:07:54 -04:00
|
|
|
}
|