Skip to content

Commit

Permalink
Feat: Support --print-detailed-resources parameter in cairo-run (#1688)
Browse files Browse the repository at this point in the history
This PR introduces an optional CLI parameter --print-detailed-resources
for scarb cairo-run extension that prints detailed used resources
report:

![image](https://github.com/user-attachments/assets/2378ae24-b5be-4461-a21c-7f731e05c738)

---------

Signed-off-by: maciektr <[email protected]>
Co-authored-by: maciektr <[email protected]>
  • Loading branch information
maciejka and maciektr authored Oct 28, 2024
1 parent cfd945a commit d29ecc4
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions extensions/scarb-cairo-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ struct Args {
#[arg(long, default_value_t = false)]
print_full_memory: bool,

/// Print detailed resources.
#[arg(long, default_value_t = false)]
print_resource_usage: bool,

/// Do not rebuild the package.
#[arg(long, default_value_t = false)]
no_build: bool,
Expand Down Expand Up @@ -150,6 +154,7 @@ fn main_inner(ui: &Ui, args: Args) -> Result<()> {
result,
print_full_memory: args.print_full_memory,
gas_defined: available_gas.is_defined(),
detailed_resources: args.print_resource_usage,
});

Ok(())
Expand Down Expand Up @@ -228,6 +233,7 @@ struct Summary {
result: RunResultStarknet,
print_full_memory: bool,
gas_defined: bool,
detailed_resources: bool,
}

impl Message for Summary {
Expand Down Expand Up @@ -272,6 +278,18 @@ impl Message for Summary {
}
println!("]");
}

if self.detailed_resources {
let resources = self.result.used_resources.basic_resources;
let sorted_builtins = sort_by_value(&resources.builtin_instance_counter);
let sorted_syscalls = sort_by_value(&self.result.used_resources.syscalls);

println!("Resources:");
println!("\tsteps: {}", resources.n_steps);
println!("\tmemory holes: {}", resources.n_memory_holes);
println!("\tbuiltins: ({})", format_items(&sorted_builtins));
println!("\tsyscalls: ({})", format_items(&sorted_syscalls));
}
}

fn structured<S: Serializer>(self, _ser: S) -> Result<S::Ok, S::Error>
Expand All @@ -282,6 +300,28 @@ impl Message for Summary {
}
}

fn sort_by_value<'a, K, V, M>(map: M) -> Vec<(&'a K, &'a V)>
where
M: IntoIterator<Item = (&'a K, &'a V)>,
V: Ord,
{
let mut sorted: Vec<_> = map.into_iter().collect();
sorted.sort_by(|a, b| b.1.cmp(a.1));
sorted
}

fn format_items<K, V>(items: &[(K, V)]) -> String
where
K: std::fmt::Debug,
V: std::fmt::Display,
{
items
.iter()
.map(|(key, value)| format!("{key:?}: {value}"))
.collect::<Vec<String>>()
.join(", ")
}

enum GasLimit {
Disabled,
Unlimited,
Expand Down

0 comments on commit d29ecc4

Please sign in to comment.