-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gas-report): include contract name in file of output (#3317)
- Loading branch information
Showing
20 changed files
with
589 additions
and
575 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "../command"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") })); | ||
} |
Oops, something went wrong.