Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement latency and bandwidth emulation #33

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 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,10 @@ 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 additional latency to what the backend actually offers:
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] -> [bytes/s]

def __repr__(self):
return f"<Store(url={self.url!r}, levels={self.levels!r})>"
Expand All @@ -73,16 +78,28 @@ def close(self) -> None:

@contextmanager
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_volume(key)
start = time.perf_counter_ns()
yield
be_needed_ns = time.perf_counter_ns() - start
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
if remaining_time > 0.0:
time.sleep(remaining_time)
end = time.perf_counter_ns()
self._stats[f"{key}_calls"] += 1
self._stats[f"{key}_time"] += end - start

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 Expand Up @@ -212,7 +229,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