Skip to content

Commit

Permalink
Merge pull request #36 from amanjeev/amanjeev/optional-greater-than
Browse files Browse the repository at this point in the history
summarize (feature): adds percent-above cli argument
  • Loading branch information
wesleywiser authored Apr 30, 2019
2 parents 26e6ddb + ceb18d8 commit e0d7945
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

## Unreleased
### Added
- `summarize`: New CLI argument `percent-above` for `summarize` crate ([GH-32])

## [0.2.1] - 2019-04-12

## [0.2.0] - 2019-04-10


[0.2.1]: https://github.com/rust-lang/measureme/releases/tag/0.2.1
[0.2.0]: https://github.com/rust-lang/measureme/releases/tag/0.2.0

[GH-32]: https://github.com/rust-lang/measureme/issues/32
27 changes: 26 additions & 1 deletion summarize/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ struct Opt {
/// Writes the analysis to a json file next to <file_prefix> instead of stdout
#[structopt(long = "json")]
json: bool,

/// Filter the output to items whose self-time is greater than this value
#[structopt(short = "pa", long = "percent-above", default_value = "0.0")]
percent_above: f64,
}

fn main() -> Result<(), Box<std::error::Error>> {
Expand All @@ -34,6 +38,16 @@ fn main() -> Result<(), Box<std::error::Error>> {
return Ok(());
}

let percent_above = opt.percent_above;
//cannot be greater than 100% or less than 0%
if percent_above > 100.0 {
eprintln!("Percentage of total time cannot be more than 100.0");
std::process::exit(1);
} else if percent_above < 0.0 {
eprintln!("Percentage of total time cannot be less than 0.0");
std::process::exit(1);
}

//order the results by descending self time
results.query_data.sort_by(|l, r| r.self_time.cmp(&l.self_time));

Expand All @@ -50,12 +64,19 @@ fn main() -> Result<(), Box<std::error::Error>> {
]);

let total_time = results.total_time.as_nanos() as f64;
let mut percent_total_time: f64 = 0.0;

for query_data in results.query_data {

let curr_percent = (query_data.self_time.as_nanos() as f64) / total_time * 100.0;
if curr_percent < percent_above { break } //no need to run entire loop if filtering by % time

percent_total_time = percent_total_time + curr_percent;

table.add_row(row![
query_data.label,
format!("{:.2?}", query_data.self_time),
format!("{:.3}", ((query_data.self_time.as_nanos() as f64) / total_time) * 100.0),
format!("{:.3}", curr_percent),
format!("{}", query_data.invocation_count),
format!("{}", query_data.number_of_cache_hits),
format!("{:.2?}", query_data.blocked_time),
Expand All @@ -67,5 +88,9 @@ fn main() -> Result<(), Box<std::error::Error>> {

println!("Total cpu time: {:?}", results.total_time);

if percent_above != 0.0 {
println!("Filtered results account for {:.3}% of total time.", percent_total_time);
}

Ok(())
}

0 comments on commit e0d7945

Please sign in to comment.