{"size": 2393, "ext": "js", "max_stars_repo_path": "study-node/boomerang/tests/perf/perf-compare.js", "max_stars_repo_name": "dreamSeekerYu/study", "max_stars_repo_stars_event_min_datetime": "2020-09-02T05:40:03.000Z", "max_stars_repo_stars_event_max_datetime": "2020-09-02T07:36:31.000Z", "max_issues_repo_path": "study-node/boomerang/tests/perf/perf-compare.js", "max_issues_repo_name": "dreamSeekerYu/study", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "study-node/boomerang/tests/perf/perf-compare.js", "max_forks_repo_name": "dreamSeekerYu/study", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.4868421053, "max_line_length": 113, "alphanum_fraction": 0.6640200585}
//
// Imports
//
const path = require("path");
const util = require("util");
const fs = require("fs");
const grunt = require("grunt");
const chalk = require("chalk");

// promisify"d functions
const mkdir = util.promisify(fs.mkdir);
const exists = util.promisify(fs.exists);
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);

const resultsPath = path.join(__dirname, "results");
const baselineFilePath = path.join(resultsPath, "baseline.json");
const metricsFilePath = path.join(resultsPath, "metrics.json");

//
// Exports
//
module.exports = async function() {
	const done = this.async();

	// check files exist first
	if (!await exists(baselineFilePath)) {
		grunt.log.error(baselineFilePath + " does not exist!  Creat it with 'grunt perf:baseline'");
		return done();
	}

	if (!await exists(metricsFilePath)) {
		grunt.log.error(metricsFilePath + " does not exist!  Creat it with 'grunt perf'");
		return done();
	}

	// load both metrics files
	const baselineMetrics = JSON.parse(await readFile(baselineFilePath, "utf-8"));
	const compareMetrics = JSON.parse(await readFile(metricsFilePath, "utf-8"));

	console.log("Results comparison to baseline:\n");

	for (var scenarioName in compareMetrics) {
		for (var testName in compareMetrics[scenarioName]) {
			for (var metricName in compareMetrics[scenarioName][testName]) {
				const base = baselineMetrics[scenarioName][testName][metricName];
				const cur = compareMetrics[scenarioName][testName][metricName];

				// compare metrics to the baseline
				const diff = cur - base;
				const diffPct = Math.floor(diff / (base ? base : diff) * 100);

				// output display content
				const metricNamePrefix = `${chalk.underline(scenarioName + "." + testName + "." + metricName).padEnd(80)}: `;
				const diffPctSuffix = " (" + (diff > 0 ? "+" : "") + `${chalk.yellow(diffPct + "%")})`;

				// show diffs
				if (!base) {
					console.log(`${metricNamePrefix}${chalk.gray("[missing in baseline]")}`);
				}
				else if (diff === 0 && !grunt.option("diff-only")) {
					console.log(`${metricNamePrefix}=${chalk.green(base)}`);
				}
				else if (diff > 0) {
					console.log(`${metricNamePrefix} ${cur} ${chalk.yellow("+" + diff)}${diffPctSuffix}`);
				}
				else if (diff < 0) {
					console.log(`${metricNamePrefix} ${cur} ${chalk.yellow(diff)}${diffPctSuffix}`);
				}
			}
		}
	}

	done();
};
