mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-08 18:31:43 +00:00
test: add browse instrumentation helpers
This commit is contained in:
parent
6d3773a0b8
commit
c55361c552
3 changed files with 127 additions and 4 deletions
57
server/utils/queries/libraryBrowseInstrumentation.js
Normal file
57
server/utils/queries/libraryBrowseInstrumentation.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
const { performance } = require('perf_hooks')
|
||||
|
||||
function getPhaseName(markName) {
|
||||
return String(markName || '').split(':')[0]
|
||||
}
|
||||
|
||||
function createBrowseRequestProfile(context = {}) {
|
||||
const now = typeof context.now === 'function' ? context.now : () => performance.now()
|
||||
const marks = []
|
||||
|
||||
return {
|
||||
route: context.route || null,
|
||||
libraryId: context.libraryId || null,
|
||||
now,
|
||||
startedAt: now(),
|
||||
marks,
|
||||
mark(name) {
|
||||
marks.push({ name, at: now() })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function finishBrowseRequestProfile(profile, { slowMs = 1000 } = {}) {
|
||||
const endedAt = profile.now()
|
||||
const phases = {}
|
||||
const openPhases = new Map()
|
||||
|
||||
profile.marks.forEach((mark) => {
|
||||
const phaseName = getPhaseName(mark.name)
|
||||
if (!phaseName) return
|
||||
|
||||
if (String(mark.name).endsWith(':start')) {
|
||||
openPhases.set(phaseName, mark.at)
|
||||
return
|
||||
}
|
||||
|
||||
if (String(mark.name).endsWith(':end') && openPhases.has(phaseName)) {
|
||||
phases[phaseName] = Math.round(mark.at - openPhases.get(phaseName))
|
||||
openPhases.delete(phaseName)
|
||||
}
|
||||
})
|
||||
|
||||
const totalMs = Math.round(endedAt - profile.startedAt)
|
||||
|
||||
return {
|
||||
route: profile.route || null,
|
||||
libraryId: profile.libraryId || null,
|
||||
totalMs,
|
||||
phases,
|
||||
isSlow: totalMs >= slowMs
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createBrowseRequestProfile,
|
||||
finishBrowseRequestProfile
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue