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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import argparse
import hashlib
import json
import re
import subprocess
import time
from pathlib import Path
from isolated import run, snapshot as resource_snapshot
parser = argparse.ArgumentParser()
parser.add_argument('--env-file', type=Path, required=True)
parser.add_argument('--output', type=Path, required=True)
parser.add_argument('--count', type=int, default=1280)
parser.add_argument('--rate', type=int, default=10)
parser.add_argument('--reverse', action='store_true')
args = parser.parse_args()
args.output.mkdir(parents=True, exist_ok=True)
compose = ['docker', 'compose', '--env-file', str(args.env_file), '-f', 'scripts/zkv/offline-compose.yaml']
def snapshot():
return resource_snapshot(compose, ["relay", "rocks"])
def client(label, *options):
began = time.monotonic()
output = run([*compose, 'run', '--rm', '--pull', 'never', '--no-deps', *options, 'client', '/load.mjs'], label,
timeout=max(180, (args.count + min(args.count, 256)) / args.rate + 90))
rows = [json.loads(line) for line in output.splitlines() if line.startswith('{')]
(args.output / f'{label}.json').write_text(json.dumps(rows, indent=2) + '\n')
print(json.dumps({'stage': label, 'seconds': time.monotonic() - began, 'results': rows}), flush=True)
return rows
try:
environment = dict(line.split('=', 1) for line in args.env_file.read_text().splitlines() if '=' in line)
binaries = {
'zkv': Path(environment['RELAY_TEST_BINARY']),
'rocks': Path(environment.get('ROCKS_TEST_BINARY', '/tmp/zkv-zlay-offline-rocks-bin/bin/zlay')),
}
metadata = {
'docker': run(['docker', 'info', '--format', '{{.Architecture}} {{.NCPU}} {{.MemTotal}} {{.KernelVersion}}'], 'environment').strip(),
'binaries': {name: {'bytes': path.stat().st_size, 'sha256': hashlib.sha256(path.read_bytes()).hexdigest()} for name, path in binaries.items()},
}
(args.output / 'environment.json').write_text(json.dumps(metadata, indent=2) + '\n')
run([*compose, 'up', '--pull', 'never', '--no-build', '--force-recreate', '--wait', '--wait-timeout', '120', 'db', 'relay', 'rocks-db', 'rocks'], 'start')
before = snapshot()
client('load', '-e', f'LOAD_COUNT={args.count}', '-e', f'LOAD_RATE={args.rate}',
'-e', f'LOAD_PHASE_ORDER={"novel,existing" if args.reverse else "existing,novel"}', '--entrypoint', 'node')
after = snapshot()
(args.output / 'resources.json').write_text(json.dumps({'before': before, 'after': after}, indent=2) + '\n')
run([*compose, 'stop', '--timeout', '30', 'relay', 'rocks'], 'stop')
logs = run([*compose, 'logs', '--no-color', 'relay'], 'maintenance counters')
final = re.findall(r'collection index final: ([^\n]+)', logs)
assert final, 'missing final index counters'
(args.output / 'maintenance.json').write_text(json.dumps(final, indent=2) + '\n')
print(json.dumps({'stage': 'maintenance', 'snapshots': final}), flush=True)
run([*compose, 'start', '--wait', '--wait-timeout', '45', 'relay', 'rocks'], 'restart')
client('restart', '-e', 'RELAY_VERIFY_ONLY=true', '--entrypoint', 'node')
except BaseException:
diagnostic = subprocess.run([*compose, 'logs', '--no-color', 'relay', 'rocks'], capture_output=True, text=True, timeout=10)
private_log = args.output / 'failure-private.log'
private_log.touch(mode=0o600, exist_ok=True)
private_log.write_text(diagnostic.stdout + diagnostic.stderr)
raise
finally:
run([*compose, 'down', '--volumes', '--timeout', '30'], 'cleanup')