Skip to content

Commit

Permalink
diff with previous report for memory report
Browse files Browse the repository at this point in the history
  • Loading branch information
guipublic committed Nov 28, 2024
1 parent ea0644b commit 3c005d0
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 31 deletions.
60 changes: 47 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,22 +433,12 @@ function run() {
catch (error) {
return core.setFailed(error.message);
}
// avoid the failing artifact download temporarily, for debugging purposes
if (memory_report) {
core.startGroup("Load reports");
core.info(`Loading reports from "${localReportPath}"`);
const compareContent = fs.readFileSync(localReportPath, "utf8");
core.info(`Format Memory markdown rows`);
const memoryContent = (0, report_1.memoryReports)(compareContent);
const markdown = (0, report_1.formatMemoryReport)(memoryContent);
core.setOutput("markdown", markdown);
return;
}
// cannot use artifactClient because downloads are limited to uploads in the same workflow run
// cf. https://docs.github.com/en/actions/using-workflows/storing-workflow-data-as-artifacts#downloading-or-deleting-artifacts
if (github_1.context.eventName === "pull_request") {
try {
core.startGroup(`Searching artifact "${baseReport}" on repository "${repository}", on branch "${baseBranch}"`);
let count = 100;
let artifactId = null;
try {
// Artifacts are returned in most recent first order.
Expand All @@ -460,7 +450,11 @@ function run() {
_e = false;
try {
const res = _c;
if (count == 0) {
break;
}
const artifact = res.data.find((artifact) => !artifact.expired && artifact.name === baseReport);
count = count - 1;
if (!artifact) {
yield new Promise((resolve) => setTimeout(resolve, 900)); // avoid reaching the API rate limit
continue;
Expand Down Expand Up @@ -512,7 +506,8 @@ function run() {
if (memory_report) {
core.info(`Format Memory markdown rows`);
const memoryContent = (0, report_1.memoryReports)(compareContent);
const markdown = (0, report_1.formatMemoryReport)(memoryContent);
const referenceReports = (0, report_1.memoryReports)(referenceContent);
const markdown = (0, report_1.computeMemoryDiff)(referenceReports, memoryContent);
core.setOutput("markdown", markdown);
return;
}
Expand Down Expand Up @@ -588,7 +583,7 @@ run();
"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.formatMemoryReport = exports.computeContractDiffs = exports.computeProgramDiffs = exports.computedWorkspaceDiff = exports.memoryReports = exports.loadReports = exports.variation = void 0;
exports.computeMemoryDiff = exports.formatMemoryReport = exports.computeContractDiffs = exports.computeProgramDiffs = exports.computedWorkspaceDiff = exports.memoryReports = exports.loadReports = exports.variation = void 0;
const variation = (current, previous) => {
const delta = current - previous;
return {
Expand Down Expand Up @@ -729,6 +724,45 @@ const formatMemoryReport = (memReports) => {
return markdown;
};
exports.formatMemoryReport = formatMemoryReport;
const computeMemoryDiff = (refReports, memReports) => {
let markdown = "";
const diff_percentage = [];
let diff_column = false;
if (refReports.length === memReports.length) {
for (let i = 0; i < refReports.length; i++) {
let diff_str = "N/A";
if (refReports[i].artifact_name === memReports[i].artifact_name) {
const compPeak = memReports[i].peak_memory;
const refPeak = refReports[i].peak_memory;
let diff = 0;
if (compPeak[compPeak.length - 1] == refPeak[refPeak.length - 1]) {
const compPeakValue = parseInt(compPeak.substring(0, compPeak.length - 1));
const refPeakValue = parseInt(refPeak.substring(0, refPeak.length - 1));
diff = Math.floor(((compPeakValue - refPeakValue) / refPeakValue) * 100);
}
else {
diff = 100;
}
if (diff != 0) {
diff_column = true;
}
diff_str = diff.toString() + "%";
}
diff_percentage.push(diff_str);
}
}
if (diff_column == true) {
markdown = "## Peak Memory Sample\n | Program | Peak Memory | % |\n | --- | --- | --- |\n";
for (let i = 0; i < memReports.length; i++) {
markdown = markdown.concat(" | ", memReports[i].artifact_name, " | ", memReports[i].peak_memory, " | ", diff_percentage[i], " |\n");
}
}
else {
markdown = (0, exports.formatMemoryReport)(memReports);
}
return markdown;
};
exports.computeMemoryDiff = computeMemoryDiff;


/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

24 changes: 8 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
formatShellDiff,
formatShellDiffBrillig,
} from "./format/program";
import { loadReports, computeProgramDiffs, memoryReports, formatMemoryReport } from "./report";
import { loadReports, computeProgramDiffs, memoryReports, computeMemoryDiff } from "./report";

const token = process.env.GITHUB_TOKEN || core.getInput("token");
const report = core.getInput("report");
Expand Down Expand Up @@ -53,36 +53,27 @@ async function run() {
return core.setFailed((error as Error).message);
}

// avoid the failing artifact download temporarily, for debugging purposes
if (memory_report) {
core.startGroup("Load reports");
core.info(`Loading reports from "${localReportPath}"`);
const compareContent = fs.readFileSync(localReportPath, "utf8");
core.info(`Format Memory markdown rows`);
const memoryContent = memoryReports(compareContent);
const markdown = formatMemoryReport(memoryContent);
core.setOutput("markdown", markdown);
return;
}

// cannot use artifactClient because downloads are limited to uploads in the same workflow run
// cf. https://docs.github.com/en/actions/using-workflows/storing-workflow-data-as-artifacts#downloading-or-deleting-artifacts
if (context.eventName === "pull_request") {
try {
core.startGroup(
`Searching artifact "${baseReport}" on repository "${repository}", on branch "${baseBranch}"`
);

let count = 100;
let artifactId: number | null = null;
// Artifacts are returned in most recent first order.
for await (const res of octokit.paginate.iterator(octokit.rest.actions.listArtifactsForRepo, {
owner,
repo,
})) {
if (count == 0) {
break;
}
const artifact = res.data.find(
(artifact) => !artifact.expired && artifact.name === baseReport
);

count = count - 1;
if (!artifact) {
await new Promise((resolve) => setTimeout(resolve, 900)); // avoid reaching the API rate limit

Expand Down Expand Up @@ -129,7 +120,8 @@ async function run() {
if (memory_report) {
core.info(`Format Memory markdown rows`);
const memoryContent = memoryReports(compareContent);
const markdown = formatMemoryReport(memoryContent);
const referenceReports = memoryReports(referenceContent);
const markdown = computeMemoryDiff(referenceReports, memoryContent);
core.setOutput("markdown", markdown);
return;
}
Expand Down
50 changes: 50 additions & 0 deletions src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,53 @@ export const formatMemoryReport = (memReports: MemoryReport[]): string => {
}
return markdown;
};

export const computeMemoryDiff = (
refReports: MemoryReport[],
memReports: MemoryReport[]
): string => {
let markdown = "";
const diff_percentage = [];
let diff_column = false;
if (refReports.length === memReports.length) {
for (let i = 0; i < refReports.length; i++) {
let diff_str = "N/A";
if (refReports[i].artifact_name === memReports[i].artifact_name) {
const compPeak = memReports[i].peak_memory;
const refPeak = refReports[i].peak_memory;
let diff = 0;
if (compPeak[compPeak.length - 1] == refPeak[refPeak.length - 1]) {
const compPeakValue = parseInt(compPeak.substring(0, compPeak.length - 1));
const refPeakValue = parseInt(refPeak.substring(0, refPeak.length - 1));
diff = Math.floor(((compPeakValue - refPeakValue) / refPeakValue) * 100);
} else {
diff = 100;
}
if (diff != 0) {
diff_column = true;
}
diff_str = diff.toString() + "%";
}
diff_percentage.push(diff_str);
}
}

if (diff_column == true) {
markdown = "## Peak Memory Sample\n | Program | Peak Memory | % |\n | --- | --- | --- |\n";
for (let i = 0; i < memReports.length; i++) {
markdown = markdown.concat(
" | ",
memReports[i].artifact_name,
" | ",
memReports[i].peak_memory,
" | ",
diff_percentage[i],
" |\n"
);
}
} else {
markdown = formatMemoryReport(memReports);
}

return markdown;
};
13 changes: 12 additions & 1 deletion tests/memory_report.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as fs from "fs";

import { memoryReports, formatMemoryReport } from "../src/report";
import { memoryReports, formatMemoryReport, computeMemoryDiff } from "../src/report";

const srcContent = fs.readFileSync("tests/mocks/mem_report.json", "utf8");
const ref_1_Content = fs.readFileSync("tests/mocks/1-1-mem_report.json", "utf8");
const ref_2_Content = fs.readFileSync("tests/mocks/1-2-mem_report.json", "utf8");

const memReports = memoryReports(srcContent);

Expand All @@ -12,4 +14,13 @@ describe("Markdown format", () => {
const markdown = formatMemoryReport(memReports);
expect(markdown.length).toBeGreaterThan(0);
});

it("should generate diff report format", () => {
const ref_1_Reports = memoryReports(ref_1_Content);
const ref_2_Reports = memoryReports(ref_2_Content);
expect(ref_1_Reports.length).toBeGreaterThan(0);
expect(ref_1_Reports.length).toBe(ref_2_Reports.length);
const markdown = computeMemoryDiff(ref_1_Reports, ref_2_Reports);
expect(markdown.length).toBeGreaterThan(0);
});
});
16 changes: 16 additions & 0 deletions tests/mocks/1-1-mem_report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{"memory_reports": [
{
"artifact_name":"keccak256",
"peak_memory":"85.19M"
}
,
{
"artifact_name":"workspace",
"peak_memory":"123.04M"
}
,
{
"artifact_name":"regression_4709",
"peak_memory":"349.30M"
}
]}
16 changes: 16 additions & 0 deletions tests/mocks/1-2-mem_report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{"memory_reports": [
{
"artifact_name":"keccak256",
"peak_memory":"95.19M"
}
,
{
"artifact_name":"workspace",
"peak_memory":"103.04M"
}
,
{
"artifact_name":"regression_4709",
"peak_memory":"749.30M"
}
]}

0 comments on commit 3c005d0

Please sign in to comment.