Skip to content

Commit

Permalink
fix: stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Ferreiro Val committed Jun 20, 2019
1 parent 335e489 commit ea9adc0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 55 deletions.
11 changes: 7 additions & 4 deletions packages/@best/analyzer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VERSION } from './constants';
import { BenchmarkResultsSnapshot, BenchmarkResultNode, BenchmarkMetricNames, BenchmarkStats, AllBenchmarksMetricsMap, BenchmarkMetricsAggregate, AllBenchmarkMetricStatsMap, StatsNode, BenchmarkMetricStatsMap, StatsNodeGroup } from "@best/types";
import { BenchmarkResultsSnapshot, BenchmarkResultNode, BenchmarkMetricNames, BenchmarkStats, AllBenchmarksMetricsMap, BenchmarkMetricsAggregate, AllBenchmarkMetricStatsMap, StatsNode, BenchmarkMetricStatsMap, StatsNodeGroup, MetricsStatsMap } from "@best/types";
import { quantile, mean, median, variance, medianAbsoluteDeviation, compare as compareSamples } from './stats';

function computeSampleStats(arr: number[], samplesQuantileThreshold: number): BenchmarkStats {
Expand Down Expand Up @@ -52,9 +52,12 @@ function collectResults(resultNode: BenchmarkResultNode, collector: AllBenchmark
function createStatsStructure(node: BenchmarkResultNode, collector: AllBenchmarkMetricStatsMap): StatsNode {
if (node.type === "benchmark") {
const { name, type } = node;
const stats = collector[name];
const metrics = Object.keys(stats).reduce((metricReducer: any, metric: string) => {
metricReducer[metric as BenchmarkMetricNames] = { stats: stats[metric as BenchmarkMetricNames] };
const metricStats = collector[name];
const metrics = Object.keys(metricStats).reduce((metricReducer: MetricsStatsMap, metric: string) => {
const stats = metricStats[metric as BenchmarkMetricNames];
if (stats) {
metricReducer[metric as BenchmarkMetricNames] = { stats };
}
return metricReducer;
}, {});
return { type, name, metrics };
Expand Down
2 changes: 1 addition & 1 deletion packages/@best/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function runCLI(argsCLI: CliConfig, projects: string[]) {
return process.exit(0);
}

const output = new Output(globalConfig, outputStream);
const output = new Output({}, outputStream);
if (argsCLI.compareStats) {
results = await runCompare(globalConfig, configs, outputStream);
if (results) {
Expand Down
64 changes: 17 additions & 47 deletions packages/@best/cli/src/cli/output.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import Table from 'cli-table';
import chalk from 'chalk';
import Histogram from './histogram';
import { computeSampleStats } from '@best/analyzer';

import {
BenchmarkMetricNames,
BenchmarkResultsSnapshot,
EnvironmentConfig, GlobalConfig, StatsNode,
EnvironmentConfig, StatsNode,
StatsResults
} from "@best/types";
import { OutputStream } from '@best/console-stream';

interface OutputConfig {
outputHistograms?: boolean;
}

/*
* The Output module can write a report or a comparison to a given stream based on a configuration.
*/
export default class Output {
config: any;
stream: any;
constructor(config: GlobalConfig, stream: any) {
config: OutputConfig;
stream: OutputStream;
constructor(config: OutputConfig, stream: OutputStream) {
this.config = config || {};
this.stream = stream;
}
Expand All @@ -26,52 +31,20 @@ export default class Output {
report(results: BenchmarkResultsSnapshot[]) {
results.forEach((result: BenchmarkResultsSnapshot) => {
const { benchmarkInfo: { benchmarkName, benchmarkFolder }, stats } = result;
// Optionally calculate totals for each metric.
if (this.config.outputTotals) {
this.generateTotal(stats);
}

// Stats table.
this.writeStats(benchmarkName, benchmarkFolder, stats!);

// OS & Browser.
this.writeEnvironment(result.environment);

// Optional histogram for each line in the stats table.
if (this.config.outputHistograms) {
this.writeHistograms(stats!);
}
});
}

/*
* Add a new entry under `stats.benchmarks`, containing totals across all of the other entries.
*/
generateTotal(stats: any) {
const total: any = {};
const metricPattern = new RegExp(`^(.*)$`); // this.config.outputMetricPattern

function add(node: any) {
const children = node.benchmarks;
if (children) {
children.forEach((child: any) => { add(child); });
} else {
Object.keys(node).forEach((metric: any) => {
if (metricPattern.test(metric) && metric !== 'name') {
const samples = total[metric] = total[metric] || [];
node[metric].samples.forEach((v: any, i: any) => {
samples[i] = (samples[i] || 0) + v;
});
}
});
}
}

add(stats);
Object.keys(total).forEach(metric => {
total[metric] = computeSampleStats(total[metric], this.config);
});
total.name = 'total';
stats.benchmarks.push(total);
}

/*
* Write a table of statistics for a single benchmark file.
*/
Expand All @@ -96,7 +69,7 @@ export default class Output {
* Write browser and CPU load information.
*/
writeEnvironment({ browser, container }: EnvironmentConfig) {
const cpuLoad = container.load.cpuLoad; //.cpu.cpuLoad;
const cpuLoad = container.load.cpuLoad;
const loadColor = cpuLoad < 10 ? 'green' : cpuLoad < 50 ? 'yellow' : 'red';

this.stream.write(' ');
Expand Down Expand Up @@ -151,11 +124,8 @@ export default class Output {
// Root benchmark
if (benchmarkNode.type === "benchmark") {
Object.keys(benchmarkNode.metrics).forEach((metric: string) => {
// if (!metricPattern.test(metric)) {
// return;
// }

const metricValues = benchmarkNode.metrics[metric as BenchmarkMetricNames].stats;
const metricsStats = benchmarkNode.metrics[metric as BenchmarkMetricNames];
const metricValues = metricsStats && metricsStats.stats;
if (metricValues && metricValues.sampleSize) {
const { sampleSize, mean, median, variance, medianAbsoluteDeviation } = metricValues;
table.push([
Expand Down Expand Up @@ -242,7 +212,7 @@ export default class Output {
}
}

function padding(n: any) {
function padding(n: number) {
return n > 0
? Array.apply(null, Array((n - 1) * 3))
.map(() => ' ')
Expand Down
10 changes: 7 additions & 3 deletions packages/@best/types/src/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ export interface BenchmarkStats {
medianAbsoluteDeviation: number,
}

export type BenchmarkMetricsAggregate = { [key in BenchmarkMetricNames]?: number[] }
export type BenchmarkMetricStatsMap = { [key in BenchmarkMetricNames]?: BenchmarkStats; }
export type BenchmarkMetricsAggregate = {
[key in BenchmarkMetricNames]?: number[]
}
export type BenchmarkMetricStatsMap = {
[key in BenchmarkMetricNames]?: BenchmarkStats;
}

// This type will hold as keys all benchmark names, and then an array with all results
export interface AllBenchmarksMetricsMap { [key: string]: BenchmarkMetricsAggregate }
export interface AllBenchmarkMetricStatsMap { [key: string]: BenchmarkMetricStatsMap; }

export type MetricsStatsMap = {
[key in BenchmarkMetricNames]: {
[key in BenchmarkMetricNames]?: {
stats: BenchmarkStats
}
}
Expand Down

0 comments on commit ea9adc0

Please sign in to comment.