Fix output of small floats in results

For now we have output of small floats in scientific notation:
3.79085540771e-05
Fix that to 0.000037. Also floats in fixed place will output as:
  0.0000001 -> 0.0
  0.000000 -> 0.0
  37 -> 37.0
  1.0000001 -> 1.0
  1.0000011 -> 1.000001
  1.0000019 -> 1.000002

This can be made in different ways like:
  "%f" % float(decimal.Decimal(num).normalize())
or in more weird way but I choose that one (see in the patch) like
the most flat, simple and explicit.

Change-Id: I4b9cdbb01f3f8e2eeaedc9d33e1203247ea79f89
This commit is contained in:
Roman Vasilets 2016-07-13 16:35:22 +03:00
parent 5e426e0766
commit ff58044892
4 changed files with 70 additions and 5 deletions

View File

@ -438,9 +438,9 @@ class TaskCommands(object):
print()
print(_("Load duration: %s") %
result["info"]["load_duration"])
rutils.format_float_to_str(result["info"]["load_duration"]))
print(_("Full duration: %s") %
result["info"]["full_duration"])
rutils.format_float_to_str(result["info"]["full_duration"]))
print("\nHINTS:")
print(_("* To plot HTML graphics with this data, run:"))

View File

@ -631,3 +631,25 @@ class LockedDict(dict):
def clear(self, *args, **kwargs):
self._check_is_unlocked()
return super(LockedDict, self).clear(*args, **kwargs)
def format_float_to_str(num):
"""Format number into human-readable float format.
More precise it convert float into the string and remove redundant
zeros from the floating part.
It will format the number by the following examples:
0.0000001 -> 0.0
0.000000 -> 0.0
37 -> 37.0
1.0000001 -> 1.0
1.0000011 -> 1.000001
1.0000019 -> 1.000002
:param num: Number to be formatted
:return: string representation of the number
"""
num_str = "%f" % num
float_part = num_str.split(".")[1].rstrip("0") or "0"
return num_str.split(".")[0] + "." + float_part

View File

@ -25,6 +25,7 @@ import six
from rally.common.i18n import _
from rally.common import logging
from rally.common import objects
from rally.common import utils
from rally import consts
from rally import exceptions
from rally import osclients
@ -112,9 +113,12 @@ class ResultConsumer(object):
load_duration = max(self.load_finished_at - self.load_started_at, 0)
LOG.info("Load duration is: %s" % load_duration)
LOG.info("Full runner duration is: %s" % self.runner.run_duration)
LOG.info("Full duration is %s" % (self.finish - self.start))
LOG.info("Load duration is: %s" % utils.format_float_to_str(
load_duration))
LOG.info("Full runner duration is: %s" %
utils.format_float_to_str(self.runner.run_duration))
LOG.info("Full duration is %s" % utils.format_float_to_str(
self.finish - self.start))
self.task.append_results(self.key, {
"raw": self.results,

View File

@ -548,3 +548,42 @@ class LockedDictTestCase(test.TestCase):
self.assertEqual(({"foo": "bar", "spam": {"a": ("b", {"c": "d"})}},),
args)
self.assertEqual({"memo": "foo_memo"}, kw)
@ddt.ddt
class FloatFormatterTestCase(test.TestCase):
@ddt.data(
{
"num_float": 0,
"num_str": "0.0"
},
{
"num_float": 37,
"num_str": "37.0"
},
{
"num_float": 0.0000001,
"num_str": "0.0"
},
{
"num_float": 0.000000,
"num_str": "0.0"
},
{
"num_float": 1.0000001,
"num_str": "1.0"
},
{
"num_float": 1.0000011,
"num_str": "1.000001"
},
{
"num_float": 1.0000019,
"num_str": "1.000002"
}
)
@ddt.unpack
def test_format_float_to_str(self, num_float, num_str):
self.assertEquals(num_str, utils.format_float_to_str(num_float))