From d4a101013fe9fc15ce6d7d7d9e0dc5771753e57a Mon Sep 17 00:00:00 2001 From: data-pup Date: Tue, 7 Aug 2018 20:27:29 -0400 Subject: [PATCH 1/2] Added remaining stats to the `garbage` command. --- analyze/analyses/garbage.rs | 50 +++++++++++++++++--- twiggy/tests/expectations/garbage | 2 +- twiggy/tests/expectations/garbage_json | 2 +- twiggy/tests/expectations/garbage_top_2 | 4 +- twiggy/tests/expectations/garbage_top_2_json | 1 + twiggy/tests/tests.rs | 10 ++++ 6 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 twiggy/tests/expectations/garbage_top_2_json diff --git a/analyze/analyses/garbage.rs b/analyze/analyses/garbage.rs index 995dadff..ef96c200 100644 --- a/analyze/analyses/garbage.rs +++ b/analyze/analyses/garbage.rs @@ -35,12 +35,22 @@ impl traits::Emit for Garbage { ]); } - if self.items.len() > self.limit { - table.add_row(vec![ - "...".to_string(), - "...".to_string(), - format!("... and {} more", self.items.len() - self.limit), - ]); + match self + .items + .iter() + .skip(self.limit) + .map(|id| &items[*id]) + .fold((0, 0), |(size, cnt), item| (size + item.size(), cnt + 1)) + { + (size, cnt) if cnt > 0 => { + let size_percent = f64::from(size) / f64::from(items.size()) * 100.0; + table.add_row(vec![ + size.to_string(), + format!("{:.2}%", size_percent), + format!("... and {} more", cnt), + ]); + } + _ => {} } let total_size: u32 = self.items.iter().map(|&id| items[id].size()).sum(); @@ -48,7 +58,7 @@ impl traits::Emit for Garbage { table.add_row(vec![ total_size.to_string(), format!("{:.2}%", total_percent), - "Σ".to_string(), + format!("Σ [{} Total Rows]", self.items.len()), ]); write!(dest, "{}", &table)?; @@ -71,6 +81,32 @@ impl traits::Emit for Garbage { obj.field("size_percent", size_percent)?; } + match self + .items + .iter() + .skip(self.limit) + .map(|id| &items[*id]) + .fold((0, 0), |(size, cnt), item| (size + item.size(), cnt + 1)) + { + (size, cnt) if cnt > 0 => { + let name = format!("... and {} more", cnt); + let size_percent = (f64::from(size)) / (f64::from(items.size())) * 100.0; + let mut obj = arr.object()?; + obj.field("name", name.as_str())?; + obj.field("bytes", size)?; + obj.field("size_percent", size_percent)?; + } + _ => {} + } + + let total_name = format!("Σ [{} Total Rows]", self.items.len()); + let total_size: u32 = self.items.iter().map(|&id| items[id].size()).sum(); + let total_size_percent = (f64::from(total_size)) / (f64::from(items.size())) * 100.0; + let mut obj = arr.object()?; + obj.field("name", total_name.as_str())?; + obj.field("bytes", total_size)?; + obj.field("size_percent", total_size_percent)?; + Ok(()) } diff --git a/twiggy/tests/expectations/garbage b/twiggy/tests/expectations/garbage index 199a184b..6e614122 100644 --- a/twiggy/tests/expectations/garbage +++ b/twiggy/tests/expectations/garbage @@ -9,4 +9,4 @@ 1 ┊ 0.51% ┊ func[0] 1 ┊ 0.51% ┊ func[1] 1 ┊ 0.51% ┊ func[2] - 43 ┊ 21.83% ┊ Σ + 43 ┊ 21.83% ┊ Σ [9 Total Rows] diff --git a/twiggy/tests/expectations/garbage_json b/twiggy/tests/expectations/garbage_json index f23df561..526544a6 100644 --- a/twiggy/tests/expectations/garbage_json +++ b/twiggy/tests/expectations/garbage_json @@ -1 +1 @@ -[{"name":"unusedAddThreeNumbers","bytes":11,"size_percent":5.583756345177665},{"name":"unusedAddOne","bytes":8,"size_percent":4.060913705583756},{"name":"type[2]","bytes":7,"size_percent":3.5532994923857872},{"name":"type[1]","bytes":5,"size_percent":2.5380710659898478},{"name":"unusedChild","bytes":5,"size_percent":2.5380710659898478},{"name":"type[0]","bytes":4,"size_percent":2.030456852791878},{"name":"func[0]","bytes":1,"size_percent":0.5076142131979695},{"name":"func[1]","bytes":1,"size_percent":0.5076142131979695},{"name":"func[2]","bytes":1,"size_percent":0.5076142131979695}] \ No newline at end of file +[{"name":"unusedAddThreeNumbers","bytes":11,"size_percent":5.583756345177665},{"name":"unusedAddOne","bytes":8,"size_percent":4.060913705583756},{"name":"type[2]","bytes":7,"size_percent":3.5532994923857872},{"name":"type[1]","bytes":5,"size_percent":2.5380710659898478},{"name":"unusedChild","bytes":5,"size_percent":2.5380710659898478},{"name":"type[0]","bytes":4,"size_percent":2.030456852791878},{"name":"func[0]","bytes":1,"size_percent":0.5076142131979695},{"name":"func[1]","bytes":1,"size_percent":0.5076142131979695},{"name":"func[2]","bytes":1,"size_percent":0.5076142131979695},{"name":"Σ [9 Total Rows]","bytes":43,"size_percent":21.82741116751269}] diff --git a/twiggy/tests/expectations/garbage_top_2 b/twiggy/tests/expectations/garbage_top_2 index d2bd5d92..b12e4851 100644 --- a/twiggy/tests/expectations/garbage_top_2 +++ b/twiggy/tests/expectations/garbage_top_2 @@ -2,5 +2,5 @@ ───────┼────────┼────────────────────── 11 ┊ 5.58% ┊ unusedAddThreeNumbers 8 ┊ 4.06% ┊ unusedAddOne - ... ┊ ... ┊ ... and 7 more - 43 ┊ 21.83% ┊ Σ + 24 ┊ 12.18% ┊ ... and 7 more + 43 ┊ 21.83% ┊ Σ [9 Total Rows] diff --git a/twiggy/tests/expectations/garbage_top_2_json b/twiggy/tests/expectations/garbage_top_2_json new file mode 100644 index 00000000..abd90984 --- /dev/null +++ b/twiggy/tests/expectations/garbage_top_2_json @@ -0,0 +1 @@ +[{"name":"unusedAddThreeNumbers","bytes":11,"size_percent":5.583756345177665},{"name":"unusedAddOne","bytes":8,"size_percent":4.060913705583756},{"name":"... and 7 more","bytes":24,"size_percent":12.18274111675127},{"name":"Σ [9 Total Rows]","bytes":43,"size_percent":21.82741116751269}] diff --git a/twiggy/tests/tests.rs b/twiggy/tests/tests.rs index 63d75638..9dc458e3 100644 --- a/twiggy/tests/tests.rs +++ b/twiggy/tests/tests.rs @@ -419,6 +419,16 @@ test!( "json" ); +test!( + garbage_top_2_json, + "garbage", + "./fixtures/garbage.wasm", + "-f", + "json", + "-n", + "2" +); + test!( monos_json, "monos", From ce4bb08b7a6fe5ed9afc438b08bc33c31ec9089e Mon Sep 17 00:00:00 2001 From: data-pup Date: Thu, 9 Aug 2018 19:00:34 -0400 Subject: [PATCH 2/2] Control flow cleanup. --- analyze/analyses/garbage.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/analyze/analyses/garbage.rs b/analyze/analyses/garbage.rs index ef96c200..21cf0516 100644 --- a/analyze/analyses/garbage.rs +++ b/analyze/analyses/garbage.rs @@ -81,22 +81,19 @@ impl traits::Emit for Garbage { obj.field("size_percent", size_percent)?; } - match self + let (total_size, total_cnt) = self .items .iter() .skip(self.limit) .map(|id| &items[*id]) - .fold((0, 0), |(size, cnt), item| (size + item.size(), cnt + 1)) - { - (size, cnt) if cnt > 0 => { - let name = format!("... and {} more", cnt); - let size_percent = (f64::from(size)) / (f64::from(items.size())) * 100.0; - let mut obj = arr.object()?; - obj.field("name", name.as_str())?; - obj.field("bytes", size)?; - obj.field("size_percent", size_percent)?; - } - _ => {} + .fold((0, 0), |(size, cnt), item| (size + item.size(), cnt + 1)); + if total_cnt > 0 { + let name = format!("... and {} more", total_cnt); + let total_size_percent = (f64::from(total_size)) / (f64::from(items.size())) * 100.0; + let mut obj = arr.object()?; + obj.field("name", name.as_str())?; + obj.field("bytes", total_size)?; + obj.field("size_percent", total_size_percent)?; } let total_name = format!("Σ [{} Total Rows]", self.items.len());