Skip to content

Commit

Permalink
make time measurements monotonic (#231)
Browse files Browse the repository at this point in the history
- `time.time()` is the system time. "it's great" (tm)
  however it is not reliable for measuring time spent.
  it gives you the "current" time, but oh... time can move backwards
  (thanks NTP).

- `time.monotonic()` is a monotonic time source. "it's great" (tm)
  it only goes forward, making measurements a peach.
  however it cannot be used as a reference point.
  • Loading branch information
dnozay committed Sep 14, 2021
1 parent 8e99918 commit 531da78
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 8 additions & 3 deletions xmlrunner/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def __init__(self, xml_doc, parent_context=None):
"""
self.xml_doc = xml_doc
self.parent = parent_context
self._start_time = self._stop_time = 0
self._start_time_m = 0
self._stop_time_m = 0
self._stop_time = 0
self.counters = {}

def element_tag(self):
Expand All @@ -72,12 +74,15 @@ def begin(self, tag, name):
"""
self.element = self.xml_doc.createElement(tag)
self.element.setAttribute('name', replace_nontext(name))
self._start_time = time.time()
self._start_time = time.monotonic()

def end(self):
"""Closes this context (started with a call to `begin`) and creates an
attribute for each counter and another for the elapsed time.
"""
# time.monotonic is reliable for measuring differences, not affected by NTP
self._stop_time_m = time.monotonic()
# time.time is used for reference point
self._stop_time = time.time()
self.element.setAttribute('time', self.elapsed_time())
self.element.setAttribute('timestamp', self.timestamp())
Expand Down Expand Up @@ -120,7 +125,7 @@ def elapsed_time(self):
"""Returns the time the context took to run between the calls to
`begin()` and `end()`, in seconds.
"""
return format(self._stop_time - self._start_time, '.3f')
return format(self._stop_time_m - self._start_time_m, '.3f')

def timestamp(self):
"""Returns the time the context ended as ISO-8601-formatted timestamp.
Expand Down
4 changes: 2 additions & 2 deletions xmlrunner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ def run(self, test):
self.stream.writeln(result.separator2)

# Execute tests
start_time = time.time()
start_time = time.monotonic()
test(result)
stop_time = time.time()
stop_time = time.monotonic()
time_taken = stop_time - start_time

# Print results
Expand Down

0 comments on commit 531da78

Please sign in to comment.