-// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
+// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
import { type PerformanceEntry, PerformanceObserver, performance } from 'node:perf_hooks'
import type { URL } from 'node:url'
import { secondsToMilliseconds } from 'date-fns'
+import { BaseError } from '../exception/index.js'
import {
ConfigurationSection,
type IncomingRequestCommand,
private readonly statistics: Statistics
private displayInterval?: NodeJS.Timeout
- private constructor (objId: string | undefined, objName: string | undefined, uri: URL) {
- this.objId = objId ?? 'Object id not specified'
- this.objName = objName ?? 'Object name not specified'
+ private constructor (objId: string, objName: string, uri: URL) {
+ this.objId = objId
+ this.objName = objName
this.initializePerformanceObserver()
this.statistics = {
id: this.objId,
}
public static getInstance (
- objId: string,
- objName: string,
- uri: URL
+ objId: string | undefined,
+ objName: string | undefined,
+ uri: URL | undefined
): PerformanceStatistics | undefined {
+ const logPfx = logPrefix(' Performance statistics')
+ if (objId == null) {
+ const errMsg = 'Cannot get performance statistics instance without specifying object id'
+ logger.error(`${logPfx} ${errMsg}`)
+ throw new BaseError(errMsg)
+ }
+ if (objName == null) {
+ const errMsg = 'Cannot get performance statistics instance without specifying object name'
+ logger.error(`${logPfx} ${errMsg}`)
+ throw new BaseError(errMsg)
+ }
+ if (uri == null) {
+ const errMsg = 'Cannot get performance statistics instance without specifying object uri'
+ logger.error(`${logPfx} ${errMsg}`)
+ throw new BaseError(errMsg)
+ }
if (!PerformanceStatistics.instances.has(objId)) {
PerformanceStatistics.instances.set(objId, new PerformanceStatistics(objId, objName, uri))
}
}
private initializePerformanceObserver (): void {
- this.performanceObserver = new PerformanceObserver((performanceObserverList) => {
+ this.performanceObserver = new PerformanceObserver(performanceObserverList => {
const lastPerformanceEntry = performanceObserverList.getEntries()[0]
// logger.debug(
// `${this.logPrefix()} '${lastPerformanceEntry.name}' performance entry: %j`,