1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import csv, io, os, select, subprocess, sys, tempfile, threading
from pathlib import Path
binary = Path(sys.argv[1]).resolve()
for valid in [True, False]:
with tempfile.TemporaryDirectory(prefix='zkv-perf-control-') as scratch:
root = Path(scratch)
ctl = root / 'control'
ack = root / 'ack'
os.mkfifo(ctl)
os.mkfifo(ack)
control = os.open(ctl, os.O_RDWR)
reply = os.open(ack, os.O_RDWR)
seen = []
errors = []
def serve():
try:
for expected in [b'enable\n', b'disable\n'] if valid else [b'enable\n']:
assert select.select([control], [], [], 20)[0], 'control timeout'
command = os.read(control, 64)
assert command == expected, command
seen.append(command)
os.write(reply, b'ack\n' if valid else b'bad\n')
except BaseException as error:
errors.append(error)
worker = threading.Thread(target=serve)
worker.start()
try:
env = {**os.environ, 'ZKV_PERF_CONTROL': str(ctl), 'ZKV_PERF_ACK': str(ack)}
result = subprocess.run([str(binary), str(root / 'db'), '1000', '1', '16', '10', '100', 'stepped-prepared', '64', '4096', '4', '0', '8', '--allocator=c'], env=env, text=True, capture_output=True, timeout=25, check=valid)
if valid:
row = list(csv.DictReader(io.StringIO(result.stdout)))[0]
assert row['failed'] == '0' and row['verified_pairs'] == '4196', row
else:
assert result.returncode != 0 and 'InvalidProfileAcknowledgement' in result.stderr, result
finally:
worker.join(timeout=1)
os.close(control)
os.close(reply)
assert not errors, errors
assert seen == ([b'enable\n', b'disable\n'] if valid else [b'enable\n']), seen
print('profile control passed:', '4196 reopened pairs verified' if valid else 'invalid acknowledgement rejected')