aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / hive-render / measure.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Read-only host measurement; canonicalization mirrors receiver serde_json. |
| 3 | Python, gzip and time are independent measurement tools, not serving dependencies. |
| 4 | """ |
| 5 | import gzip |
| 6 | import hashlib |
| 7 | import json |
| 8 | from pathlib import Path |
| 9 | import subprocess |
| 10 | import sys |
| 11 | import time |
| 12 | |
| 13 | binary, snapshot, destination = map(Path, sys.argv[1:4]) |
| 14 | binary = binary.resolve() |
| 15 | value = json.loads(snapshot.read_bytes()) |
| 16 | raw = (json.dumps(value, sort_keys=True, separators=(',', ':'), ensure_ascii=False) + '\n').encode() |
| 17 | start = time.perf_counter_ns() |
| 18 | first = subprocess.run([binary], input=raw, capture_output=True, check=True).stdout |
| 19 | elapsed = time.perf_counter_ns() - start |
| 20 | second = subprocess.run([binary], input=raw, capture_output=True, check=True).stdout |
| 21 | assert first == second |
| 22 | report = dict(generated_at=value['generated_at'], agents=len(value['agents']), interactions=len(value['interactions']), |
| 23 | input_bytes=len(raw), board_bytes=len(first), board_gzip_bytes=len(gzip.compress(first, mtime=0)), |
| 24 | spark_render_ns=elapsed, board_sha256=hashlib.sha256(first).hexdigest(), |
| 25 | renderer_sha256=hashlib.sha256(binary.read_bytes()).hexdigest()) |
| 26 | # Only a per-run scratch directory under /tmp is created on hub. No service, |
| 27 | # configuration or web-root files are read or changed by this measurement. |
| 28 | remote = subprocess.check_output(['ssh', 'hub', 'mktemp -d /tmp/hive-render-measure.XXXXXXXX'], text=True).strip() |
| 29 | try: |
| 30 | subprocess.run(['scp', '-q', str(binary), 'hub:' + remote + '/hive-render'], check=True) |
| 31 | result = subprocess.run(['ssh', 'hub', 'python3 -c ' + __import__('shlex').quote('''import hashlib,json,subprocess,sys,time |
| 32 | raw=sys.stdin.buffer.read() |
| 33 | start=time.perf_counter_ns() |
| 34 | out=subprocess.run([sys.argv[1]],input=raw,capture_output=True,check=True).stdout |
| 35 | elapsed=time.perf_counter_ns()-start |
| 36 | print(json.dumps(dict(hub_render_ns=elapsed,hub_board_bytes=len(out),hub_board_sha256=hashlib.sha256(out).hexdigest()))) |
| 37 | ''') + ' ' + remote + '/hive-render'], input=raw, capture_output=True, check=True) |
| 38 | edge = json.loads(result.stdout) |
| 39 | assert edge['hub_board_sha256'] == report['board_sha256'] |
| 40 | report.update(edge) |
| 41 | finally: |
| 42 | subprocess.run(['ssh', 'hub', 'rm -f ' + remote + '/hive-render && rmdir ' + remote], check=True) |
| 43 | destination.write_text(json.dumps(report, indent=2) + '\n') |
| 44 | print(json.dumps(report, indent=2)) |