Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into holic/abi-ts-extension
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Oct 22, 2024
2 parents 5f8cea5 + d5c2700 commit 701700d
Show file tree
Hide file tree
Showing 20 changed files with 589 additions and 575 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-panthers-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/gas-report": patch
---

Gas report output now include contract name as part of the `file` to help with stable ordering when sorting output.
4 changes: 2 additions & 2 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandModule } from "yargs";

import gasReport from "@latticexyz/gas-report";
import { command as gasReport } from "@latticexyz/gas-report/internal";
import { command as abiTs } from "@latticexyz/abi-ts/internal";

import build from "./build";
Expand All @@ -21,7 +21,7 @@ export const commands: CommandModule<any, any>[] = [
build,
deploy,
devnode,
gasReport as CommandModule,
gasReport,
hello,
tablegen,
worldgen,
Expand Down
2 changes: 1 addition & 1 deletion packages/gas-report/bin/gas-report.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node

// workaround for https://github.com/pnpm/pnpm/issues/1801
import "../dist/gas-report.js";
import "../dist/bin/gas-report.js";
6 changes: 3 additions & 3 deletions packages/gas-report/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/index.js"
"./internal": "./dist/exports/internal.js"
},
"typesVersions": {
"*": {
"index": [
"./dist/index.d.ts"
"internal": [
"./dist/exports/internal.d.ts"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import gasReport from ".";
import { command as gasReport } from "../command";

// Load .env file into process.env
import * as dotenv from "dotenv";
Expand Down
81 changes: 81 additions & 0 deletions packages/gas-report/ts/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import type { CommandModule } from "yargs";
import { readFileSync } from "fs";
import chalk from "chalk";
import { runGasReport } from "./runGasReport";
import { printGasReport } from "./printGasReport";
import { saveGasReport } from "./saveGasReport";
import { CommandOptions, GasReport } from "./common";

/**
* Print the gas report to the console, save it to a file and compare it to a previous gas report if provided.
*
* Requires foundry to be installed. Inherit from GasReporter and use startGasReport/endGasReport to measure gas used.
*
* ```solidity
* contract MyContractTest is Test, GasReporter {
* function testBuffer() public pure {
* startGasReport("allocate a buffer");
* Buffer buffer = Buffer_.allocate(32);
* endGasReport();
*
* bytes32 value = keccak256("some data");
*
* startGasReport("append 32 bytes to a buffer");
* buffer.append(value);
* endGasReport();
* }
* }
* ```
*/

export const command: CommandModule<CommandOptions, CommandOptions> = {
command: "gas-report",

describe: "Create a gas report",

builder(yargs) {
return yargs.options({
save: { type: "string", desc: "Save the gas report to a file" },
compare: { type: "string", desc: "Compare to an existing gas report" },
stdin: {
type: "boolean",
desc: "Parse the gas report logs from stdin instead of running an internal test command",
},
});
},

async handler(options) {
let gasReport: GasReport;
try {
gasReport = await runGasReport(options);
} catch (error) {
console.error(error);
setTimeout(() => process.exit());
return;
}

// If this gas report should be compared to an existing one, load the existing one
let { compare } = options;
if (compare) {
try {
const compareGasReport: GasReport = JSON.parse(readFileSync(compare, "utf8"));
// Merge the previous gas report with the new one
gasReport = gasReport.map((entry) => {
const prevEntry = compareGasReport.find((e) => e.file === entry.file && e.name === entry.name);
return { ...entry, prevGasUsed: prevEntry?.gasUsed };
});
} catch {
console.log(chalk.red(`Gas report to compare not found: ${compare}`));
compare = undefined;
}
}

// Print gas report
printGasReport(gasReport, compare);

// Save gas report to file if requested
if (options.save) saveGasReport(gasReport, options.save);

process.exit(0);
},
};
16 changes: 16 additions & 0 deletions packages/gas-report/ts/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type GasReportEntry = {
file: string;
test: string;
name: string;
gasUsed: number;
prevGasUsed?: number;
};

export type GasReport = GasReportEntry[];

export type CommandOptions = {
path: string[];
save?: string;
compare?: string;
stdin?: boolean;
};
1 change: 1 addition & 0 deletions packages/gas-report/ts/exports/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "../command";
211 changes: 0 additions & 211 deletions packages/gas-report/ts/index.ts

This file was deleted.

27 changes: 27 additions & 0 deletions packages/gas-report/ts/printGasReport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import chalk from "chalk";
import { table, getBorderCharacters } from "table";
import { GasReport } from "./common";

export function printGasReport(gasReport: GasReport, compare?: string) {
if (compare) console.log(chalk.bold(`Gas report compared to ${compare}`));

const headers = [
chalk.bold("File"),
chalk.bold("Test"),
chalk.bold("Name"),
chalk.bold("Gas used"),
...(compare ? [chalk.bold("Prev gas used"), chalk.bold("Difference")] : []),
];

const values = gasReport.map((entry) => {
const diff = entry.prevGasUsed ? entry.gasUsed - entry.prevGasUsed : 0;
const diffEntry = diff > 0 ? chalk.red(`+${diff}`) : diff < 0 ? chalk.green(`${diff}`) : diff;
const compareColumns = compare ? [entry.prevGasUsed ?? "n/a", diffEntry] : [];
const gasUsedEntry = diff > 0 ? chalk.red(entry.gasUsed) : diff < 0 ? chalk.green(entry.gasUsed) : entry.gasUsed;
return [entry.file, entry.test, entry.name, gasUsedEntry, ...compareColumns];
});

const rows = [headers, ...values];

console.log(table(rows, { border: getBorderCharacters("norc") }));
}
Loading

0 comments on commit 701700d

Please sign in to comment.