import argparse
import statistics
import sys
import time
from pathlib import Path
from mrrc import MARCReader
def read_all(path):
start = time.perf_counter()
reader = MARCReader(str(path))
count = 0
while reader.read_record() is not None:
count += 1
elapsed = time.perf_counter() - start
return count, elapsed
def bench_file(path, repeat, include_first):
runs = []
count = None
for i in range(repeat + (0 if include_first else 1)):
n, elapsed = read_all(path)
if count is None:
count = n
if not include_first:
continue elif n != count:
sys.exit(f"{path}: record count changed between runs ({count} vs {n})")
runs.append(n / elapsed)
return count, runs
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("files", nargs="+", type=Path, help=".mrc files to read")
parser.add_argument(
"--repeat", type=int, default=5, help="measured repetitions per file (default 5)"
)
parser.add_argument(
"--include-first",
action="store_true",
help="measure the first repetition instead of discarding it as cache warm-up",
)
args = parser.parse_args()
for path in args.files:
if not path.exists():
sys.exit(f"No such file: {path}")
count, runs = bench_file(path, args.repeat, args.include_first)
median = statistics.median(runs)
spread = (max(runs) - min(runs)) / median * 100
print(f"{path}: {count:,} records")
print(f" runs (rec/s): {', '.join(f'{r:,.0f}' for r in runs)}")
print(f" median: {median:,.0f} rec/s (spread {spread:.1f}%)")
if __name__ == "__main__":
main()