Skip to content

Commit

Permalink
Restructure the report
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Jan 7, 2025
1 parent d79159f commit 53bbb67
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 154 deletions.
14 changes: 10 additions & 4 deletions src/cli/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import path from "node:path";
import fs from "node:fs";
import {LocalHistoryProvider} from "../history/local.js";
import {consoleLog} from "../utils/output.js";
import {compareBenchmarks} from "../compare/compute.js";
import {computeComparisonReport} from "../compare/compute.js";
import {renderBenchmarkComparisonTable} from "../utils/render.js";
import {isGaRun} from "../github/context.js";
import {postGaCommentCrossComparison} from "../github/comment.js";
import {postGaComment} from "../github/comments/index.js";
import {benchmarkComparisonComment} from "../github/comments/comparisonReportComment.js";
import {GithubCommentTag} from "../github/octokit.js";

export async function compare({dirs}: {dirs: string[]}): Promise<void> {
consoleLog("Comparing benchmarks:");
Expand All @@ -32,11 +34,15 @@ export async function compare({dirs}: {dirs: string[]}): Promise<void> {
}
}

const resultsComp = compareBenchmarks(benchmarks);
const resultsComp = computeComparisonReport(benchmarks);

consoleLog(renderBenchmarkComparisonTable(resultsComp, "cli"));

if (isGaRun()) {
await postGaCommentCrossComparison(resultsComp);
await postGaComment({
commentBody: benchmarkComparisonComment(resultsComp),
tag: GithubCommentTag.ComparisonReport,
commentOnPush: true,
});
}
}
14 changes: 10 additions & 4 deletions src/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {validateBenchmark} from "../history/schema.js";
import {Benchmark, BenchmarkOpts, FileCollectionOptions, StorageOptions} from "../types.js";
import {renderCompareWith, resolveCompareWith, resolvePrevBenchmark} from "../compare/index.js";
import {parseBranchFromRef, getCurrentCommitInfo, shell, getCurrentBranch, collectFiles} from "../utils/index.js";
import {computeBenchComparison} from "../compare/compute.js";
import {postGaCommentSelfComparison} from "../github/comment.js";
import {computePerformanceReport} from "../compare/compute.js";
import {postGaComment} from "../github/comments/index.js";
import {isGaRun} from "../github/context.js";
import {BenchmarkRunner} from "../benchmark/runner.js";
import {optionsDefault} from "./options.js";
import {consoleLog} from "../utils/output.js";
import {HistoryProviderType} from "../history/provider.js";
import {performanceReportComment} from "../github/comments/performanceReportComment.js";
import {GithubCommentTag} from "../github/octokit.js";

