foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.27k stars 1.74k forks source link

feat: add `--file` flag to `forge test --gas-report` to write to file #8781

Closed 3esmit closed 1 week ago

3esmit commented 2 months ago

Component

Forge

Describe the feature you would like

I would like that forge test --gas-report to be saved in a file, similar to what forge snapshot does.

This could be either a different flag in forge test --gas-report like forge-test --gas-report-file or probably the better would be forge snapshot --gas-report, so the gas report also generates a file called .gas-report with the table output of forge-test --gas-report

Additional context

forge test --gas-report seems more complete than forge snapshot, because it actually tests the gas costs for different scenarios, for example when storage is loaded, or empty, etc, or different call stacks, and create a very useful report to see how the changes impact in gas usage.

Currently I am using a custom commands on package.json to achieve this functionality:

  "scripts": {
"gas-report": "forge test --gas-report 2>&1 | (tee /dev/tty | awk '/Suite result:/ {found=1; buffer=\"\"; next} found && !/Ran/ {buffer=buffer $0 ORS} /Ran/ {found=0} END {printf \"%s\", buffer}' > .gas-report)",
"adorno": "pnpm prettier:write && forge fmt && forge snapshot && pnpm gas-report"
} 

Recently, forge changed a word in the test report, from "Test result" to "Suite result" that broke that command entirely in many repositories I am working. See https://github.com/vacp2p/foundry-template/pull/31

This feature would avoid having to tweak with awk, and give peace of mind on both sides.

zerosnacks commented 2 months ago

Hi @3esmit I think this issue can be resolved if forge test --gas-report --json yields a predictable and correct JSON serialized representation of the table layout. This would allow you to just pipe to file using builtins.

See: https://github.com/foundry-rs/foundry/issues/8789

3esmit commented 1 month ago

@zerosnacks yes, if that command outputed the data in the table it would fix the issue.

I need something to keep track of all gas changes.. Ideally, gas-snapshot and gas-report become one single file, and both in json, so we can have some tool (maybe a plugin in coverage stuff) that would follow the gas cost of operations.

3esmit commented 1 week ago

Closed by #9063