On Friday, November 15, 2013 09:06:51 AM Damien Lespiau wrote: > It's a bit hard to parse raw seconds, so make those time values easier > to read while trying to preserve roughly enough relevant precision to be > useful. > > It gives strings like: > > 22.4ms > 7.798s > 42s > 7min 25s > ... > > Signed-off-by: Damien Lespiau <[email protected]> > --- > framework/summary.py | 36 ++++++++++++++++++++++++++++++++++-- > 1 file changed, 34 insertions(+), 2 deletions(-) > > diff --git a/framework/summary.py b/framework/summary.py > index 8fbe2a8..c42ee03 100644 > --- a/framework/summary.py > +++ b/framework/summary.py > @@ -38,6 +38,38 @@ __all__ = [ > ] > > > +INTERVALS = (1, 60, 3600, 86400, 604800, 2419200, 29030400) > +NAMES = ('s', 'min', 'hr', 'day', 'week', 'month', 'year') > + > +# Gives a human readable elapsed time > +# @amount is a string with a number of seconds > +def humanize_time(amount): > + result = [] > + > + if amount == 'None': > + return 'None'
the python idiom is 'if not amount: return None'
> +
> + amount_f = float(amount)
> + if (amount_f < 1):
> + amount_ms = amount_f * 1000
> + if amount_ms < 1:
> + return "< 1ms"
> + return "%.1fms" % amount_ms
> +
> + # if < 10s, consider ms are important
> + if amount_f < 10:
> + return "%.03fs" % amount_f
> +
> + amount = int(amount_f)
> +
> + for i in range(len(NAMES) - 1, -1, -1):
in python2 avoid range, use xrange instead (xrange is range's lazy cousin) in
python3 range is xrange.
> + a = amount / INTERVALS[i]
> + if a > 0:
> + result.append("%d%s" % (a, NAMES[i]))
> + amount -= a * INTERVALS[i]
> +
> + return " ".join(result)
> +
on a more practicle note, why roll all of this instead of just using
datetime.timedelta?
> class HTMLIndex(list):
> """
> Builds HTML output to be passed to the index mako template, which will
> be @@ -420,7 +452,7 @@ class Summary:
>
> with open(path.join(destination, each.name, "index.html"), 'w')
> as out: out.write(testindex.render(name=each.name,
> - time=each.time_elapsed,
> +
> time=humanize_time(each.time_elapsed), options=each.options,
> glxinfo=each.glxinfo,
> lspci=each.lspci))
> @@ -447,7 +479,7 @@ class Summary:
> # disapear at somepoint
> env=value.get('environment', None),
> returncode=value.get('returncode', 'None'),
> - time=value.get('time', 'None'),
> + time=humanize_time(value.get('time', 'None')),
> info=value.get('info', 'None'),
> traceback=value.get('traceback', 'None'),
> command=value.get('command', 'None'),
signature.asc
Description: This is a digitally signed message part.
_______________________________________________ Piglit mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/piglit