export async function run(opts_: FileCollectionOptions & StorageOptions & BenchmarkOpts): Promise<void> {
const opts = Object.assign({}, optionsDefault, opts_);
Expand Down Expand Up @@ -74,10 +76,14 @@ export async function run(opts_: FileCollectionOptions & StorageOptions & Benchm
await historyProvider.writeToHistory(currBench);
}

const resultsComp = computeBenchComparison(currBench, prevBench, opts.threshold);
const resultsComp = computePerformanceReport(currBench, prevBench, opts.threshold);

if (!opts.skipPostComment && isGaRun()) {
await postGaCommentSelfComparison(resultsComp);
await postGaComment({
commentBody: performanceReportComment(resultsComp),
tag: GithubCommentTag.PerformanceReport,
commentOnPush: resultsComp.someFailed,
});
}

if (resultsComp.someFailed && !opts.noThrow) {
Expand Down
18 changes: 9 additions & 9 deletions src/compare/compute.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {
ResultSelfComparison,
BenchmarkSelfComparison,
PerformanceResult,
PerformanceReport,
Benchmark,
BenchmarkResult,
BenchmarkCrossComparison,
ResultCrossComparison,
BenchmarkComparisonReport,
BenchmarkComparisonResult,
} from "../types.js";

export function compareBenchmarks(benchmarks: Benchmark[]): BenchmarkCrossComparison {
export function computeComparisonReport(benchmarks: Benchmark[]): BenchmarkComparisonReport {
const originBenchmark = benchmarks[0];
const targetBenchmarks = benchmarks.slice(1);

const results = new Map<string, ResultCrossComparison[]>();
const results = new Map<string, BenchmarkComparisonResult[]>();
for (const res of originBenchmark.results) {
results.set(res.id, [
{...res, targetAverageNs: null, originAverageNs: res.averageNs, isFailed: false, isImproved: false, ratio: 1.0},
Expand Down Expand Up @@ -64,19 +64,19 @@ export function compareBenchmarks(benchmarks: Benchmark[]): BenchmarkCrossCompar
};
}

export function computeBenchComparison(
export function computePerformanceReport(
currBench: Benchmark,
prevBench: Benchmark | null,
threshold: number
): BenchmarkSelfComparison {
): PerformanceReport {
const prevResults = new Map<string, BenchmarkResult>();
if (prevBench) {
for (const bench of prevBench.results) {
prevResults.set(bench.id, bench);
}
}

const results = currBench.results.map((currBench): ResultSelfComparison => {
const results = currBench.results.map((currBench): PerformanceResult => {
const {id} = currBench;
const prevBench = prevResults.get(id);
const thresholdBench = currBench.threshold ?? threshold;
Expand Down
71 changes: 0 additions & 71 deletions src/github/comment.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/github/comments/comparisonReportComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {BenchmarkComparisonReport} from "../../types.js";
import {renderBenchmarkComparisonTable} from "../../utils/render.js";

export function benchmarkComparisonComment(report: BenchmarkComparisonReport): string {
return `
## Comparison Report
${renderBenchmarkComparisonTable(report, "html")}
`;
}
39 changes: 39 additions & 0 deletions src/github/comments/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as github from "@actions/github";
import {getGithubEventData, GithubActionsEventData} from "../../utils/index.js";
import {commentToCommit, commentToPrUpdatable, GithubCommentTag} from "../octokit.js";

export async function postGaComment({
commentBody,
tag,
commentOnPush,
}: {
commentBody: string;
tag: GithubCommentTag;
commentOnPush: boolean;
}): Promise<void> {
switch (github.context.eventName) {
case "pull_request": {
const eventData = getGithubEventData<GithubActionsEventData["pull_request"]>();
const prNumber = eventData.number;
if (!prNumber) {
throw Error("github event data has no PR number");
}

await commentToPrUpdatable(prNumber, commentBody, tag);

break;
}

case "push": {
// Only comment on performance regression
if (commentOnPush) {
await commentToCommit(github.context.sha, commentBody);
}

break;
}

default:
throw Error(`event not supported ${github.context.eventName}`);
}
}
40 changes: 40 additions & 0 deletions src/github/comments/performanceReportComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {PerformanceReport} from "../../types.js";
import {renderPerformanceTable} from "../../utils/render.js";

export function performanceReportComment(report: PerformanceReport): string {
const isFailedResults = report.results.filter((r) => r.isFailed);
const isImprovedResults = report.results.filter((r) => r.isImproved);

if (report.someFailed) {
return `
## :warning: **Performance Alert** :warning:
Possible performance regression was detected for some benchmarks.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold.
${renderPerformanceTable(isFailedResults, report, "html")}
`;
}

if (isImprovedResults.length > 0) {
return `
## Performance Report
🚀🚀 Significant benchmark improvement detected
<details><summary>Full benchmark results</summary>
${renderPerformanceTable(isImprovedResults, report, "html")}
</details>
`;
}

return `
## Performance Report
✔️ no performance regression detected
<details><summary>Full benchmark results</summary>
${renderPerformanceTable(report.results, report, "html")}
</details>
`;
}
4 changes: 2 additions & 2 deletions src/github/octokit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {getContext} from "./context.js";

export enum GithubCommentTag {
BenchmarkResult = "benchmarkbot/action",
CrossCompareResult = "benchmarkbot/action/compare",
PerformanceReport = "benchmarkbot/action",
ComparisonReport = "benchmarkbot/compare",
}

export async function commentToPrUpdatable(prNumber: number, body: string, tag: GithubCommentTag): Promise<void> {
Expand Down
12 changes: 6 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ export type BenchmarkHistory = {
};
};

export type BenchmarkSelfComparison = {
export type PerformanceReport = {
currCommitSha: CommitSha;
prevCommitSha: CommitSha | null;
someFailed: boolean;
results: ResultSelfComparison[];
results: PerformanceResult[];
};

export type ResultSelfComparison = {
export type PerformanceResult = {
id: BenchId;
currAverageNs: number;
prevAverageNs: number | null;
Expand All @@ -130,16 +130,16 @@ export type ResultSelfComparison = {
isImproved: boolean;
};

export type BenchmarkCrossComparison = {
export type BenchmarkComparisonReport = {
someFailed: boolean;
// The first element will always contain origin which is used to compare
commitsShas: (CommitSha | null)[];
dirNames: string[];
// The result array contains the origin commit first and then targets
results: Map<BenchId, ResultCrossComparison[]>;
results: Map<BenchId, BenchmarkComparisonResult[]>;
};

export type ResultCrossComparison = {
export type BenchmarkComparisonResult = {
id: BenchId;
originAverageNs: number | null;
targetAverageNs: number | null;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/extendedTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ExtendedTable extends CliTable3 {
* Render the table as HTML
*/
toHTML(): string {
let html = "<table>\n";
let html = "\n<table>\n";

if (this.options.head && this.options.head.length > 0) {
html += "<thead>\n<tr>\n";
Expand Down
Loading

0 comments on commit 53bbb67

Please sign in to comment.