8000 Use timeit.default_timer instead of time.time (#106) · sonlinux/client_python@bde3259 · GitHub
[go: up one dir, main page]

Skip to content

Commit bde3259

Browse files
acdhabrian-brazil
authored andcommitted
Use timeit.default_timer instead of time.time (prometheus#106)
On Python 3, this uses time.perf_counter. On Python 2, it will use time.clock on Windows and time.time everywhere else
1 parent 4197142 commit bde3259

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

prometheus_client/bridge/graphite.py

100644100755
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import socket
77
import time
88
import threading
9+
from timeit import default_timer
910

1011
from .. import core
1112

@@ -26,10 +27,10 @@ def __init__(self, pusher, interval, prefix):
2627
self._prefix = prefix
2728

2829
def run(self):
29-
wait_until = time.time()
30+
wait_until = default_timer()
3031
while True:
3132
while True:
32-
now = time.time()
33+
now = default_timer()
3334
if now >= wait_until:
3435
# May need to skip some pushes.
3536
while wait_until < now:
@@ -44,14 +45,14 @@ def run(self):
4445

4546

4647
class GraphiteBridge(object):
47-
def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _time=time):
48+
def __init__(self, address, registry=core.REGISTRY, timeout_seconds=30, _timer=default_timer):
4849
self._address = address
4950
self._registry = registry
5051
self._timeout = timeout_seconds
51-
self._time = _time
52+
self._timer = _timer
5253

5354
def push(self, prefix=''):
54-
now = int(self._time.time())
55+
now = int(self._timer())
5556
output = []
5657

5758
prefixstr = ''

prometheus_client/core.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import copy
66
import math
77
import re
8-
import time
98
import types
9+
from timeit import default_timer
1010

1111
try:
1212
from BaseHTTPServer import BaseHTTPRequestHandler
@@ -466,7 +466,7 @@ def set(self, value):
466466

467467
def set_to_current_time(self):
468468
'''Set gauge to the current unixtime.'''
469-
self.set(time.time())
469+
self.set(default_timer())
470470

471471
def track_inprogress(self):
472472
'''Track inprogress blocks of code or functions.
@@ -654,11 +654,11 @@ def __init__(self, histogram):
654654
self._histogram = histogram
655655

656656
def __enter__(self):
657-
self._start = time.time()
657+
self._start = default_timer()
658658

659659
def __exit__(self, typ, value, traceback):
660660
# Time can go backwards.
661-
self._histogram.observe(max(time.time() - self._start, 0))
661+
self._histogram.observe(max(default_timer() - self._start, 0))
662662

663663
def __call__(self, f):
664664
def wrapped(func, *args, **kwargs):
@@ -708,11 +708,11 @@ def __init__(self, summary):
708708
self._summary = summary
709709

710710
def __enter__(self):
711-
self._start = time.time()
711+
self._start = default_timer()
712712

713713
def __exit__(self, typ, value, traceback):
714714
# Time can go backwards.
715-
self._summary.observe(max(time.time() - self._start, 0))
715+
self._summary.observe(max(default_timer() - self._start, 0))
716716

717717
def __call__(self, f):
718718
def wrapped(func, *args, **kwargs):
@@ -726,11 +726,11 @@ def __init__(self, gauge):
726726
self._gauge = gauge
727727

728728
def __enter__(self):
729-
self._start = time.time()
729+
self._start = default_timer()
730730

731731
def __exit__(self, typ, value, traceback):
732732
# Time can go backwards.
733-
self._gauge.set(max(time.time() - self._start, 0))
733+
self._gauge.set(max(default_timer() - self._start, 0))
734734

735735
def __call__(self, f):
736736
def wrapped(func, *args, **kwargs):

tests/test_graphite_bridge.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
from prometheus_client import Counter, CollectorRegistry
99
from prometheus_client.bridge.graphite import GraphiteBridge
1010

11-
class FakeTime(object):
12-
def time(self):
13-
return 1434898897.5
11+
def fake_timer():
12+
return 1434898897.5
1413

1514
class TestGraphiteBridge(unittest.TestCase):
1615
def setUp(self):
@@ -27,10 +26,10 @@ def run(self):
2726
server.socket.close()
2827
self.t = ServingThread()
2928
self.t.start()
30-
29+
3130
# Explicitly use localhost as the target host, since connecting to 0.0.0.0 fails on Windows
3231
address = ('localhost', server.server_address[1])
33-
self.gb = GraphiteBridge(address, self.registry, _time=FakeTime())
32+
self.gb = GraphiteBridge(address, self.registry, _timer=fake_timer)
3433

3534
def test_nolabels(self):
3635
counter = Counter('c', 'help', registry=self.registry)

0 commit comments

Comments
 (0)
0