Skip to content

Commit

Permalink
performance: Add logs for application drawing average fps and drops i…
Browse files Browse the repository at this point in the history
…n it
  • Loading branch information
rafaellehmkuhl authored and ArturoManzoli committed Aug 2, 2024
1 parent a98782b commit 6abdcb9
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/stores/omniscientLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,44 @@ export const useOmniscientLoggerStore = defineStore('omniscient-logger', () => {
})
})
}, 250)

// Routine to log the framerate of the application rendering
const appFrameRateHistory = ref<number[]>([])
const appAverageFrameRateSampleDelay = 100
const appAverageFrameRateLogDelay = 1000
let lastAppAverageFpsLog = new Date()
const fpsMeter = (): void => {
let prevTime = performance.now()
let frames = 0

requestAnimationFrame(function loop() {
const time = performance.now()
frames++
if (time > prevTime + appAverageFrameRateSampleDelay) {
const currentFPS = Math.round((frames * 1000) / (time - prevTime))
prevTime = time
frames = 0

appFrameRateHistory.value.push(currentFPS)
appFrameRateHistory.value.splice(0, appFrameRateHistory.value.length - 10)

const average = appFrameRateHistory.value.reduce((a, b) => a + b, 0) / appFrameRateHistory.value.length
const minThreshold = 0.9 * average

// Warn about drops in the framerate of the application rendering
if (currentFPS < minThreshold) {
console.warn(`Drop in the framerate detected for the application rendering: ${currentFPS.toFixed(2)} fps.`)
}

// Log the average framerate of the application rendering recursively
if (new Date().getTime() - lastAppAverageFpsLog.getTime() > appAverageFrameRateLogDelay) {
console.debug(`Average framerate for the application rendering: ${currentFPS.toFixed(2)} fps.`)
lastAppAverageFpsLog = new Date()
}
}

requestAnimationFrame(loop)
})
}
fpsMeter()
})

0 comments on commit 6abdcb9

Please sign in to comment.