Skip to content

Commit 8517806

Browse files
committed
Display gas report table
1 parent 1c1e984 commit 8517806

File tree

6 files changed

+133
-20
lines changed

6 files changed

+133
-20
lines changed

Cargo.lock

Lines changed: 58 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,4 @@ tracing-subscriber = { version = "0.3.20", features = ["env-filter"] }
130130
tracing = "0.1.41"
131131
tracing-chrome = "0.7.2"
132132
tracing-log = "0.2.0"
133+
comfy-table = "7"

crates/forge-runner/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ strum = "0.27"
4949
strum_macros = "0.27"
5050
scarb-oracle-hint-service.workspace = true
5151
tracing.workspace = true
52+
comfy-table.workspace = true

crates/forge-runner/src/gas/report.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
use crate::gas::stats::GasStats;
22
use cheatnet::trace_data::{CallTrace, CallTraceNode};
3+
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
4+
use comfy_table::{Cell, Color, Table};
35
use debugging::ContractName as DebuggingContractName;
46
use debugging::ContractsDataStore;
57
use starknet_api::core::{ClassHash, EntryPointSelector};
68
use starknet_api::execution_resources::GasVector;
79
use std::collections::BTreeMap;
10+
use std::fmt;
11+
use std::fmt::Display;
812

913
type ContractName = String;
1014
type Selector = String;
@@ -117,3 +121,48 @@ fn get_selector(contracts_data: &ContractsDataStore, selector: EntryPointSelecto
117121
.0
118122
.clone()
119123
}
124+
125+
impl Display for ReportData {
126+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127+
for (name, contract_info) in &self.0 {
128+
if !contract_info.functions.is_empty() {
129+
let table = format_table_output(contract_info, name);
130+
writeln!(f, "\n{table}")?;
131+
}
132+
}
133+
Ok(())
134+
}
135+
}
136+
137+
pub fn format_table_output(contract_info: &ContractInfo, name: &ContractName) -> Table {
138+
let mut table = Table::new();
139+
table.apply_modifier(UTF8_ROUND_CORNERS);
140+
141+
table.set_header(vec![
142+
Cell::new(format!("{name} Contract")).fg(Color::Magenta),
143+
]);
144+
table.add_row(vec![
145+
Cell::new("Function Name"),
146+
Cell::new("Min").fg(Color::Green),
147+
Cell::new("Avg").fg(Color::Yellow),
148+
Cell::new("Standard Deviation").fg(Color::Yellow),
149+
Cell::new("Max").fg(Color::Red),
150+
Cell::new("# Calls").fg(Color::Cyan),
151+
]);
152+
153+
contract_info
154+
.functions
155+
.iter()
156+
.for_each(|(selector, report_data)| {
157+
table.add_row(vec![
158+
Cell::new(selector),
159+
Cell::new(report_data.gas_stats.min.to_string()).fg(Color::Green),
160+
Cell::new(report_data.gas_stats.mean.to_string()).fg(Color::Yellow),
161+
Cell::new(report_data.gas_stats.std_deviation.to_string()).fg(Color::Yellow),
162+
Cell::new(report_data.gas_stats.max.to_string()).fg(Color::Red),
163+
Cell::new(report_data.n_calls.to_string()),
164+
]);
165+
});
166+
167+
table
168+
}

crates/forge-runner/src/messages.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::forge_config::ForgeTrackedResource;
1+
use crate::forge_config::{ForgeTrackedResource, OutputConfig};
22
use crate::test_case_summary::{AnyTestCaseSummary, FuzzingStatistics, TestCaseSummary};
33
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
44
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::UsedResources;
@@ -39,12 +39,13 @@ pub struct TestResultMessage {
3939
fuzzer_report: String,
4040
gas_usage: String,
4141
used_resources: String,
42+
gas_report: String,
4243
}
4344

4445
impl TestResultMessage {
4546
pub fn new(
4647
test_result: &AnyTestCaseSummary,
47-
show_detailed_resources: bool,
48+
output_config: &OutputConfig,
4849
tracked_resource: ForgeTrackedResource,
4950
) -> Self {
5051
let name = test_result
@@ -75,19 +76,28 @@ impl TestResultMessage {
7576
String::new()
7677
};
7778

78-
let gas_usage = match test_result {
79+
let (gas_usage, gas_report) = match test_result {
7980
AnyTestCaseSummary::Single(TestCaseSummary::Passed { gas_info, .. }) => {
80-
format!(
81-
" (l1_gas: ~{}, l1_data_gas: ~{}, l2_gas: ~{})",
82-
gas_info.gas_used.l1_gas,
83-
gas_info.gas_used.l1_data_gas,
84-
gas_info.gas_used.l2_gas
81+
let gas_report = if output_config.gas_report {
82+
format!("{}", gas_info.report_data)
83+
} else {
84+
String::new()
85+
};
86+
87+
(
88+
format!(
89+
" (l1_gas: ~{}, l1_data_gas: ~{}, l2_gas: ~{})",
90+
gas_info.gas_used.l1_gas,
91+
gas_info.gas_used.l1_data_gas,
92+
gas_info.gas_used.l2_gas
93+
),
94+
gas_report,
8595
)
8696
}
87-
_ => String::new(),
97+
_ => (String::new(), String::new()),
8898
};
8999

90-
let used_resources = match (show_detailed_resources, &test_result) {
100+
let used_resources = match (output_config.detailed_resources, &test_result) {
91101
(true, AnyTestCaseSummary::Single(TestCaseSummary::Passed { used_resources, .. })) => {
92102
format_detailed_resources(used_resources, tracked_resource)
93103
}
@@ -104,6 +114,7 @@ impl TestResultMessage {
104114
fuzzer_report,
105115
gas_usage,
106116
used_resources,
117+
gas_report,
107118
}
108119
}
109120

@@ -140,10 +151,11 @@ impl Message for TestResultMessage {
140151

141152
let fuzzer_report = &self.fuzzer_report;
142153
let gas_usage = &self.gas_usage;
154+
let gas_report = &self.gas_report;
143155
let used_resources = &self.used_resources;
144156

145157
format!(
146-
"{result_header} {result_name}{fuzzer_report}{gas_usage}{used_resources}{result_msg}{result_debug_trace}"
158+
"{result_header} {result_name}{fuzzer_report}{gas_usage}{used_resources}{result_msg}{result_debug_trace}{gas_report}"
147159
)
148160
}
149161

crates/forge/src/run_tests/test_target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub async fn run_for_test_target(
7171
if !result.is_interrupted() {
7272
let test_result_message = TestResultMessage::new(
7373
&result,
74-
forge_config.output_config.detailed_resources,
74+
&forge_config.output_config,
7575
forge_config.test_runner_config.tracked_resource,
7676
);
7777
ui.println(&test_result_message);

0 commit comments

Comments
 (0)