Skip to content

Commit

Permalink
fix: types (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
jodarove authored and Diego Ferreiro Val committed Jun 20, 2019
1 parent cdfe267 commit 5801a33
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 76 deletions.
69 changes: 38 additions & 31 deletions packages/@best/cli/src/cli/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,41 @@ import Table from 'cli-table';
import chalk from 'chalk';
import Histogram from './histogram';
import { computeSampleStats } from '@best/analyzer';
import {
BenchmarkMetricNames,
BenchmarkResultsSnapshot,
EnvironmentConfig, GlobalConfig, StatsNode,
StatsResults
} from "@best/types";

/*
* 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: any, stream: any) {
constructor(config: GlobalConfig, stream: any) {
this.config = config || {};
this.stream = stream;
}

/*
* Show a report for a given set of results.
*/
report(results: any) {
results.forEach((result: any) => {
const { benchmarkName, benchmarkOutputResult, stats } = result;
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, benchmarkOutputResult, stats.benchmarks);
this.writeStats(benchmarkName, benchmarkFolder, stats!);
// OS & Browser.
this.writeEnvironment(stats.environment);
this.writeEnvironment(result.environment);
// Optional histogram for each line in the stats table.
if (this.config.outputHistograms) {
this.writeHistograms(stats.benchmarks);
this.writeHistograms(stats!);
}
});
}
Expand All @@ -40,7 +46,7 @@ export default class Output {
*/
generateTotal(stats: any) {
const total: any = {};
const metricPattern = this.config.outputMetricPattern;
const metricPattern = new RegExp(`^(.*)$`); // this.config.outputMetricPattern

function add(node: any) {
const children = node.benchmarks;
Expand Down Expand Up @@ -69,13 +75,13 @@ export default class Output {
/*
* Write a table of statistics for a single benchmark file.
*/
writeStats(benchmarkName: string, resultsFolder: string, stats: any) {
writeStats(benchmarkName: string, resultsFolder: string, stats: StatsResults) {
const table = new Table({
head: ['Benchmark name', 'Metric (ms)', 'N', 'Mean ± StdDev', 'Median ± MAD'],
style: { head: ['bgBlue', 'white'] },
});

this.generateRows(table, stats);
this.generateRows(table, stats.results);

this.stream.write(
[
Expand All @@ -89,8 +95,8 @@ export default class Output {
/*
* Write browser and CPU load information.
*/
writeEnvironment({ browser, runtime }: any) {
const cpuLoad = runtime.load.cpuLoad;
writeEnvironment({ browser, container }: EnvironmentConfig) {
const cpuLoad = container.load.cpuLoad; //.cpu.cpuLoad;
const loadColor = cpuLoad < 10 ? 'green' : cpuLoad < 50 ? 'yellow' : 'red';

this.stream.write(' ');
Expand All @@ -108,21 +114,21 @@ export default class Output {
* Write a set of histograms for a tree of benchmarks.
*/
writeHistograms(benchmarks: any, parentPath: string = '') {
const metricPattern = this.config.outputMetricPattern;
const histogramPattern = this.config.outputHistogramPattern;
// const metricPattern = this.config.outputMetricPattern;
// const histogramPattern = this.config.outputHistogramPattern;
benchmarks.forEach((benchmark: any) => {
const path = `${parentPath}${benchmark.name}`;
const children = benchmark.benchmarks;
if (children) {
this.writeHistograms(children, `${path} > `);
} else {
if (!histogramPattern.test(path)) {
return;
}
// if (!histogramPattern.test(path)) {
// return;
// }
Object.keys(benchmark).forEach(metric => {
if (!metricPattern.test(metric)) {
return;
}
// if (!metricPattern.test(metric)) {
// return;
// }
const stats = benchmark[metric];
if (stats && stats.sampleSize) {
const { samples } = stats;
Expand All @@ -138,17 +144,18 @@ export default class Output {
/*
* Recursively populate rows of statistics into a table for a tree of benchmarks.
*/
generateRows(table: any, benchmarks: any, level = 0) {
const metricPattern = this.config.outputMetricPattern;
benchmarks.forEach((benchmarkNode: any) => {
generateRows(table: any, benchmarks: StatsNode[], level = 0) {
// const metricPattern = //this.config.outputMetricPattern;
benchmarks.forEach((benchmarkNode: StatsNode) => {
const name = benchmarkNode.name;
// Root benchmark
if (!benchmarkNode.benchmarks) {
Object.keys(benchmarkNode).forEach(metric => {
if (!metricPattern.test(metric)) {
return;
}
const metricValues = benchmarkNode[metric];
if (benchmarkNode.type === "benchmark") {
Object.keys(benchmarkNode.metrics).forEach((metric: string) => {
// if (!metricPattern.test(metric)) {
// return;
// }

const metricValues = benchmarkNode.metrics[metric as BenchmarkMetricNames].stats;
if (metricValues && metricValues.sampleSize) {
const { sampleSize, mean, median, variance, medianAbsoluteDeviation } = metricValues;
table.push([
Expand All @@ -161,10 +168,10 @@ export default class Output {
}
});
// Group
} else {
} else if (benchmarkNode.type === "group") {
const emptyFields = Array.apply(null, Array(4)).map(() => '-');
table.push([padding(level) + name, ...emptyFields]);
this.generateRows(table, benchmarkNode.benchmarks, level + 1);
this.generateRows(table, benchmarkNode.nodes, level + 1);
}
});
}
Expand Down
45 changes: 12 additions & 33 deletions packages/@best/store/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
import mkdirp from 'mkdirp';
import rimraf from 'rimraf';

import path from 'path';
import { ncp } from 'ncp';
import { formatEnvironment } from './md-formatter';
import { stringify } from './pretty-json';
import fs from 'fs';
import chalk from 'chalk';

function copyArtifacts(benchmarkFolder: any, outputFolder: any) {
return new Promise((resolve, reject) => {
ncp(benchmarkFolder, outputFolder, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
import { BenchmarkResultsSnapshot, FrozenGlobalConfig } from "@best/types";

function formatJSON(json: any) {
return stringify(json, { indent: "2", maxLength: 90 });
}

function getStoredFileMapping(benchmarkFolder:any, artifactsFolder:any) {
function getStoredFileMapping(benchmarkFolder: string, artifactsFolder: string) {
const WHITELIST = ['.js', '.html', '.css', '.json'];

const currentFiles = fs.readdirSync(benchmarkFolder);
Expand All @@ -36,33 +23,25 @@ function getStoredFileMapping(benchmarkFolder:any, artifactsFolder:any) {
}, {});
}

export function storeBenchmarkResults(benchmarkResults: any, globalConfig: any) {
export function storeBenchmarkResults(benchmarkResults: BenchmarkResultsSnapshot[], globalConfig: FrozenGlobalConfig) {
return Promise.all(
benchmarkResults.map(async (benchmarkResult: any) => {
const { benchmarkName, benchmarkSignature, projectConfig, environment, results, stats } = benchmarkResult;
const { benchmarkOutput, cacheDirectory, projectName } = projectConfig;
const { externalStorage, gitCommit, gitLocalChanges } = globalConfig;
const hash = gitLocalChanges ? 'local_' + benchmarkSignature.substr(0, 6) : gitCommit;
const outputFolder = path.join(benchmarkOutput, projectName, `${benchmarkName}_${hash}`);
const artifactsFolder = path.join(outputFolder, 'artifacts');
const benchmarkFolder = path.join(cacheDirectory, projectName, benchmarkName);
benchmarkResults.map(async (benchmarkResult: BenchmarkResultsSnapshot) => {
const { benchmarkInfo: { benchmarkFolder }, environment, results, stats } = benchmarkResult;
const { externalStorage } = globalConfig;

mkdirp.sync(outputFolder);
rimraf.sync(artifactsFolder);
await copyArtifacts(benchmarkFolder, artifactsFolder);
const artifactsFolder = path.join(benchmarkFolder, 'artifacts');

// Environment
fs.writeFileSync(path.join(outputFolder, 'environment.md'), formatEnvironment(environment), 'utf8');
fs.writeFileSync(path.join(benchmarkFolder, 'environment.md'), formatEnvironment(environment), 'utf8');

// Results
fs.writeFileSync(path.join(outputFolder, 'stats.json'), formatJSON(stats), 'utf8');
fs.writeFileSync(path.join(outputFolder, 'raw_results.json'), formatJSON(results), 'utf8');
benchmarkResult.benchmarkOutputResult = outputFolder;
fs.writeFileSync(path.join(benchmarkFolder, 'stats.json'), formatJSON(stats), 'utf8');
fs.writeFileSync(path.join(benchmarkFolder, 'raw_results.json'), formatJSON(results), 'utf8');

if (externalStorage) {
try {
const storageModule = require(externalStorage);
const fileMap = getStoredFileMapping(outputFolder, artifactsFolder);
const fileMap = getStoredFileMapping(benchmarkFolder, artifactsFolder);
await storageModule.storeBenchmarkResults(fileMap, benchmarkResult, globalConfig);
} catch (err) {
const ERR_TEXT = chalk.reset.inverse.red.bold(' ERROR ') + ' ';
Expand Down
5 changes: 4 additions & 1 deletion packages/@best/store/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"compilerOptions": {
"rootDir": "src",
"outDir": "build",
}
},
"references": [
{ "path": "../types" }
]
}
24 changes: 17 additions & 7 deletions packages/@best/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export interface CliConfig {
help: boolean,
clearCache: boolean,
clearResults: boolean,
gitIntegration: string | undefined,
gitIntegration?: string,
useHttp: boolean,
externalStorage: string | undefined,
externalStorage?: string,
runner: string,
runnerConfig: { [x:string]: any },
config: string | undefined,
Expand All @@ -43,7 +43,6 @@ export interface CliConfig {
compareStats: string[] | undefined
}


export interface NormalizedConfig {
nonFlagArgs: string[];
cache: boolean,
Expand Down Expand Up @@ -86,6 +85,7 @@ export interface GlobalConfig {
isInteractive?: boolean;
gitInfo: GitConfig;
apiDatabase?: ApiDatabaseConfig;
externalStorage?: string;
}

export type ProjectConfigPlugin = string | [string, { [key : string]: any }]
Expand Down Expand Up @@ -136,12 +136,22 @@ export interface BrowserConfig {

export interface EnvironmentConfig {
hardware: {
system: any;
cpu: any;
os: any;
system: {
manufacturer: string,
model: string,
},
cpu: {
manufacturer: string;
brand: string;
family: string;
model: string;
speed: string;
cores: number;
},
os: { platform: string, distro: string, release: string, kernel: string, arch: string };
},
container: {
load: any
load: { cpuLoad: number }
},
browser: BrowserConfig;
configuration: {
Expand Down
6 changes: 2 additions & 4 deletions packages/@best/types/src/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ export interface AllBenchmarksMetricsMap { [key: string]: BenchmarkMetricsAggreg
export interface AllBenchmarkMetricStatsMap { [key: string]: BenchmarkMetricStatsMap; }

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

Expand Down

0 comments on commit 5801a33

Please sign in to comment.