Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 531da78

Browse files
authored
make time measurements monotonic (#231)
- `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.
1 parent 8e99918 commit 531da78

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

‎xmlrunner/builder.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def __init__(self, xml_doc, parent_context=None):
5858
"""
5959
self.xml_doc = xml_doc
6060
self.parent = parent_context
61-
self._start_time = self._stop_time = 0
61+
self._start_time_m = 0
62+
self._stop_time_m = 0
63+
self._stop_time = 0
6264
self.counters = {}
6365

6466
def element_tag(self):
@@ -72,12 +74,15 @@ def begin(self, tag, name):
7274
"""
7375
self.element = self.xml_doc.createElement(tag)
7476
self.element.setAttribute('name', replace_nontext(name))
75-
self._start_time = time.time()
77+
self._start_time = time.monotonic()
7678

7779
def end(self):
7880
"""Closes this context (started with a call to `begin`) and creates an
7981
attribute for each counter and another for the elapsed time.
8082
"""
83+
# time.monotonic is reliable for measuring differences, not affected by NTP
84+
self._stop_time_m = time.monotonic()
85+
# time.time is used for reference point
8186
self._stop_time = time.time()
8287
self.element.setAttribute('time', self.elapsed_time())
8388
self.element.setAttribute('timestamp', self.timestamp())
@@ -120,7 +125,7 @@ def elapsed_time(self):
120125
"""Returns the time the context took to run between the calls to
121126
`begin()` and `end()`, in seconds.
122127
"""
123-
return format(self._stop_time - self._start_time, '.3f')
128+
return format(self._stop_time_m - self._start_time_m, '.3f')
124129

125130
def timestamp(self):
126131
"""Returns the time the context ended as ISO-8601-formatted timestamp.

‎xmlrunner/runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ def run(self, test):
6262
self.stream.writeln(result.separator2)
6363

6464
# Execute tests
65-
start_time = time.time()
65+
start_time = time.monotonic()
6666
test(result)
67-
stop_time = time.time()
67+
stop_time = time.monotonic()
6868
time_taken = stop_time - start_time
6969

7070
# Print results

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /