Skip to content

Commit

Permalink
update docs in accordance to foundry-rs/foundry#9791 and foundry-rs/f…
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosnacks committed Jan 30, 2025
1 parent fd5a204 commit 79d1881
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/cheatcodes/gas-snapshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ function snapshotGasLastCall(string calldata group, string calldata name) extern

`snapshotGas*` cheatcodes allow you to capture gas usage in your tests. This can be useful to track how much gas your logic is consuming. You can capture the gas usage of the last call by name, capture an arbitrary numerical value by name, or start and stop a snapshot capture of the current gas usage by name.

In order to strictly compare gas usage across test runs, set the `FORGE_SNAPSHOT_CHECK` environment variable to `true` before running your tests. This will compare the gas usage of your tests against the last snapshot and fail if the gas usage has changed. By default the snapshots directory will be newly created and its contents removed before each test run to ensure no stale data is present.
In order to strictly compare gas usage across test runs you can do one of the following:

It is intended that the `snapshots` directory created when using the `snapshotGas*` cheatcodes is checked into version control. This allows you to track changes in gas usage over time and compare gas usage during code reviews.
- Set the `FORGE_SNAPSHOT_CHECK=true` environment variable
- Set `gas_snapshot_check` in `foundry.toml`
- Pass `--gas-snapshot-check=true`

By default this is not enabled and passing `--gas-snapshot-check=false` will override all others.

When running `forge clean` the `snapshots` directory will be deleted.
This will compare the gas usage of your tests against the last snapshot and fail if the gas usage has changed.

It is intended that the `snapshots` directory created when using the `snapshotGas*` cheatcodes is checked into version control. This allows you to track changes in gas usage over time and compare gas usage during code reviews.

### Examples

Expand All @@ -68,7 +74,6 @@ contract SnapshotGasTest is Test {

Capturing the gas usage of multiple sections of code that modify the internal state:


```solidity
contract SnapshotGasTest is Test {
uint256 public slot0;
Expand Down

0 comments on commit 79d1881

Please sign in to comment.