paycheck: fix errors around percentage / formatting of zero sizes
This fixes two problems, both having to do with histogram generation:
* When the total number of elements is zero, paycheck would crash due to
a division by zero; further, even if the crash is fixed (returning,
say, None) the histogram will contain a meaningless value in
parenthesis, which we might as well drop. Both are fixed here.
* When some size (say, bytes) is zero, its formatter
(bytes-to-human-readable) returns None, which shows up as is in the
final report. This should be checked and avoided.
BUG=None
TEST=Crash fixed; None percentage/formatted value omitted.
Change-Id: I8bb5fbc47e1cde9dcbee7f7b96bcb63ef3a0935e
Reviewed-on: https://chromium-review.googlesource.com/172046
Reviewed-by: Don Garrett <dgarrett@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
diff --git a/scripts/update_payload/checker.py b/scripts/update_payload/checker.py
index 549ffcb..8a86619 100644
--- a/scripts/update_payload/checker.py
+++ b/scripts/update_payload/checker.py
@@ -67,7 +67,11 @@
Returns:
A string 'x (y)' where x = str(value) and y = format_func(value).
"""
- return '%s (%s)' % (value, format_func(value))
+ ret = str(value)
+ formatted_str = format_func(value)
+ if formatted_str:
+ ret += ' (%s)' % formatted_str
+ return ret
def _AddHumanReadableSize(size):
diff --git a/scripts/update_payload/format_utils.py b/scripts/update_payload/format_utils.py
index 2c82f32..2c3775c 100644
--- a/scripts/update_payload/format_utils.py
+++ b/scripts/update_payload/format_utils.py
@@ -27,9 +27,13 @@
min_precision: minimum precision for fractional percentage
max_precision: maximum precision for fractional percentage
Returns:
- Percentage string.
+ Percentage string, or None if percent cannot be computed (i.e. total is
+ zero).
"""
+ if total == 0:
+ return None
+
percent = 0
precision = min(min_precision, max_precision)
factor = 10 ** precision
diff --git a/scripts/update_payload/histogram.py b/scripts/update_payload/histogram.py
index a5ddac4..9916329 100644
--- a/scripts/update_payload/histogram.py
+++ b/scripts/update_payload/histogram.py
@@ -101,11 +101,13 @@
bar_len = count * self.scale / self.total
hist_bar = '|%s|' % ('#' * bar_len).ljust(self.scale)
- line = '%s %s %s (%s)' % (
+ line = '%s %s %s' % (
str(key).ljust(self.max_key_len),
hist_bar,
- self.formatter(count),
- format_utils.NumToPercent(count, self.total))
+ self.formatter(count))
+ percent_str = format_utils.NumToPercent(count, self.total)
+ if percent_str:
+ line += ' (%s)' % percent_str
hist_lines.append(line)
return '\n'.join(hist_lines)