Skip to content

Commit

Permalink
feat: display gas used for unit tests (FuelLabs#4178)
Browse files Browse the repository at this point in the history
## Description
closes FuelLabs#2411.

This PR adds used gas information to the output of `forc test`. An
example run can be seen below:

<img width="534" alt="Screen Shot 2023-02-24 at 4 47 29 PM"
src="https://user-images.githubusercontent.com/20915464/221198624-a3bdb264-2182-4b9d-9497-7d8cfe84132b.png">

This is done automatically without any flags. I felt like this should be
the default way but open to suggestions if you feel like it should be
behind a flag like `--gas-usage`.

Also by accessing `TestResult` structs new used gas field, we can add
this information into vscode plugin just like FuelLabs#513. We do not have a
line by line gas cast information just yet but we could show the whole
test's used gas info next to the run button or something in vscode.
Thoughts @JoshuaBatty @sdankel

Please let me know if anyone has any requests around the flag and the
way we are printing the used gas. Maybe @nfurfaro could have some ideas
as the prime user of `forc-test` 😄
## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.
  • Loading branch information
kayagokalp authored Feb 27, 2023
1 parent 67b29ac commit 64f7340
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 14 additions & 1 deletion forc-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ pub struct TestResult {
pub name: String,
/// The time taken for the test to execute.
pub duration: std::time::Duration,
/// The span for the function declaring this tests.
/// The span for the function declaring this test.
pub span: Span,
/// The resulting state after executing the test function.
pub state: vm::state::ProgramState,
/// The required state of the VM for this test to pass.
pub condition: pkg::TestPassCondition,
/// Emitted `Recipt`s during the execution of the test.
pub logs: Vec<fuel_tx::Receipt>,
/// Gas used while executing this test.
pub gas_used: u64,
}

const TEST_METADATA_SEED: u64 = 0x7E57u64;
Expand Down Expand Up @@ -204,6 +206,16 @@ impl<'a> PackageTests {
let (state, duration, receipts) =
exec_test(&pkg_with_tests.bytecode, offset, test_setup);

let gas_used = *receipts
.iter()
.find_map(|receipt| match receipt {
tx::Receipt::ScriptResult { gas_used, .. } => Some(gas_used),
_ => None,
})
.ok_or_else(|| {
anyhow::anyhow!("missing used gas information from test execution")
})?;

// Only retain `Log` and `LogData` receipts.
let logs = receipts
.into_iter()
Expand All @@ -222,6 +234,7 @@ impl<'a> PackageTests {
state,
condition,
logs,
gas_used,
})
})
.collect::<anyhow::Result<_>>()?;
Expand Down
5 changes: 3 additions & 2 deletions forc/src/cli/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ fn print_tested_pkg(pkg: &TestedPackage, test_print_opts: &TestPrintOpts) -> Res
false => ("FAILED", Colour::Red),
};
info!(
" test {} ... {} ({:?})",
" test {} ... {} ({:?}, {} gas)",
test.name,
color.paint(state),
test.duration
test.duration,
test.gas_used
);

// If logs are enabled, print them.
Expand Down

0 comments on commit 64f7340

Please sign in to comment.