import { parentPort } from 'node:worker_threads'
import { secondsToMilliseconds } from 'date-fns'
+import { mean, median } from 'rambda'
import { BaseError } from '../exception/index.js'
import {
type TimestampedData
} from '../types/index.js'
import {
- average,
buildPerformanceStatisticsMessage,
CircularArray,
Configuration,
logger,
logPrefix,
max,
- median,
min,
nthPercentile,
stdDeviation
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)
-/**
- * Computes the average of the given data set.
- *
- * @param dataSet - Data set.
- * @returns The average of the given data set.
- * @internal
- */
-export const average = (dataSet: number[]): number => {
- if (Array.isArray(dataSet) && dataSet.length === 0) {
- return 0
- } else if (Array.isArray(dataSet) && dataSet.length === 1) {
- return dataSet[0]
- }
- return dataSet.reduce((accumulator, nb) => accumulator + nb, 0) / dataSet.length
-}
+import { mean } from 'rambda'
-/**
- * Computes the median of the given data set.
- *
- * @param dataSet - Data set.
- * @returns The median of the given data set.
- * @internal
- */
-export const median = (dataSet: number[]): number => {
- if (Array.isArray(dataSet) && dataSet.length === 0) {
- return 0
- } else if (Array.isArray(dataSet) && dataSet.length === 1) {
- return dataSet[0]
- }
- const sortedDataSet = dataSet.slice().sort((a, b) => a - b)
- return (
- (sortedDataSet[(sortedDataSet.length - 1) >> 1] + sortedDataSet[sortedDataSet.length >> 1]) / 2
- )
-}
+export const min = (...args: number[]): number =>
+ args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity)
+
+export const max = (...args: number[]): number =>
+ args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity)
// TODO: use order statistics tree https://en.wikipedia.org/wiki/Order_statistic_tree
export const nthPercentile = (dataSet: number[], percentile: number): number => {
* @see https://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation
* @internal
*/
-export const stdDeviation = (
- dataSet: number[],
- dataSetAverage: number = average(dataSet)
-): number => {
+export const stdDeviation = (dataSet: number[], dataSetAverage: number = mean(dataSet)): number => {
if (Array.isArray(dataSet) && (dataSet.length === 0 || dataSet.length === 1)) {
return 0
}
return true
}
-export const min = (...args: number[]): number =>
- args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity)
-
-export const max = (...args: number[]): number =>
- args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity)
-
export const throwErrorInNextTick = (error: Error): void => {
nextTick(() => {
throw error
buildStoppedMessage,
buildUpdatedMessage
} from './MessageChannelUtils.js'
-export { average, median, nthPercentile, stdDeviation } from './StatisticUtils.js'
+export { max, min, nthPercentile, stdDeviation } from './StatisticUtils.js'
export {
clone,
convertToBoolean,
isValidDate,
JSONStringify,
logPrefix,
- max,
- min,
roundTo,
secureRandom,
sleep,
import { expect } from 'expect'
-import { average, median, nthPercentile, stdDeviation } from '../../src/utils/StatisticUtils.js'
+import { max, min, nthPercentile, stdDeviation } from '../../src/utils/StatisticUtils.js'
await describe('StatisticUtils test suite', async () => {
- await it('Verify average()', () => {
- expect(average([])).toBe(0)
- expect(average([0.08])).toBe(0.08)
- expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.1642857142857146)
- expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.8533333333333335)
+ await it('Verify min()', () => {
+ expect(min()).toBe(Infinity)
+ expect(min(0, 1)).toBe(0)
+ expect(min(1, 0)).toBe(0)
+ expect(min(0, -1)).toBe(-1)
+ expect(min(-1, 0)).toBe(-1)
})
- await it('Verify median()', () => {
- expect(median([])).toBe(0)
- expect(median([0.08])).toBe(0.08)
- expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05)
- expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535)
+ await it('Verify max()', () => {
+ expect(max()).toBe(-Infinity)
+ expect(max(0, 1)).toBe(1)
+ expect(max(1, 0)).toBe(1)
+ expect(max(0, -1)).toBe(0)
+ expect(max(-1, 0)).toBe(0)
})
await it('Verify nthPercentile()', () => {
isNotEmptyString,
isObject,
isValidDate,
- max,
- min,
roundTo,
secureRandom,
sleep,
expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false)
expect(isArraySorted<number>([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false)
})
-
- await it('Verify min()', () => {
- expect(min()).toBe(Infinity)
- expect(min(0, 1)).toBe(0)
- expect(min(1, 0)).toBe(0)
- expect(min(0, -1)).toBe(-1)
- expect(min(-1, 0)).toBe(-1)
- })
-
- await it('Verify max()', () => {
- expect(max()).toBe(-Infinity)
- expect(max(0, 1)).toBe(1)
- expect(max(1, 0)).toBe(1)
- expect(max(0, -1)).toBe(0)
- expect(max(-1, 0)).toBe(0)
- })
})