Skip to content

Commit c4e602f

Browse files
authored
Merge pull request #1244 from carver/use-perf-counter
Prefer time.perf_counter or monotonic over time()
2 parents 040674c + 81f176f commit c4e602f

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

p2p/kademlia.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def __init__(self, start: int, end: int) -> None:
132132
self.end = end
133133
self.nodes: List[Node] = []
134134
self.replacement_cache: List[Node] = []
135-
self.last_updated = time.time()
135+
self.last_updated = time.monotonic()
136136

137137
@property
138138
def midpoint(self) -> int:
@@ -181,7 +181,7 @@ def add(self, node: Node) -> Node:
181181
node at the head of the list (i.e. the least recently seen), which should be evicted if it
182182
fails to respond to a ping.
183183
"""
184-
self.last_updated = time.time()
184+
self.last_updated = time.monotonic()
185185
if node in self.nodes:
186186
self.nodes.remove(node)
187187
self.nodes.append(node)
@@ -213,13 +213,13 @@ class RoutingTable:
213213
logger = logging.getLogger("p2p.kademlia.RoutingTable")
214214

215215
def __init__(self, node: Node) -> None:
216-
self._initialized_at = time.time()
216+
self._initialized_at = time.monotonic()
217217
self.this_node = node
218218
self.buckets = [KBucket(0, k_max_node_id)]
219219

220220
def get_random_nodes(self, count: int) -> Iterator[Node]:
221221
if count > len(self):
222-
if time.time() - self._initialized_at > 30:
222+
if time.monotonic() - self._initialized_at > 30:
223223
self.logger.warn(
224224
"Cannot get %d nodes as RoutingTable contains only %d nodes", count, len(self))
225225
count = len(self)
@@ -245,7 +245,7 @@ def split_bucket(self, index: int) -> None:
245245

246246
@property
247247
def idle_buckets(self) -> List[KBucket]:
248-
idle_cutoff_time = time.time() - k_idle_bucket_refresh_interval
248+
idle_cutoff_time = time.monotonic() - k_idle_bucket_refresh_interval
249249
return [b for b in self.buckets if b.last_updated < idle_cutoff_time]
250250

251251
@property

scripts/benchmark/utils/meters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class TimedResult(NamedTuple):
1414

1515

1616
def time_call(fn: Callable[..., Any]=None) -> TimedResult:
17-
start = time.time()
17+
start = time.perf_counter()
1818
return_value = fn()
19-
duration = time.time() - start
19+
duration = time.perf_counter() - start
2020
return TimedResult(duration=duration, wrapped_value=return_value)

tests/trinity/integration/test_lightchain_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ def geth_process(geth_command_arguments):
117117

118118

119119
def wait_for_socket(ipc_path, timeout=10):
120-
start = time.time()
121-
while time.time() < start + timeout:
120+
start = time.monotonic()
121+
while time.monotonic() < start + timeout:
122122
try:
123123
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
124124
sock.connect(str(ipc_path))

trinity/sync/sharding/service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __init__(self, shard: Shard, peer_pool: PeerPool, token: CancelToken=None) -
7777

7878
self.collation_hashes_at_peer: Dict[ShardingPeer, Set[Hash32]] = defaultdict(set)
7979

80-
self.start_time = time.time()
80+
self.start_time = time.monotonic()
8181

8282
subscription_msg_types: Set[Type[Command]] = {Collations, GetCollations, NewCollationHashes}
8383

@@ -204,4 +204,4 @@ async def _handle_new_collation_hashes(self, peer: ShardingPeer, msg: Dict[str,
204204

205205
def get_current_period(self) -> int:
206206
# TODO: get this from main chain
207-
return int((time.time() - self.start_time) // COLLATION_PERIOD)
207+
return int((time.monotonic() - self.start_time) // COLLATION_PERIOD)

trinity/utils/ipc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def wait_for_ipc(ipc_path: pathlib.Path, timeout: int=10) -> None:
1313
Waits up to ``timeout`` seconds for the IPC socket file to appear at path
1414
``ipc_path``, or raises a :exc:`TimeoutError` otherwise.
1515
"""
16-
start_at = time.time()
17-
while time.time() - start_at < timeout:
16+
start_at = time.monotonic()
17+
while time.monotonic() - start_at < timeout:
1818
if ipc_path.exists():
1919
return
2020
else:

trinity/utils/timer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def __init__(self, auto_start: bool = True) -> None:
99
self.start()
1010

1111
def start(self) -> None:
12-
self._start = time.time()
12+
self._start = time.perf_counter()
1313

1414
@property
1515
def elapsed(self) -> float:
16-
return time.time() - self._start
16+
return time.perf_counter() - self._start

0 commit comments

Comments
 (0)