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 14 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
103 changes: 100 additions & 3 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,11 +25,14 @@ pub struct BuildArgs {
#[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,
}

impl BuildArgs {
pub fn run(self, config: &Config) -> Result<()> {
let compile_info = compile_workspace(
let compile_info: dojo_lang::scarb_internal::CompileInfo = compile_workspace(
config,
fabrobles92 marked this conversation as resolved.
Show resolved Hide resolved
CompileOpts { include_targets: vec![], exclude_targets: vec![TargetKind::TEST] },
)?;
Expand All @@ -44,6 +50,15 @@ impl BuildArgs {
builtin_plugins.push(BuiltinPlugins::Unity);
}

if self.stats {
let target_dir = &compile_info.target_dir;
let contracts_statistics: Vec<ContractStatistics> =
get_contract_statistics_for_dir(target_dir)
fabrobles92 marked this conversation as resolved.
Show resolved Hide resolved
.context(format!("Error getting contracts stats"))?;
let table = create_stats_table(contracts_statistics);
table.printstd()
}

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

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

// Add table headers
table.add_row(Row::new(vec![
Cell::new_align("Contract", format::Alignment::CENTER),
fabrobles92 marked this conversation as resolved.
Show resolved Hide resolved
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 +125,60 @@ mod tests {
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
let contracts_statistics = vec![
fabrobles92 marked this conversation as resolved.
Show resolved Hide resolved
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.add_row(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
let table = create_stats_table(contracts_statistics);
fabrobles92 marked this conversation as resolved.
Show resolved Hide resolved

// 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