signal-desktop/ts/util/benchmark/stats.ts
trevor-signal 82e058f2b8
Conversation open speed benchmarking for staging builds
Co-authored-by: Fedor Indutnyy <indutny@signal.org>
2023-07-21 00:37:56 +02:00

40 lines
809 B
TypeScript

// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
export type StatsType = {
mean: number;
stddev: number;
[key: string]: number;
};
export function stats(
list: ReadonlyArray<number>,
percentiles: ReadonlyArray<number> = []
): StatsType {
if (list.length === 0) {
throw new Error('Empty list given to stats');
}
let mean = 0;
let stddev = 0;
for (const value of list) {
mean += value;
stddev += value ** 2;
}
mean /= list.length;
stddev /= list.length;
stddev -= mean ** 2;
stddev = Math.sqrt(stddev);
const sorted = list.slice().sort((a, b) => a - b);
const result: StatsType = { mean, stddev };
for (const p of percentiles) {
result[`p${p}`] = sorted[Math.floor((sorted.length * p) / 100)];
}
return result;
}