bittorrent-benchmarks/benchmarks/core/utils.py

28 lines
723 B
Python
Raw Normal View History

2024-10-30 23:22:09 +00:00
import random
from pathlib import Path
2024-11-01 21:07:08 +00:00
from typing import Callable, Iterator, Tuple
2024-10-30 23:22:09 +00:00
# A Sampler samples without replacement from [0, ..., n].
2024-11-01 21:07:08 +00:00
type Sampler = Callable[[int], Iterator[int]]
2024-10-30 23:22:09 +00:00
# A DataGenerator generates files for experiments.
2024-11-01 21:07:08 +00:00
type DataGenerator[TInitialMetadata] = Callable[[], Tuple[TInitialMetadata, Path]]
2024-10-30 23:22:09 +00:00
def sample(n: int) -> Iterator[int]:
2024-11-01 21:07:08 +00:00
"""Samples without replacement using a basic Fisher-Yates shuffle."""
2024-10-30 23:22:09 +00:00
p = list(range(0, n))
for i in range(n - 1):
j = i + random.randint(0, n - i)
tmp = p[j]
p[j], p[j + 1] = p[j + 1], tmp
yield p[i]
2024-11-01 21:07:08 +00:00
def kilobytes(n: int) -> int:
return n * 1024
def megabytes(n: int) -> int:
return kilobytes(n) * 1024