X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=inline;f=src%2Fperformance%2FPerformanceStatistics.ts;h=6363be1c6b2196842b49dc1140ca9887d5b4b385;hb=c17a8d29b2177a087f7fea1db4ada2b3795a7a31;hp=2f10960604a8429a5a059b62d2ed46d44c2bb241;hpb=68220b423c52da387fdf41967dd8c738da0ff52e;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 2f109606..6363be1c 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -1,36 +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 { 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 { + buildPerformanceStatisticsMessage, CircularArray, Configuration, Constants, - JSONStringifyWithMapSupport, - average, - buildPerformanceStatisticsMessage, extractTimeSeriesValues, formatDurationSeconds, generateUUID, - logPrefix, + JSONStringify, logger, + logPrefix, max, - median, min, nthPercentile, stdDeviation @@ -66,20 +67,19 @@ export class PerformanceStatistics { 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}`) + 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(`${logPfx} ${errMsg}`) + 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(`${logPfx} ${errMsg}`) + logger.error(`${PerformanceStatistics.logPrefix()} ${errMsg}`) throw new BaseError(errMsg) } if (!PerformanceStatistics.instances.has(objId)) { @@ -88,6 +88,15 @@ export class PerformanceStatistics { 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) @@ -190,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`, @@ -204,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 }) } @@ -281,8 +292,7 @@ export class PerformanceStatistics { this.statistics.statisticsData.get(entry.name)!.measurementTimeSeries! ) // 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) @@ -305,6 +315,10 @@ export class PerformanceStatistics { } } + private static readonly logPrefix = (): string => { + return logPrefix(' Performance statistics') + } + private readonly logPrefix = (): string => { return logPrefix(` ${this.objName} | Performance statistics`) }