Skip to content

Commit

Permalink
BORGSTORE_LATENCY [us], see #24
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Sep 11, 2024
1 parent 4506810 commit 668b7a2
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/borgstore/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from collections import Counter
from contextlib import contextmanager
import os
import time
from typing import Iterator, Optional

Expand Down Expand Up @@ -47,6 +48,8 @@ def __init__(self, url: Optional[str] = None, backend: Optional[BackendBase] = N
raise ValueError("You need to give a backend instance or a backend url.")
self.backend = backend
self._stats: Counter = Counter()
# this is to emulate higher latency than the backend actually offers:
self.latency = float(os.environ.get("BORGSTORE_LATENCY", "0")) / 1e6 # [us]

def __repr__(self):
return f"<Store(url={self.url!r}, levels={self.levels!r})>"
Expand Down Expand Up @@ -76,6 +79,8 @@ def _stats_updater(self, key):
# do not use this in generators!
start = time.perf_counter_ns()
yield
if self.latency:
time.sleep(self.latency)
end = time.perf_counter_ns()
self._stats[f"{key}_calls"] += 1
self._stats[f"{key}_time"] += end - start
Expand Down Expand Up @@ -212,7 +217,13 @@ def _list(self, name: str, deleted: bool = False) -> Iterator[ItemInfo]:
# as the backend.list method only supports non-recursive listing and
# also returns directories/namespaces we introduced for nesting, we do the
# recursion here (and also we do not yield directory names from here).
start = time.perf_counter_ns()
backend_list_iterator = self.backend.list(name)
if self.latency:
# we add the simulated latency once per backend.list iteration, not per element.
time.sleep(self.latency)
end = time.perf_counter_ns()
self._stats["list_time"] += end - start
while True:
start = time.perf_counter_ns()
try:
Expand Down

0 comments on commit 668b7a2

Please sign in to comment.