From 024f4f3c2bbb7c41c7368a55948d09bd20ff1824 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 14 Jun 2018 02:17:56 +0200 Subject: [PATCH 1/2] bpo-33718: regrtest: use format_duration() to display failed tests * Enhance also format_duration(): work on integers and rounds towards infinity (math.ceil). * Write unit tests on format_duration() --- Lib/test/libregrtest/runtest_mp.py | 2 +- Lib/test/libregrtest/utils.py | 25 ++++++++++++++++--------- Lib/test/test_regrtest.py | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/Lib/test/libregrtest/runtest_mp.py b/Lib/test/libregrtest/runtest_mp.py index f7fa10cf551a4e..907451cf63116c 100644 --- a/Lib/test/libregrtest/runtest_mp.py +++ b/Lib/test/libregrtest/runtest_mp.py @@ -200,7 +200,7 @@ def get_running(workers): if (ok not in (CHILD_ERROR, INTERRUPTED) and test_time >= PROGRESS_MIN_TIME and not regrtest.ns.pgo): - text += ' (%.0f sec)' % test_time + text += ' (%s)' % format_duration(test_time) elif ok == CHILD_ERROR: text = '%s (%s)' % (text, test_time) running = get_running(workers) diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 85049cb06b3649..27e02cd8e86fd8 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -1,19 +1,26 @@ import os.path +import math import textwrap def format_duration(seconds): - if seconds < 1.0: - return '%.0f ms' % (seconds * 1e3) - if seconds < 60.0: - return '%.0f sec' % seconds + ms, seconds = math.modf(seconds) + seconds = math.ceil(seconds) + ms = math.ceil(ms * 1e3) - minutes, seconds = divmod(seconds, 60.0) - hours, minutes = divmod(minutes, 60.0) + minutes, seconds = divmod(seconds, 60) + hours, minutes = divmod(minutes, 60) + parts = [] if hours: - return '%.0f hour %.0f min' % (hours, minutes) - else: - return '%.0f min %.0f sec' % (minutes, seconds) + parts.append('%s hour' % hours) + if minutes: + parts.append('%s min' % minutes) + if seconds: + parts.append('%s sec' % seconds) + if ms or (not parts): + parts.append('%s ms' % ms) + parts = parts[:2] + return ' '.join(parts) def removepy(names): diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index 10411522dffb02..af332ad15d922a 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -19,6 +19,7 @@ import unittest from test import libregrtest from test import support +from test.libregrtest import utils Py_DEBUG = hasattr(sys, 'getobjects') @@ -980,5 +981,29 @@ def test_bug(self): failed=testname, rerun=testname) +class TestUtils(unittest.TestCase): + def test_format_duration(self): + self.assertEqual(utils.format_duration(0), + '0 ms') + self.assertEqual(utils.format_duration(1e-9), + '1 ms') + self.assertEqual(utils.format_duration(10e-3), + '10 ms') + self.assertEqual(utils.format_duration(1.5), + '1 sec 500 ms') + self.assertEqual(utils.format_duration(1), + '1 sec') + self.assertEqual(utils.format_duration(2 * 60), + '2 min') + self.assertEqual(utils.format_duration(2 * 60 + 1), + '2 min 1 sec') + self.assertEqual(utils.format_duration(3 * 3600), + '3 hour') + self.assertEqual(utils.format_duration(3 * 3600 + 2 * 60 + 1), + '3 hour 2 min') + self.assertEqual(utils.format_duration(3 * 3600 + 1), + '3 hour 1 sec') + + if __name__ == '__main__': unittest.main() From 459f2be99487c7ebe9d8cb6e01b1f6a2053ebd06 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 14 Jun 2018 08:43:58 +0200 Subject: [PATCH 2/2] only round once --- Lib/test/libregrtest/utils.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 27e02cd8e86fd8..d36bf9196626db 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -4,12 +4,11 @@ def format_duration(seconds): - ms, seconds = math.modf(seconds) - seconds = math.ceil(seconds) - ms = math.ceil(ms * 1e3) - + ms = math.ceil(seconds * 1e3) + seconds, ms = divmod(ms, 1000) minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) + parts = [] if hours: parts.append('%s hour' % hours) @@ -17,8 +16,11 @@ def format_duration(seconds): parts.append('%s min' % minutes) if seconds: parts.append('%s sec' % seconds) - if ms or (not parts): + if ms: parts.append('%s ms' % ms) + if not parts: + return '0 ms' + parts = parts[:2] return ' '.join(parts)