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(sozo): add --stats flag to output stats of the build artifacts #1799

Merged
Merged
Show file tree
Hide file tree
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
44 changes: 43 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/sozo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
prettytable-rs = "0.10.0"
anyhow.workspace = true
async-trait.workspace = true
cairo-lang-compiler.workspace = true
Expand Down
100 changes: 98 additions & 2 deletions bin/sozo/src/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use anyhow::Result;
use anyhow::{Context, Result};
use clap::Args;
use dojo_bindgen::{BuiltinPlugins, PluginManager};
use dojo_lang::scarb_internal::compile_workspace;
use prettytable::format::consts::FORMAT_NO_LINESEP_WITH_TITLE;
use prettytable::{format, Cell, Row, Table};
use scarb::core::{Config, TargetKind};
use scarb::ops::CompileOpts;
use sozo_ops::statistics::{get_contract_statistics_for_dir, ContractStatistics};

#[derive(Debug, Args)]
pub struct BuildArgs {
Expand All @@ -22,6 +25,9 @@
#[arg(long)]
#[arg(help = "Output directory.", default_value = "bindings")]
pub bindings_output: String,

#[arg(long, help = "Display statistics about the compiled contracts")]
pub stats: bool,

Check warning on line 30 in bin/sozo/src/commands/build.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/build.rs#L30

Added line #L30 was not covered by tests
}

impl BuildArgs {
Expand All @@ -44,6 +50,14 @@
builtin_plugins.push(BuiltinPlugins::Unity);
}

if self.stats {
let target_dir = &compile_info.target_dir;
let contracts_statistics = get_contract_statistics_for_dir(target_dir)
.context(format!("Error getting contracts stats"))?;
let table = create_stats_table(contracts_statistics);
table.printstd()
}

Check warning on line 59 in bin/sozo/src/commands/build.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/build.rs#L59

Added line #L59 was not covered by tests

// Custom plugins are always empty for now.
let bindgen = PluginManager {
profile_name: compile_info.profile_name,
Expand All @@ -65,11 +79,41 @@
}
}

fn create_stats_table(contracts_statistics: Vec<ContractStatistics>) -> Table {
let mut table = Table::new();
fabrobles92 marked this conversation as resolved.
Show resolved Hide resolved
table.set_format(*FORMAT_NO_LINESEP_WITH_TITLE);

// Add table headers
table.set_titles(Row::new(vec![
Cell::new_align("Contract", format::Alignment::CENTER),
Cell::new_align("Bytecode size (felts)", format::Alignment::CENTER),
Cell::new_align("Class size (bytes)", format::Alignment::CENTER),
]));

for contract_stats in contracts_statistics {
// Add table rows
let contract_name = contract_stats.contract_name;
let number_felts = contract_stats.number_felts;
let file_size = contract_stats.file_size;

table.add_row(Row::new(vec![
Cell::new_align(&contract_name, format::Alignment::LEFT),
Cell::new_align(format!("{}", number_felts).as_str(), format::Alignment::RIGHT),
Cell::new_align(format!("{}", file_size).as_str(), format::Alignment::RIGHT),
]));
}

table
}

#[cfg(test)]
mod tests {
use dojo_test_utils::compiler::build_test_config;
use prettytable::format::consts::FORMAT_NO_LINESEP_WITH_TITLE;
use prettytable::{format, Cell, Row, Table};
use sozo_ops::statistics::ContractStatistics;

use super::BuildArgs;
use super::{create_stats_table, BuildArgs};

#[test]
fn build_example_with_typescript_and_unity_bindings() {
Expand All @@ -80,8 +124,60 @@
typescript: true,
unity: true,
typescript_v2: true,
stats: true,
};
let result = build_args.run(&config);
assert!(result.is_ok());
}

#[test]
fn test_create_stats_table() {
// Arrange
fabrobles92 marked this conversation as resolved.
Show resolved Hide resolved
let contracts_statistics = vec![
ContractStatistics {
contract_name: "Test1".to_string(),
number_felts: 33,
file_size: 33,
},
ContractStatistics {
contract_name: "Test2".to_string(),
number_felts: 43,
file_size: 24,
},
ContractStatistics {
contract_name: "Test3".to_string(),
number_felts: 36,
file_size: 12,
},
];

let mut expected_table = Table::new();
expected_table.set_format(*FORMAT_NO_LINESEP_WITH_TITLE);
expected_table.set_titles(Row::new(vec![
Cell::new_align("Contract", format::Alignment::CENTER),
Cell::new_align("Bytecode size (felts)", format::Alignment::CENTER),
Cell::new_align("Class size (bytes)", format::Alignment::CENTER),
]));
expected_table.add_row(Row::new(vec![
Cell::new_align("Test1", format::Alignment::LEFT),
Cell::new_align(format!("{}", 33).as_str(), format::Alignment::RIGHT),
Cell::new_align(format!("{}", 33).as_str(), format::Alignment::RIGHT),
]));
expected_table.add_row(Row::new(vec![
Cell::new_align("Test2", format::Alignment::LEFT),
Cell::new_align(format!("{}", 43).as_str(), format::Alignment::RIGHT),
Cell::new_align(format!("{}", 24).as_str(), format::Alignment::RIGHT),
]));
expected_table.add_row(Row::new(vec![
Cell::new_align("Test3", format::Alignment::LEFT),
Cell::new_align(format!("{}", 36).as_str(), format::Alignment::RIGHT),
Cell::new_align(format!("{}", 12).as_str(), format::Alignment::RIGHT),
]));

// Act
fabrobles92 marked this conversation as resolved.
Show resolved Hide resolved
let table = create_stats_table(contracts_statistics);

// Assert
assert_eq!(table, expected_table, "Tables mismatch")
}
}
1 change: 1 addition & 0 deletions crates/sozo/ops/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod execute;
pub mod migration;
pub mod model;
pub mod register;
pub mod statistics;
pub mod utils;

#[cfg(test)]
Expand Down
Loading
Loading