interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / hive-render / measure.py

44 lines · 2385 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Read-only host measurement; canonicalization mirrors receiver serde_json.
3Python, gzip and time are independent measurement tools, not serving dependencies.
4"""
5import gzip
6import hashlib
7import json
8from pathlib import Path
9import subprocess
10import sys
11import time
12
13binary, snapshot, destination = map(Path, sys.argv[1:4])
14binary = binary.resolve()
15value = json.loads(snapshot.read_bytes())
16raw = (json.dumps(value, sort_keys=True, separators=(',', ':'), ensure_ascii=False) + '\n').encode()
17start = time.perf_counter_ns()
18first = subprocess.run([binary], input=raw, capture_output=True, check=True).stdout
19elapsed = time.perf_counter_ns() - start
20second = subprocess.run([binary], input=raw, capture_output=True, check=True).stdout
21assert first == second
22report = 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.
28remote = subprocess.check_output(['ssh', 'hub', 'mktemp -d /tmp/hive-render-measure.XXXXXXXX'], text=True).strip()
29try:
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
32raw=sys.stdin.buffer.read()
33start=time.perf_counter_ns()
34out=subprocess.run([sys.argv[1]],input=raw,capture_output=True,check=True).stdout
35elapsed=time.perf_counter_ns()-start
36print(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)
41finally:
42 subprocess.run(['ssh', 'hub', 'rm -f ' + remote + '/hive-render && rmdir ' + remote], check=True)
43destination.write_text(json.dumps(report, indent=2) + '\n')
44print(json.dumps(report, indent=2))