Skip to content

Commit e73b024

Browse files
authored
Merge pull request #2490 from jsiirola/timer-tests-win10
Improve `common.timing` test robustness
2 parents 413f8f6 + bede9b9 commit e73b024

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

pyomo/common/tests/test_timing.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,25 @@ def test_TicTocTimer_tictoc(self):
167167

168168
time.sleep(SLEEP)
169169

170-
ref = time.perf_counter()
171170
with capture_output() as out:
171+
ref = time.perf_counter()
172172
delta = timer.toc()
173173
self.assertAlmostEqual(ref - start_time, delta, delta=RES)
174-
# entering / leaving the context manager can take non-trivial
175-
# time on some platforms (up to 0.02 on Windows)
176-
self.assertAlmostEqual(0.01, timer.toc(None), delta=RES)
177174
self.assertRegex(
178175
out.getvalue(),
179176
r'\[\+ [.0-9]+\] .* in test_TicTocTimer_tictoc'
180177
)
181-
ref = time.perf_counter()
182178
with capture_output() as out:
179+
# entering / leaving the context manager can take non-trivial
180+
# time on some platforms (up to 0.03 on Windows / Python 3.10)
181+
self.assertAlmostEqual(
182+
time.perf_counter() - ref, timer.toc(None), delta=RES)
183+
self.assertEqual(out.getvalue(), '')
184+
185+
with capture_output() as out:
186+
ref = time.perf_counter()
183187
total = timer.toc(delta=False)
184-
self.assertAlmostEqual(ref - abs_time, total, delta=RES)
188+
self.assertAlmostEqual(ref - start_time, total, delta=RES)
185189
self.assertRegex(
186190
out.getvalue(),
187191
r'\[ [.0-9]+\] .* in test_TicTocTimer_tictoc'

pyomo/common/timing.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class was constructed). Note: timing logged using logger.info
228228
level (int): an optional logging output level.
229229
230230
"""
231-
self._lastTime = default_timer()
231+
self._lastTime = self._loadTime = default_timer()
232232
if msg is _NotSpecified:
233233
msg = "Resetting the tic/toc delta timer"
234234
if msg is not None:
@@ -258,9 +258,9 @@ def toc(self, msg=_NotSpecified, *args, delta=True,
258258
*args (tuple): optional positional arguments used for
259259
%-formatting the `msg`
260260
delta (bool): print out the elapsed wall clock time since
261-
the last call to :meth:`tic` or :meth:`toc`
262-
(``True`` (default)) or since the module was first
263-
loaded (``False``).
261+
the last call to :meth:`tic` (``False``) or since the
262+
most recent call to either :meth:`tic` or :meth:`toc`
263+
(``True`` (default)).
264264
ostream (FILE): an optional output stream (overrides the ostream
265265
provided when the class was constructed).
266266
logger (Logger): an optional output stream using the python
@@ -298,6 +298,9 @@ class was constructed). Note: timing logged using `level`
298298
data = (ans, msg)
299299
else:
300300
ans = now - self._loadTime
301+
# Even though we are reporting the cumulative time, we will
302+
# still reset the delta timer.
303+
self._lastTime = now
301304
if msg is not None:
302305
fmt = "[%8.2f] %s"
303306
data = (ans, msg)

0 commit comments

Comments
 (0)