mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-07 09:51:37 +00:00
fix: wire browse request timing hooks
This commit is contained in:
parent
c55361c552
commit
da490605b7
3 changed files with 146 additions and 13 deletions
|
|
@ -4,15 +4,40 @@ const Logger = require('../Logger')
|
||||||
|
|
||||||
const histograms = new Map()
|
const histograms = new Map()
|
||||||
|
|
||||||
function createRequestScopedFindOptions(findOptions, funcName) {
|
function callTimingHook(requestTiming, hookName, ...args) {
|
||||||
if (!findOptions || typeof findOptions !== 'object' || Array.isArray(findOptions)) {
|
if (!requestTiming || typeof requestTiming[hookName] !== 'function') return
|
||||||
return findOptions
|
requestTiming[hookName](...args)
|
||||||
|
}
|
||||||
|
|
||||||
|
function createRequestScopedLogging(funcName, logging, requestTiming) {
|
||||||
|
const logger = (...args) => {
|
||||||
|
Logger.info(`[${funcName}] ${args[0]} Elapsed time: ${args[1]}ms`)
|
||||||
|
if (typeof logging === 'function') {
|
||||||
|
logging(...args)
|
||||||
|
}
|
||||||
|
callTimingHook(requestTiming, 'onQuery', ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return logger
|
||||||
|
}
|
||||||
|
|
||||||
|
function createRequestScopedFindOptions(findOptions, funcName) {
|
||||||
|
if (!findOptions || typeof findOptions !== 'object' || Array.isArray(findOptions)) {
|
||||||
|
return {
|
||||||
|
findOptions,
|
||||||
|
requestTiming: null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestTiming = findOptions.requestTiming || null
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...findOptions,
|
requestTiming,
|
||||||
logging: (query, time) => Logger.info(`[${funcName}] ${query} Elapsed time: ${time}ms`),
|
findOptions: {
|
||||||
benchmark: true
|
...findOptions,
|
||||||
|
logging: createRequestScopedLogging(funcName, findOptions.logging, requestTiming),
|
||||||
|
benchmark: true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -25,22 +50,26 @@ function profile(asyncFunc, isFindQuery = true, funcName = asyncFunc.name) {
|
||||||
const histogram = histograms.get(funcName)
|
const histogram = histograms.get(funcName)
|
||||||
|
|
||||||
return async (...args) => {
|
return async (...args) => {
|
||||||
const requestArgs = isFindQuery ? [createRequestScopedFindOptions(args[0], funcName), ...args.slice(1)] : args
|
const requestScoped = isFindQuery ? createRequestScopedFindOptions(args[0], funcName) : { findOptions: args[0], requestTiming: null }
|
||||||
|
const requestArgs = isFindQuery ? [requestScoped.findOptions, ...args.slice(1)] : args
|
||||||
|
|
||||||
if (isFindQuery) {
|
if (isFindQuery) {
|
||||||
const findOptions = requestArgs[0]
|
const findOptions = requestArgs[0]
|
||||||
Logger.info(`[${funcName}] findOptions:`, util.inspect(findOptions, { depth: null }))
|
Logger.info(`[${funcName}] findOptions:`, util.inspect(findOptions, { depth: null }))
|
||||||
}
|
}
|
||||||
const start = performance.now()
|
const start = performance.now()
|
||||||
|
callTimingHook(requestScoped.requestTiming, 'onStart', requestArgs[0])
|
||||||
try {
|
try {
|
||||||
const result = await asyncFunc(...requestArgs)
|
const result = await asyncFunc(...requestArgs)
|
||||||
|
callTimingHook(requestScoped.requestTiming, 'onFinish', result)
|
||||||
return result
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
Logger.error(`[${funcName}] failed`)
|
Logger.error(`[${funcName}] failed`)
|
||||||
|
callTimingHook(requestScoped.requestTiming, 'onError', error)
|
||||||
throw error
|
throw error
|
||||||
} finally {
|
} finally {
|
||||||
const end = performance.now()
|
const end = performance.now()
|
||||||
const duration = Math.round(end - start)
|
const duration = Math.max(1, Math.round(end - start))
|
||||||
histogram.record(duration)
|
histogram.record(duration)
|
||||||
histogram.values.push(duration)
|
histogram.values.push(duration)
|
||||||
Logger.info(`[${funcName}] duration: ${duration}ms`)
|
Logger.info(`[${funcName}] duration: ${duration}ms`)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,24 @@ function getPhaseName(markName) {
|
||||||
return String(markName || '').split(':')[0]
|
return String(markName || '').split(':')[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createBrowsePhaseTiming(profile, phaseName) {
|
||||||
|
if (!profile || typeof profile.mark !== 'function' || !phaseName) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
onStart() {
|
||||||
|
profile.mark(`${phaseName}:start`)
|
||||||
|
},
|
||||||
|
onFinish() {
|
||||||
|
profile.mark(`${phaseName}:end`)
|
||||||
|
},
|
||||||
|
onError() {
|
||||||
|
profile.mark(`${phaseName}:end`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function createBrowseRequestProfile(context = {}) {
|
function createBrowseRequestProfile(context = {}) {
|
||||||
const now = typeof context.now === 'function' ? context.now : () => performance.now()
|
const now = typeof context.now === 'function' ? context.now : () => performance.now()
|
||||||
const marks = []
|
const marks = []
|
||||||
|
|
@ -21,11 +39,24 @@ function createBrowseRequestProfile(context = {}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function finishBrowseRequestProfile(profile, { slowMs = 1000 } = {}) {
|
function finishBrowseRequestProfile(profile, { slowMs = 1000 } = {}) {
|
||||||
const endedAt = profile.now()
|
if (!profile || typeof profile !== 'object') {
|
||||||
|
return {
|
||||||
|
route: null,
|
||||||
|
libraryId: null,
|
||||||
|
totalMs: 0,
|
||||||
|
phases: {},
|
||||||
|
isSlow: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = typeof profile.now === 'function' ? profile.now : () => performance.now()
|
||||||
|
const startedAt = typeof profile.startedAt === 'number' ? profile.startedAt : null
|
||||||
|
const marks = Array.isArray(profile.marks) ? profile.marks : []
|
||||||
|
const endedAt = startedAt === null ? null : now()
|
||||||
const phases = {}
|
const phases = {}
|
||||||
const openPhases = new Map()
|
const openPhases = new Map()
|
||||||
|
|
||||||
profile.marks.forEach((mark) => {
|
marks.forEach((mark) => {
|
||||||
const phaseName = getPhaseName(mark.name)
|
const phaseName = getPhaseName(mark.name)
|
||||||
if (!phaseName) return
|
if (!phaseName) return
|
||||||
|
|
||||||
|
|
@ -35,23 +66,24 @@ function finishBrowseRequestProfile(profile, { slowMs = 1000 } = {}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (String(mark.name).endsWith(':end') && openPhases.has(phaseName)) {
|
if (String(mark.name).endsWith(':end') && openPhases.has(phaseName)) {
|
||||||
phases[phaseName] = Math.round(mark.at - openPhases.get(phaseName))
|
phases[phaseName] = (phases[phaseName] || 0) + Math.round(mark.at - openPhases.get(phaseName))
|
||||||
openPhases.delete(phaseName)
|
openPhases.delete(phaseName)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const totalMs = Math.round(endedAt - profile.startedAt)
|
const totalMs = endedAt === null ? 0 : Math.round(endedAt - startedAt)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
route: profile.route || null,
|
route: profile.route || null,
|
||||||
libraryId: profile.libraryId || null,
|
libraryId: profile.libraryId || null,
|
||||||
totalMs,
|
totalMs,
|
||||||
phases,
|
phases,
|
||||||
isSlow: totalMs >= slowMs
|
isSlow: totalMs > 0 && totalMs >= slowMs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
createBrowsePhaseTiming,
|
||||||
createBrowseRequestProfile,
|
createBrowseRequestProfile,
|
||||||
finishBrowseRequestProfile
|
finishBrowseRequestProfile
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,19 @@
|
||||||
const chai = require('chai')
|
const chai = require('chai')
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
|
const sinon = require('sinon')
|
||||||
|
|
||||||
const {
|
const {
|
||||||
createBrowseRequestProfile,
|
createBrowseRequestProfile,
|
||||||
|
createBrowsePhaseTiming,
|
||||||
finishBrowseRequestProfile
|
finishBrowseRequestProfile
|
||||||
} = require('../../../server/utils/queries/libraryBrowseInstrumentation')
|
} = require('../../../server/utils/queries/libraryBrowseInstrumentation')
|
||||||
|
const { profile } = require('../../../server/utils/profiler')
|
||||||
|
|
||||||
describe('libraryBrowseInstrumentation', () => {
|
describe('libraryBrowseInstrumentation', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
sinon.restore()
|
||||||
|
})
|
||||||
|
|
||||||
it('records named browse phases and returns a slow-summary payload', () => {
|
it('records named browse phases and returns a slow-summary payload', () => {
|
||||||
const timestamps = [
|
const timestamps = [
|
||||||
100,
|
100,
|
||||||
|
|
@ -51,4 +58,69 @@ describe('libraryBrowseInstrumentation', () => {
|
||||||
isSlow: true
|
isSlow: true
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('accumulates repeated timings for the same phase', () => {
|
||||||
|
const timestamps = [100, 110, 140, 150, 190, 210]
|
||||||
|
const profile = createBrowseRequestProfile({
|
||||||
|
route: 'GET /api/libraries/:id/items',
|
||||||
|
libraryId: 'lib_1',
|
||||||
|
now: () => timestamps.shift()
|
||||||
|
})
|
||||||
|
|
||||||
|
profile.mark('rows:start')
|
||||||
|
profile.mark('rows:end')
|
||||||
|
profile.mark('rows:start')
|
||||||
|
profile.mark('rows:end')
|
||||||
|
|
||||||
|
const result = finishBrowseRequestProfile(profile, { slowMs: 500 })
|
||||||
|
|
||||||
|
expect(result.phases).to.deep.equal({ rows: 70 })
|
||||||
|
expect(result.isSlow).to.equal(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('handles null and partial profiles safely', () => {
|
||||||
|
expect(finishBrowseRequestProfile(null, { slowMs: 0 })).to.deep.equal({
|
||||||
|
route: null,
|
||||||
|
libraryId: null,
|
||||||
|
totalMs: 0,
|
||||||
|
phases: {},
|
||||||
|
isSlow: false
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(finishBrowseRequestProfile({ route: 'GET /items', marks: [{ name: 'rows:start', at: 10 }] })).to.deep.equal({
|
||||||
|
route: 'GET /items',
|
||||||
|
libraryId: null,
|
||||||
|
totalMs: 0,
|
||||||
|
phases: {},
|
||||||
|
isSlow: false
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('composes caller logging with request-scoped browse timing hooks', async () => {
|
||||||
|
const timestamps = [100, 110, 150, 180]
|
||||||
|
const browseProfile = createBrowseRequestProfile({
|
||||||
|
route: 'GET /api/libraries/:id/items',
|
||||||
|
libraryId: 'lib_1',
|
||||||
|
now: () => timestamps.shift()
|
||||||
|
})
|
||||||
|
const callerLogging = sinon.spy()
|
||||||
|
const originalFindOptions = {
|
||||||
|
where: { id: 'item_1' },
|
||||||
|
logging: callerLogging,
|
||||||
|
requestTiming: createBrowsePhaseTiming(browseProfile, 'rows')
|
||||||
|
}
|
||||||
|
|
||||||
|
const wrappedQuery = profile(async (findOptions) => {
|
||||||
|
findOptions.logging('SELECT 1', 12)
|
||||||
|
return findOptions
|
||||||
|
}, true, 'browseRows')
|
||||||
|
|
||||||
|
const wrappedFindOptions = await wrappedQuery(originalFindOptions)
|
||||||
|
const result = finishBrowseRequestProfile(browseProfile, { slowMs: 1000 })
|
||||||
|
|
||||||
|
expect(wrappedFindOptions).to.not.equal(originalFindOptions)
|
||||||
|
expect(originalFindOptions).to.not.have.property('benchmark')
|
||||||
|
expect(callerLogging.calledOnceWithExactly('SELECT 1', 12)).to.equal(true)
|
||||||
|
expect(result.phases).to.deep.equal({ rows: 40 })
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue