Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Support --print-detailed-resources parameter in cairo-run #1688

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading