#!/usr/bin/env python3 """NVMe read ceiling with O_DIRECT (no page cache): sequential 1 MiB, random 1 MiB and 128 KiB, 1 and 16 threads.""" import os, sys, mmap, random, time, threading, json path = sys.argv[1]; size = os.path.getsize(path) def run(block, threads, seconds, sequential): total = [0]; lock = threading.Lock(); stop = time.time() + seconds def worker(i): fd = os.open(path, os.O_RDONLY | os.O_DIRECT); buf = mmap.mmap(-1, block); n = 0 off = (size // threads) * i // block * block; rnd = random.Random(i) while time.time() < stop: if not sequential: off = rnd.randrange(0, size - block) // 4096 * 4096 r = os.preadv(fd, [buf], off); n += r if sequential: off = off + block if off + 2 * block < size else 0 os.close(fd) with lock: total[0] += n ts = [threading.Thread(target=worker, args=(i,)) for i in range(threads)] t0 = time.time(); [t.start() for t in ts]; [t.join() for t in ts] return total[0] / (time.time() - t0) / 1e9 res = {} for name, b, t, seq in [("seq-1MiB-t1", 1 << 20, 1, True), ("seq-1MiB-t4", 1 << 20, 4, True), ("rand-1MiB-t1", 1 << 20, 1, False), ("rand-1MiB-t16", 1 << 20, 16, False), ("rand-128KiB-t1", 128 << 10, 1, False), ("rand-128KiB-t16", 128 << 10, 16, False)]: res[name] = round(run(b, t, 15, seq), 2); print(name, res[name], "GB/s", flush=True) print(json.dumps(res))