Skip to content

Commit

Permalink
fix: minor type corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Ferreiro Val committed Jun 19, 2019
1 parent baf1f42 commit 523e443
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
30 changes: 15 additions & 15 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 } from "@best/types";
import { BenchmarkResultsSnapshot, BenchmarkResultNode, BenchmarkMetricNames, BenchmarkStats, AllBenchmarksMetricsMap, BenchmarkMetricsAggregate, AllBenchmarkMetricStatsMap, StatsNode, BenchmarkMetricStatsMap, StatsNodeGroup } from "@best/types";
import { quantile, mean, median, variance, medianAbsoluteDeviation, compare as compareSamples } from './stats';

function computeSampleStats(arr: number[], samplesQuantileThreshold: number): BenchmarkStats {
Expand All @@ -20,17 +20,19 @@ function computeSampleStats(arr: number[], samplesQuantileThreshold: number): Be
}

// Given an iteration benchmark (whith nested benchmarks), collect its metrics
function collectResults({ name, metrics, nodes, aggregate }: BenchmarkResultNode, collector: AllBenchmarksMetricsMap) {
function collectResults(resultNode: BenchmarkResultNode, collector: AllBenchmarksMetricsMap) {
const { name } = resultNode;
let collectorNode = collector[name];
if (!collectorNode) {
collectorNode = collector[name] = { script: [], aggregate: [] };
}

if (aggregate > 0 && collectorNode.aggregate) {
collectorNode.aggregate.push(aggregate);
if (resultNode.aggregate > 0 && collectorNode.aggregate) {
collectorNode.aggregate.push(resultNode.aggregate);
}

if (metrics) {
if (resultNode.type ==="benchmark") {
const { metrics } = resultNode;
Object.keys(metrics).reduce((collector: BenchmarkMetricsAggregate, key: string) => {
const bucket = collector[key as BenchmarkMetricNames];
const value = metrics[key as BenchmarkMetricNames];
Expand All @@ -40,24 +42,24 @@ function collectResults({ name, metrics, nodes, aggregate }: BenchmarkResultNode

return collector;
}, collectorNode);
}

if (nodes) {
nodes.forEach((node: BenchmarkResultNode) => collectResults(node, collector));
} else {
resultNode.nodes.forEach((node: BenchmarkResultNode) => collectResults(node, collector));
}

return collector;
}

function createStatsStructure({ nodes: children = [], name, type }: BenchmarkResultNode, collector: AllBenchmarkMetricStatsMap): StatsNode {
if (type === "benchmark") {
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] };
return metricReducer;
}, {});
return { type, name, metrics };
} else {
const { name, type, nodes: children } = node;
const nodes = children.map((childNode: BenchmarkResultNode) => createStatsStructure(childNode, collector))
return { type, name, nodes };
}
Expand Down Expand Up @@ -88,15 +90,13 @@ export async function analyzeBenchmarks(benchmarkResults: BenchmarkResultsSnapsh
return stats;
}, {});

const benchmarkStructure = createStatsStructure(structure, benchmarkStats);

const benchmarkStructure = createStatsStructure(structure, benchmarkStats) as StatsNodeGroup;
console.log(JSON.stringify(benchmarkStructure, null, ' '));

benchmarkResult.stats = {
version: VERSION,
benchmarkName,
benchmarks: benchmarkStructure.nodes,
environment,
results: benchmarkStructure.nodes
};
}),
);
Expand Down
10 changes: 5 additions & 5 deletions packages/@best/runtime/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { getBenchmarkRootNode } from './state';
import { runBenchmarkIteration } from './run_iteration';
import { normalizeResults } from './results';
import { validateState } from "./utils/validate";
import { BenchmarkResultNode, ResultNodeTypes, BenchmarkResults } from "@best/types";
import { BenchmarkResultNode, ResultNodeTypes, BenchmarkResults, BenchmarkResultBenchmarkNode, BenchmarkResultGroupNode } from "@best/types";

