Skip to content

Commit

Permalink
add support for memory report
Browse files Browse the repository at this point in the history
  • Loading branch information
guipublic committed Nov 26, 2024
1 parent d88f752 commit b0319af
Show file tree
Hide file tree
Showing 5 changed files with 399 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import {
formatShellDiff,
formatShellDiffBrillig,
} from "./format/program";
import { loadReports, computeProgramDiffs } from "./report";
import { loadReports, computeProgramDiffs, memoryReports, formatMemoryReport } from "./report";

const token = process.env.GITHUB_TOKEN || core.getInput("token");
const report = core.getInput("report");
const header = core.getInput("header");
const brillig_report = core.getInput("brillig_report");
const brillig_report_bytes = core.getInput("brillig_report_bytes");
const memory_report = core.getInput("memory_report");
const summaryQuantile = parseFloat(core.getInput("summaryQuantile"));
// const sortCriteria = core.getInput("sortCriteria").split(",");
// const sortOrders = core.getInput("sortOrders").split(",");
Expand Down Expand Up @@ -109,21 +110,30 @@ async function run() {
}

try {
core.startGroup("Load gas reports");
core.info(`Loading gas reports from "${localReportPath}"`);
core.startGroup("Load reports");
core.info(`Loading reports from "${localReportPath}"`);
const compareContent = fs.readFileSync(localReportPath, "utf8");
referenceContent ??= compareContent; // if no source gas reports were loaded, defaults to the current gas reports

core.info(`Mapping compared gas reports`);
if (memory_report) {
core.info(`Format Memory markdown rows`);
const memoryContent = memoryReports(compareContent);
const markdown = formatMemoryReport(memoryContent);
core.setOutput("markdown", markdown);
return;
}

referenceContent ??= compareContent; // if no source reports were loaded, defaults to the current reports

core.info(`Mapping compared reports`);
const compareReports = loadReports(compareContent);
core.info(`Got ${compareReports.programs.length} compare programs`);

core.info(`Mapping reference gas reports`);
core.info(`Mapping reference reports`);
const referenceReports = loadReports(referenceContent);
core.info(`Got ${compareReports.programs.length} reference programs`);
core.endGroup();

core.startGroup("Compute gas diff");
core.startGroup("Compute diff");
const [diffCircuitRows, diffBrilligRows] = computeProgramDiffs(
referenceReports.programs,
compareReports.programs
Expand All @@ -141,8 +151,6 @@ async function run() {
[summaryRows, fullReportRows] = formatCircuitRows(diffCircuitRows, summaryQuantile);
}

core.info(`Format markdown of ${numDiffs} diffs`);
// const [summaryRows, fullReportRows] = formatCircuitRows(diffCircuitRows, summaryQuantile);
const markdown = formatMarkdownDiff(
header,
repository,
Expand Down
20 changes: 20 additions & 0 deletions src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ProgramReport,
BrilligReport,
DiffBrillig,
MemoryReport,
} from "./types";

export const variation = (current: number, previous: number) => {
Expand All @@ -27,6 +28,10 @@ export const loadReports = (content: string): WorkspaceReport => {
return JSON.parse(content);
};

export const memoryReports = (content: string): MemoryReport[] => {
return JSON.parse(content).memory_reports;
};

export const computedWorkspaceDiff = (
sourceReport: WorkspaceReport,
compareReport: WorkspaceReport
Expand Down Expand Up @@ -193,3 +198,18 @@ const computeContractDiff = (
functions: functionDiffs,
};
};

export const formatMemoryReport = (memReports: MemoryReport[]): string => {
let markdown = "## Peak Memory Sample\n | Program | Peak Memory |\n | --- | --- |\n";
expect(memReports.length).toBeGreaterThan(0);
for (let i = 0; i < memReports.length; i++) {
markdown = markdown.concat(
" | ",
memReports[i].artifact_name,
" | ",
memReports[i].peak_memory,
" |\n"
);
}
return markdown;
};
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ export interface DiffCell {
percentage: number;
}

export interface MemoryReport {
artifact_name: string;
peak_memory: string;
}

export interface MemoryReports {
memory_reports: MemoryReport[];
}

export type SortCriterion = keyof DiffCircuit;
export type SortOrder = "asc" | "desc";

Expand Down
15 changes: 15 additions & 0 deletions tests/memory_report.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as fs from "fs";

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

const srcContent = fs.readFileSync("tests/mocks/mem_report.json", "utf8");

const memReports = memoryReports(srcContent);

describe("Markdown format", () => {
it("should generate markdown format", () => {
expect(memReports.length).toBeGreaterThan(0);
const markdown = formatMemoryReport(memReports);
expect(markdown.length).toBeGreaterThan(0);
});
});
Loading

0 comments on commit b0319af

Please sign in to comment.