X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fperformance%2FPerformanceStatistics.ts;h=864446c6ce30ee1bad805c65a61eec9b46755e2d;hb=6c7388a4c4c47e9d19511b8e9b70b0f57d6dbd5f;hp=88ea748c635bbdea9023c24819740d8184e749dc;hpb=5199f9fdf202eb534948f165a0994e1993675aa8;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 88ea748c..864446c6 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -1,35 +1,37 @@ -// 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 { performance, type PerformanceEntry, PerformanceObserver } from 'node:perf_hooks' import type { URL } from 'node:url' import { parentPort } from 'node:worker_threads' import { secondsToMilliseconds } from 'date-fns' +import { CircularBuffer } from 'mnemonist' +import { is, mean, median } from 'rambda' +import { BaseError } from '../exception/index.js' import { ConfigurationSection, type IncomingRequestCommand, type LogConfiguration, + MapStringifyFormat, MessageType, type RequestCommand, type Statistics, + type StatisticsData, type StorageConfiguration, type TimestampedData } from '../types/index.js' import { - CircularArray, + buildPerformanceStatisticsMessage, Configuration, Constants, - JSONStringifyWithMapSupport, - average, - buildPerformanceStatisticsMessage, extractTimeSeriesValues, formatDurationSeconds, generateUUID, - logPrefix, + JSONStringify, logger, + logPrefix, max, - median, min, nthPercentile, stdDeviation @@ -47,9 +49,9 @@ export class PerformanceStatistics { 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, @@ -61,16 +63,40 @@ export class PerformanceStatistics { } public static getInstance ( - objId: string, - objName: string, - uri: URL + objId: string | undefined, + objName: string | undefined, + uri: URL | undefined ): PerformanceStatistics | undefined { + if (objId == null) { + const errMsg = 'Cannot get performance statistics instance without specifying object id' + logger.error(`${PerformanceStatistics.logPrefix()} ${errMsg}`) + throw new BaseError(errMsg) + } + if (objName == null) { + const errMsg = 'Cannot get performance statistics instance without specifying object name' + logger.error(`${PerformanceStatistics.logPrefix()} ${errMsg}`) + throw new BaseError(errMsg) + } + if (uri == null) { + const errMsg = 'Cannot get performance statistics instance without specifying object uri' + logger.error(`${PerformanceStatistics.logPrefix()} ${errMsg}`) + throw new BaseError(errMsg) + } if (!PerformanceStatistics.instances.has(objId)) { PerformanceStatistics.instances.set(objId, new PerformanceStatistics(objId, objName, uri)) } return PerformanceStatistics.instances.get(objId) } + public static deleteInstance (objId: string | undefined): boolean { + if (objId == null) { + const errMsg = 'Cannot delete performance statistics instance without specifying object id' + logger.error(`${PerformanceStatistics.logPrefix()} ${errMsg}`) + throw new BaseError(errMsg) + } + return PerformanceStatistics.instances.delete(objId) + } + public static beginMeasure (id: string): string { const markId = `${id.charAt(0).toUpperCase()}${id.slice(1)}~${generateUUID()}` performance.mark(markId) @@ -81,7 +107,7 @@ export class PerformanceStatistics { try { performance.measure(name, markId) } catch (error) { - if (error instanceof Error && error.message.includes('performance mark has not been set')) { + if (is(Error, error) && error.message.includes('performance mark has not been set')) { /* Ignore */ } else { throw error @@ -153,9 +179,9 @@ export class PerformanceStatistics { ) if (performanceStorageConfiguration.enabled === true) { logger.info( - `${this.logPrefix()} storage enabled: type ${performanceStorageConfiguration.type}, uri: ${ - performanceStorageConfiguration.uri - }` + `${this.logPrefix()} storage enabled: type ${ + performanceStorageConfiguration.type + }, uri: ${performanceStorageConfiguration.uri}` ) } } @@ -173,7 +199,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`, @@ -187,7 +213,9 @@ export class PerformanceStatistics { private logStatistics (): void { logger.info(this.logPrefix(), { ...this.statistics, - statisticsData: JSONStringifyWithMapSupport(this.statistics.statisticsData) + statisticsData: JSON.parse( + JSONStringify(this.statistics.statisticsData, undefined, MapStringifyFormat.object) + ) as Map }) } @@ -239,33 +267,40 @@ export class PerformanceStatistics { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.statistics.statisticsData.get(entry.name)!.minTimeMeasurement = min( entry.duration, - this.statistics.statisticsData.get(entry.name)?.minTimeMeasurement ?? Infinity + this.statistics.statisticsData.get(entry.name)?.minTimeMeasurement ?? Number.POSITIVE_INFINITY ) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.statistics.statisticsData.get(entry.name)!.maxTimeMeasurement = max( entry.duration, - this.statistics.statisticsData.get(entry.name)?.maxTimeMeasurement ?? -Infinity + this.statistics.statisticsData.get(entry.name)?.maxTimeMeasurement ?? Number.NEGATIVE_INFINITY ) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.statistics.statisticsData.get(entry.name)!.totalTimeMeasurement = (this.statistics.statisticsData.get(entry.name)?.totalTimeMeasurement ?? 0) + entry.duration - this.statistics.statisticsData.get(entry.name)?.measurementTimeSeries instanceof CircularArray - ? this.statistics.statisticsData - .get(entry.name) - ?.measurementTimeSeries?.push({ timestamp: entry.startTime, value: entry.duration }) - : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - (this.statistics.statisticsData.get(entry.name)!.measurementTimeSeries = - new CircularArray(Constants.DEFAULT_CIRCULAR_BUFFER_CAPACITY, { - timestamp: entry.startTime, - value: entry.duration - })) + if ( + !( + this.statistics.statisticsData.get(entry.name)?.measurementTimeSeries instanceof + CircularBuffer + ) + ) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.statistics.statisticsData.get(entry.name)!.measurementTimeSeries = + new CircularBuffer( + Array, + Constants.DEFAULT_CIRCULAR_BUFFER_CAPACITY + ) + } + this.statistics.statisticsData.get(entry.name)?.measurementTimeSeries?.push({ + timestamp: entry.startTime, + value: entry.duration + }) const timeMeasurementValues = extractTimeSeriesValues( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.statistics.statisticsData.get(entry.name)!.measurementTimeSeries! + this.statistics.statisticsData.get(entry.name)! + .measurementTimeSeries as CircularBuffer ) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.statistics.statisticsData.get(entry.name)!.avgTimeMeasurement = - average(timeMeasurementValues) + this.statistics.statisticsData.get(entry.name)!.avgTimeMeasurement = mean(timeMeasurementValues) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.statistics.statisticsData.get(entry.name)!.medTimeMeasurement = median(timeMeasurementValues) @@ -288,6 +323,10 @@ export class PerformanceStatistics { } } + private static readonly logPrefix = (): string => { + return logPrefix(' Performance statistics') + } + private readonly logPrefix = (): string => { return logPrefix(` ${this.objName} | Performance statistics`) }