function collectNodeResults(node: RuntimeNode): BenchmarkResultNode {
const { name, aggregate, startedAt, run, children } = node;
const type = node.type as ResultNodeTypes;
const resultNode: BenchmarkResultNode = { type, name, aggregate, startedAt };
const resultNode = { type, name, aggregate, startedAt };

if (run) {
resultNode.aggregate = run.aggregate;
resultNode.metrics = { script: run.metrics.script };
(resultNode as BenchmarkResultBenchmarkNode).metrics = { script: run.metrics.script };
} else if (children) {
resultNode.nodes = children.map((c: RuntimeNode) => collectNodeResults(c));
(resultNode as BenchmarkResultGroupNode).nodes = children.map((c: RuntimeNode) => collectNodeResults(c));
}

return resultNode;
return (resultNode as BenchmarkResultNode);
}

async function runIterations(config: BenchmarkState): Promise<BenchmarkState> {
Expand Down
21 changes: 5 additions & 16 deletions packages/@best/types/src/benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { EnvironmentConfig, FrozenProjectConfig } from "./config";

export interface BenchmarkInfo {
benchmarkName: string;
benchmarkEntry: string;
Expand All @@ -22,41 +20,32 @@ export type BenchmarkMetrics = {
}

export type ResultNodeTypes = "group" | "benchmark";
export interface BenchmarkResultNode {
export interface BenchmarkResultNodeBase {
type: ResultNodeTypes;
name: string;
aggregate: number;
startedAt: number;
metrics?: BenchmarkMetrics;
nodes? : BenchmarkResultNode[];
}

export interface BenchmarkResultGroupNode extends BenchmarkResultNode {
export interface BenchmarkResultGroupNode extends BenchmarkResultNodeBase {
type: "group";
nodes : BenchmarkResultNode[];
}

export interface BenchmarkResultBenchmarkNode extends BenchmarkResultNode {
export interface BenchmarkResultBenchmarkNode extends BenchmarkResultNodeBase {
type: "benchmark";
metrics: BenchmarkMetrics;
}

export type BenchmarkResultNode = BenchmarkResultGroupNode | BenchmarkResultBenchmarkNode;

export interface BenchmarkResults {
benchmarkName: string;
executedIterations: number;
aggregate: number;
results: BenchmarkResultNode[];
}


export interface BenchmarkResultsSnapshot {
results: BenchmarkResultNode[];
environment: EnvironmentConfig;
benchmarkInfo: BenchmarkInfo;
projectConfig: FrozenProjectConfig;
stats?: any;
}

export interface BenchmarkResultsState {
executedTime: number,
executedIterations: number,
Expand Down
37 changes: 32 additions & 5 deletions packages/@best/types/src/stats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { BenchmarkMetricNames } from "./benchmark";
import { BenchmarkMetricNames, BenchmarkResultNode, BenchmarkInfo } from "./benchmark";
import { EnvironmentConfig, FrozenProjectConfig } from "./config";

export interface BenchmarkResultsSnapshot {
results: BenchmarkResultNode[];
environment: EnvironmentConfig;
benchmarkInfo: BenchmarkInfo;
projectConfig: FrozenProjectConfig;
stats?: StatsResults;
}

export type BenchmarkStatsNames = "samples" | "sampleSize" | "samplesQuantileThreshold" | "mean" | "median" | "variance" | "medianAbsoluteDeviation";
export interface BenchmarkStats {
Expand Down Expand Up @@ -26,9 +35,27 @@ export type MetricsStatsMap = {
}
}

export interface StatsNode {
type: "group" | "benchmark";
type StatsNodeTypes = "group" | "benchmark";
export interface StatsNodeBase {
type: StatsNodeTypes;
name: string;
nodes?: StatsNode[];
metrics?: MetricsStatsMap
}

export interface StatsNodeBenchmark extends StatsNodeBase {
type: "benchmark";
metrics: MetricsStatsMap
}

export interface StatsNodeGroup extends StatsNodeBase {
type: "group";
nodes: StatsNode[];
}

export type StatsNode = StatsNodeGroup | StatsNodeBenchmark;


export interface StatsResults {
version: number;
benchmarkName: string;
results: StatsNode[];
}

0 comments on commit 523e443

Please sign in to comment.