@dapplion/benchmark
Version:
Ensures that new code does not introduce performance regressions with CI. Tracks:
100 lines (99 loc) • 4.37 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveCompare = resolveCompare;
exports.resolvePrevBenchmark = resolvePrevBenchmark;
exports.renderCompareWith = renderCompareWith;
exports.resolveCompareWith = resolveCompareWith;
const github = __importStar(require("@actions/github"));
const index_js_1 = require("../utils/index.js");
const context_js_1 = require("../github/context.js");
const schema_js_1 = require("../history/schema.js");
var CompareWithType;
(function (CompareWithType) {
CompareWithType["latestCommitInBranch"] = "latestCommitInBranch";
CompareWithType["exactCommit"] = "exactCommit";
})(CompareWithType || (CompareWithType = {}));
async function resolveCompare(provider, opts) {
const compareWith = await resolveCompareWith(opts);
const prevBench = await resolvePrevBenchmark(compareWith, provider);
if (!prevBench)
return null;
(0, schema_js_1.validateBenchmark)(prevBench);
return prevBench;
}
async function resolvePrevBenchmark(compareWith, provider) {
switch (compareWith.type) {
case CompareWithType.exactCommit:
return await provider.readHistoryCommit(compareWith.commitSha);
case CompareWithType.latestCommitInBranch: {
// Try first latest commit in branch
return await provider.readLatestInBranch(compareWith.branch);
}
}
}
function renderCompareWith(compareWith) {
switch (compareWith.type) {
case CompareWithType.exactCommit:
return `exactCommit ${compareWith.commitSha}`;
case CompareWithType.latestCommitInBranch: {
if (compareWith.before) {
return `latestCommitInBranch '${compareWith.branch}' before commit ${compareWith.before}`;
}
else {
return `latestCommitInBranch '${compareWith.branch}'`;
}
}
}
}
async function resolveCompareWith(opts) {
// compare may be a branch or commit
if (opts.compareBranch) {
return { type: CompareWithType.latestCommitInBranch, branch: opts.compareBranch };
}
if (opts.compareCommit) {
return { type: CompareWithType.exactCommit, commitSha: opts.compareCommit };
}
// In GA CI figure out what to compare against with github actions events
if ((0, context_js_1.isGaRun)()) {
switch (github.context.eventName) {
case "pull_request": {
const eventData = (0, index_js_1.getGithubEventData)();
const baseBranch = eventData.pull_request.base.ref; // base.ref is already parsed
return { type: CompareWithType.latestCommitInBranch, branch: baseBranch };
}
case "push": {
const eventData = (0, index_js_1.getGithubEventData)();
const branch = (0, index_js_1.parseBranchFromRef)(github.context.ref);
return { type: CompareWithType.latestCommitInBranch, branch: branch, before: eventData.before };
}
default:
throw Error(`event not supported ${github.context.eventName}`);
}
}
// Otherwise compare against the default branch
const defaultBranch = await (0, index_js_1.getDefaultBranch)(opts);
return { type: CompareWithType.latestCommitInBranch, branch: defaultBranch };
}