X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fperformance%2FPerformanceStatistics.ts;h=88bb29d04d97d9aa315df258dc0923334d803d1e;hb=55ae7b758f478a2beb4557bbc96363fb913dcc73;hp=f61d095874f9dafbde0df907b7ad60e58f504098;hpb=1fb214826a367053852c7d56e91775cdb92c9588;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index f61d0958..88bb29d0 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -1,4 +1,4 @@ -// 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' @@ -6,6 +6,7 @@ import { parentPort } from 'node:worker_threads' import { secondsToMilliseconds } from 'date-fns' +import { BaseError } from '../exception/index.js' import { ConfigurationSection, type IncomingRequestCommand, @@ -41,8 +42,8 @@ export class PerformanceStatistics { PerformanceStatistics >() - private readonly objId: string - private readonly objName: string + private readonly objId: string | undefined + private readonly objName: string | undefined private performanceObserver!: PerformanceObserver private readonly statistics: Statistics private displayInterval?: NodeJS.Timeout @@ -52,8 +53,8 @@ export class PerformanceStatistics { this.objName = objName this.initializePerformanceObserver() this.statistics = { - id: this.objId ?? 'Object id not specified', - name: this.objName ?? 'Object name not specified', + id: this.objId, + name: this.objName, uri: uri.toString(), createdAt: new Date(), statisticsData: new Map() @@ -61,10 +62,26 @@ export class PerformanceStatistics { } 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)) } @@ -164,7 +181,7 @@ export class PerformanceStatistics { this.stopLogStatisticsInterval() performance.clearMarks() performance.clearMeasures() - this.performanceObserver?.disconnect() + this.performanceObserver.disconnect() } public restart (): void { @@ -173,7 +190,7 @@ export class PerformanceStatistics { } 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`, @@ -185,7 +202,7 @@ export class PerformanceStatistics { } private logStatistics (): void { - logger.info(`${this.logPrefix()}`, { + logger.info(this.logPrefix(), { ...this.statistics, statisticsData: JSONStringifyWithMapSupport(this.statistics.statisticsData) })