Skip to content

Commit

Permalink
feat: Allow local (fs) comparison (#96)
Browse files Browse the repository at this point in the history
* feat: Add better errors for comparison

* fix: Fix compareStats

* chore: Minor changes in gitignore

* feat: Draft local comparison

* feat: First step

* fix: Allowing object rest spread syntax

* fix: Missing await

* fix: Missing anme

* fix: Add compareCommit

* feat: Add store fs abstraction

* chore: Adding missing files

* fix: Refactor compare code

* fix: Finish implementation

* fix: Trimming console

* fix: Quotes

* perf: Add default ignore in globby
  • Loading branch information
diervo authored Mar 19, 2018
1 parent e1bdbb7 commit 5781d55
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 422 deletions.
7 changes: 5 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"plugins": [
["transform-es2015-modules-commonjs", {
["@babel/plugin-transform-modules-commonjs", {
"allowTopLevelThis": true
}]
],
"retainLines": true
"retainLines": true,
"parserOpts": {
"plugins": ["objectRestSpread"]
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.DS_STORE

# Tools artifacts
playground.js
node_modules
build
coverage
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
},
"devDependencies": {
"@babel/core": "7.0.0-beta.40",
"@babel/plugin-transform-modules-commonjs": "^7.0.0-beta.40",
"@babel/plugin-transform-modules-commonjs": "7.0.0-beta.40",
"babel-core": "^7.0.0-0",
"babel-jest": "22.4.1",
"chalk": "^2.3.0",
"commitizen": "^2.9.6",
"conventional-changelog-cli": "^1.3.5",
Expand Down
7 changes: 3 additions & 4 deletions packages/best-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
"@best/messager": "0.3.2",
"@best/runner": "0.3.2",
"@best/store": "0.3.2",
"@best/store-fs": "0.3.2",
"chalk": "~2.3.0",
"cli-table": "~0.3.1",
"globby": "~7.1.1",
"micromatch": "~3.1.4",
"rimraf": "~2.6.2",
"yargs": "~10.0.3"
},
"devDependencies": {
"babel-preset-env": "^1.6.1"
"simple-git": "~1.92.0",
"yargs": "~11.0.0"
}
}
1 change: 0 additions & 1 deletion packages/best-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const options = {
type: 'string',
},
compareStats: {
default: undefined,
description:
'Compares two benchmark runs for a given commit. ' +
'If --externalStorage is provided it will use that source' +
Expand Down
3 changes: 1 addition & 2 deletions packages/best-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export async function run(maybeArgv, project) {
const projects = getProjectListFromCLIArgs(argsCLI, project);
await runCLI(argsCLI, projects);
} catch (error) {
console.log('>>', error);
const errParts = error.stack ? error.stack.split('\n') : ['unknown', 'unknown'];
errorMessager.print(errParts.shift());
console.warn(errParts.join('\n'));
Expand Down Expand Up @@ -69,7 +68,7 @@ export async function runCLI(argsCLI, projects) {
return process.exit(0);
}

if (globalConfig.compareStats) {
if (argsCLI.compareStats) {
results = await runCompare(globalConfig, configs, outputStream);
if (results) {
generateComparisonTable(results, outputStream);
Expand Down
4 changes: 2 additions & 2 deletions packages/best-cli/src/cli/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import chalk from 'chalk';
function padding(n) {
return n > 0
? Array.apply(null, Array((n - 1) * 3))
.map(() => ' ')
.join('') + '└─ '
.map(() => ' ')
.join('') + '└─ '
: '';
}

Expand Down
7 changes: 6 additions & 1 deletion packages/best-cli/src/run_best.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import { storeBenchmarkResults } from '@best/store';
import { analyzeBenchmarks } from '@best/analyzer';
import path from 'path';

const IGNORE_PATHS = [
'**/node_modules/**',
'**/__tests__/**'
];

async function getBenchmarkPaths({ nonFlagArgs, rootDir }, config) {
const { testMatch, rootDir: projectRoot } = config;
const rootPath = projectRoot || rootDir;
const paths = nonFlagArgs && nonFlagArgs.length ? nonFlagArgs : testMatch;
const results = await globby(paths, { cwd: rootPath });
const results = await globby(paths, { cwd: rootPath, ignore: IGNORE_PATHS });
return results.map(p => path.resolve(rootPath, p));
}

Expand Down
81 changes: 63 additions & 18 deletions packages/best-cli/src/run_compare.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import { preRunMessager } from '@best/messager';
import { compareBenchmarkStats } from '@best/compare';
import { pushBenchmarkComparison } from '@best/github-integration';
import { runBest } from "./run_best";
import git from "simple-git/promise";

const STORAGE_FS = '@best/store-fs';
const isHex = x => /^[0-9a-fA-F]+$/.test(x);
const normalizeCommit = async (commit, gitCLI) => {
if (commit === 'current') {
const result = await gitCLI.log();
commit = result.latest.hash;
}

if (!isHex(commit)) {
// If is not hexadecimal we try to look for a local branch
commit = await gitCLI.revparse([commit]);
}

return commit.slice(0, 7);
};

export async function runCompare(globalConfig, configs, outputStream) {
const { gitIntegration, externalStorage, compareStats: commits } = globalConfig;
const [baseCommit, compareCommit] = commits;
const { gitLocalChanges, rootDir, gitIntegration, externalStorage, compareStats = [] } = globalConfig;
let [baseCommit, compareCommit] = compareStats;

if (!externalStorage) {
throw new Error('WIP - Do not support local comparison just yet. You need a --externalStorage');
if (gitLocalChanges) {
throw new Error(`Can't compare benchmmarks due to uncommited local changes in this branch.`);
}

if (commits.length === 0 || commits.length > 2) {
if (compareStats.length === 0 || compareStats.length > 2) {
throw new Error('Wrong number of commmits to compare we are expectine one or two');
}

Expand All @@ -19,21 +37,48 @@ export async function runCompare(globalConfig, configs, outputStream) {
return false;
}

const projects = configs.map(cfg => cfg.projectName);
const projectNames = projects.length ? projects : [globalConfig.rootProjectName];
let storageProvider;
const gitCLI = git(rootDir);
const status = await gitCLI.status();
const initialBranch = status.current;

baseCommit = await normalizeCommit(baseCommit, gitCLI);
compareCommit = await normalizeCommit(compareCommit, gitCLI);

try {
storageProvider = require(externalStorage);
} catch (err) {
throw new Error(`Can't resolve the externalStorage ${externalStorage}`);
}
const projects = configs.map(cfg => cfg.projectName);
const projectNames = projects.length ? projects : [globalConfig.rootProjectName];
let storageProvider;

preRunMessager.print('\n Fetching benchmark results to compare... \n\n', outputStream);
const compareResults = await compareBenchmarkStats(baseCommit, compareCommit, projectNames, storageProvider);
// If not external storage we will run the benchmarks and compare using fs
if (!externalStorage) {
storageProvider = require(STORAGE_FS);
storageProvider.initialize({ rootDir });
// Run base commit
preRunMessager.print(`\n Running best for commit ${baseCommit} \n`, outputStream);
await gitCLI.checkout(baseCommit);
await runBest({ ...globalConfig, gitCommit: baseCommit }, configs, outputStream);

if (gitIntegration) {
await pushBenchmarkComparison(baseCommit, compareCommit, compareResults, globalConfig);
}
// Run compare Commit
preRunMessager.print(`\n Running best for commit ${compareCommit} \n`, outputStream);
await gitCLI.checkout(compareCommit);
await runBest({ ...globalConfig, gitCommit: compareCommit }, configs, outputStream);
} else {
try {
storageProvider = require(externalStorage);
storageProvider.initialize({});
} catch (err) {
throw new Error(`Can't resolve the externalStorage ${externalStorage}`);
}
}

return compareResults;
const compareResults = await compareBenchmarkStats(baseCommit, compareCommit, projectNames, storageProvider);

if (gitIntegration) {
await pushBenchmarkComparison(baseCommit, compareCommit, compareResults, globalConfig);
}

return compareResults;
} finally {
await gitCLI.checkout(initialBranch);
}
}
21 changes: 14 additions & 7 deletions packages/best-messager/src/messager-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const DONE = chalk.reset.inverse.green.bold(DONE_TEXT) + ' ';
const ERROR_TEXT = ' ERROR ';
const ERROR = chalk.reset.inverse.red.bold(ERROR_TEXT) + ' ';
const PROGRESS_BAR_WIDTH = 40;
const COLUMNS = process.stdout.columns || 80;
const TRUNCATED_TEXT = chalk.reset.dim('(truncated)');
const TRUNCATED = `.../${TRUNCATED_TEXT}/`;

const printState = state => {
switch (state) {
Expand All @@ -36,6 +39,14 @@ const printState = state => {
}
};

const printBenchmarkEntry = benchmarkEntry => {
if (benchmarkEntry.length > COLUMNS) {
benchmarkEntry = TRUNCATED + benchmarkEntry.slice(TRUNCATED.length - COLUMNS);
}

return chalk.dim(`${benchmarkEntry}\n`);
};

const printDisplayPath = relativeDir => {
if (relativeDir) {
const dirname = path.dirname(relativeDir);
Expand Down Expand Up @@ -123,10 +134,7 @@ export default class RunnerMessager {
return map;
}, {});

this._state = {
benchmarks: benchmarksState,
buffer: '',
};
this._state = { benchmarks: benchmarksState, buffer: '' };

this._write();
}
Expand Down Expand Up @@ -186,7 +194,7 @@ export default class RunnerMessager {
}

_clear() {
this._out(this._state.clear);
this._out(clearStream(this._state.buffer));
}

_write() {
Expand Down Expand Up @@ -214,7 +222,7 @@ export default class RunnerMessager {
buffer +=
[
'\n' + PROGRESS_TEXT + chalk.bold.black(displayName) + ' ',
chalk.dim(`${benchmarkEntry}\n`),
printBenchmarkEntry(benchmarkEntry),
chalk.bold.black('Avg iteration: ') + progress.avgIteration.toFixed(2) + 'ms',
chalk.bold.black('Completed iterations: ') + progress.executedIterations,
].join('\n') + '\n\n';
Expand All @@ -227,7 +235,6 @@ export default class RunnerMessager {
}

this._state.buffer = buffer;
this._state.clear = clearStream(buffer);
this._out(buffer);
}
}
4 changes: 4 additions & 0 deletions packages/best-store-aws/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function getS3Instance() {
return S3_INSTANCE;
}

export function initialize(/* config */) {
getS3Instance();
}

export async function storeBenchmarkResults(
fileMap,
{ benchmarkName, benchmarkSignature, projectConfig },
Expand Down
1 change: 1 addition & 0 deletions packages/best-store-fs/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
15 changes: 15 additions & 0 deletions packages/best-store-fs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@best/store-fs",
"version": "0.3.2",
"description": "Best Store - File System",
"keywords": [
"Best",
"LWC"
],
"main": "build/index.js",
"module": "src/index.js",
"dependencies": {
"chalk": "~2.3.0",
"globby": "~7.1.1"
}
}
39 changes: 39 additions & 0 deletions packages/best-store-fs/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from 'fs';
import chalk from 'chalk';
import globby from 'globby';

let ROOT_DIR = process.cwd();
const IGNORE_PATHS = [
'**/node_modules/**',
'**/__tests__/**'
];

export function initialize({ rootDir }) {
ROOT_DIR = rootDir || ROOT_DIR;
}

export async function storeBenchmarkResults(
fileMap,
{ benchmarkName, benchmarkSignature, projectConfig },
globalConfig,
) {
console.log("WIP");
}

export async function getAllBenchmarkStatsPerCommit(projectName, commit) {
const pattern = `**/${projectName}/*.benchmark_${commit}/stats.json`;
const results = await globby([pattern], { cwd: ROOT_DIR, ignore: IGNORE_PATHS });
const statsResults = results.map(statsPath => {
const stats = JSON.parse(fs.readFileSync(statsPath, 'utf8'));
return { ...stats, projectName };
});
return statsResults;
}

export function getProjects() {
console.log('WIP');
}

export function getCommits(projectName, branch) {
console.log('WIP');
}
5 changes: 3 additions & 2 deletions packages/best-store/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ export function storeBenchmarkResults(benchmarkResults, globalConfig) {
benchmarkResults.map(async benchmarkResult => {
const { benchmarkName, benchmarkSignature, projectConfig, environment, results, stats } = benchmarkResult;
const { benchmarkOutput, cacheDirectory, projectName } = projectConfig;
const { externalStorage } = globalConfig;
const outputFolder = path.join(benchmarkOutput, projectName, `${benchmarkName}_${benchmarkSignature.substr(0, 6)}`);
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);

Expand Down
Loading

0 comments on commit 5781d55

Please sign in to comment.