Skip to content

Commit

Permalink
small refactor, more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Sep 11, 2024
1 parent 7d52636 commit eb7ea4f
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/borgstore/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def __init__(self, url: Optional[str] = None, backend: Optional[BackendBase] = N
self.backend = backend
self._stats: Counter = Counter()
# this is to emulate additional latency to what the backend actually offers:
self.latency = float(os.environ.get("BORGSTORE_LATENCY", "0")) / 1e6 # [us]
self.latency = float(os.environ.get("BORGSTORE_LATENCY", "0")) / 1e6 # [us] -> [s]
# this is to emulate less bandwidth than what the backend actually offers:
self.bandwidth = float(os.environ.get("BORGSTORE_BANDWIDTH", "0")) / 8 # [bits/s]
self.bandwidth = float(os.environ.get("BORGSTORE_BANDWIDTH", "0")) / 8 # [bits/s] -> [bytes/s]

def __repr__(self):
return f"<Store(url={self.url!r}, levels={self.levels!r})>"
Expand Down Expand Up @@ -80,11 +80,11 @@ def close(self) -> None:
def _stats_updater(self, key):
"""update call counters and overall times, also emulate latency and bandwidth"""
# do not use this in generators!
volume_before = self._stats.get(f"{key}_volume", 0)
volume_before = self._stats_get_volume(key)
start = time.perf_counter_ns()
yield
be_needed_ns = time.perf_counter_ns() - start
volume_after = self._stats.get(f"{key}_volume", 0)
volume_after = self._stats_get_volume(key)
volume = volume_after - volume_before
emulated_time = self.latency + (0 if not self.bandwidth else float(volume) / self.bandwidth)
remaining_time = emulated_time - be_needed_ns / 1e9
Expand All @@ -97,6 +97,9 @@ def _stats_updater(self, key):
def _stats_update_volume(self, key, amount):
self._stats[f"{key}_volume"] += amount

def _stats_get_volume(self, key):
return self._stats.get(f"{key}_volume", 0)

@property
def stats(self):
"""
Expand Down

0 comments on commit eb7ea4f

Please sign in to comment.