Skip to content

Commit

Permalink
Confidence interval around the data.
Browse files Browse the repository at this point in the history
  • Loading branch information
shimkiv committed Mar 14, 2024
1 parent 546e112 commit 6465c4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
16 changes: 15 additions & 1 deletion benchmark/base-benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
*/

import jStat from 'jstat';
export { Benchmark, BenchmarkResult, benchmark, logResult, pValue };
export {
Benchmark,
BenchmarkResult,
benchmark,
calculateBounds,
logResult,
pValue,
};

type BenchmarkResult = {
label: string;
Expand Down Expand Up @@ -158,3 +165,10 @@ function pValue(sample1: BenchmarkResult, sample2: BenchmarkResult): number {
const pValue = 2 * (1 - jStat.studentt.cdf(Math.abs(tStatistic), df));
return pValue;
}

function calculateBounds(result: BenchmarkResult) {
const percentage = (Math.sqrt(result.variance) / result.mean) * 100;
const upperBound = result.mean + (result.mean * percentage) / 100;
const lowerBound = result.mean - (result.mean * percentage) / 100;
return { upperBound, lowerBound };
}
6 changes: 5 additions & 1 deletion benchmark/utils/influxdb-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { InfluxDB, Point } from '@influxdata/influxdb-client';
import os from 'node:os';
import { BenchmarkResult } from '../base-benchmark.js';
import { BenchmarkResult, calculateBounds } from '../base-benchmark.js';

const INFLUXDB_CLIENT_OPTIONS = {
url: process.env.INFLUXDB_URL,
Expand Down Expand Up @@ -36,11 +36,15 @@ export function writeResultToInfluxDb(result: BenchmarkResult): void {
console.log('Writing result to InfluxDB.');
const influxDbWriteClient = influxDbClient.getWriteApi(org, bucket, 'ms');
try {
const sampleName = result.label.split('-')[1].trim();
const { upperBound, lowerBound } = calculateBounds(result);
const point = new Point(`${result.label} - ${result.size} samples`)
.tag('benchmarkName', result.label.trim())
.tag('sampledTimes', result.size.toString())
.floatField('mean', result.mean)
.floatField('variance', result.variance)
.floatField(`${sampleName} - upperBound`, upperBound)
.floatField(`${sampleName} - lowerBound`, lowerBound)
.intField('size', result.size);
for (const [key, value] of Object.entries(INFLUXDB_COMMON_POINT_TAGS)) {
point.tag(key, value.trim());
Expand Down

0 comments on commit 6465c4e

Please sign in to comment.