#!/usr/bin/env python3 """Änderung auf Client A, wie lange bis sie bei Client B liegt. prop.py nc|oc N : N Läufe. A schreibt eine kleine Datei und startet sofort seinen Sync (wie ein Desktop-Client nach der lokalen Änderungserkennung). B synchronisiert, sobald der Server ihn benachrichtigt (nc: notify_push-Websocket, wie der Desktop-Client) bzw. im Dauerlauf (oc: kein Push-Kanal für den Kommandozeilen-Client, B startet opencloudcmd direkt wieder, sobald der vorige Lauf endet). Gemessen: Wanduhr von "Datei auf A geschrieben" bis "Datei auf B vorhanden".""" import os, sys, time, subprocess, threading, json, asyncio, statistics P, N = sys.argv[1], int(sys.argv[2]) E = {k: v.strip("'") for k, v in (l.strip().split('=', 1) for l in open('/root/ep19fs/env') if '=' in l and not l.startswith('#'))} DA, DB = f'/home/clienta/p/{P}', f'/home/clientb/p/{P}' H = E.get('NC_URL', E.get('OC_URL', '')).replace('https://', '') def sync(user, d): if P == 'nc': cmd = ['nextcloudcmd', '-n', '--non-interactive', '--path', '/prop', d, E['NC_URL']] else: cmd = ['opencloudcmd', '--non-interactive', '-u', E['OC_USER'], '--remote-folder', '/prop', E['OC_URL'], E['OC_SPACE'], d] env = dict(os.environ); env['OPENCLOUD_TOKEN'] = E.get('OC_TOKEN', '') t = time.time(); r = subprocess.run(['sudo', '-u', user, '-H', '--preserve-env=OPENCLOUD_TOKEN'] + cmd, capture_output=True, env=env) return time.time() - t, r.returncode for d, u in ((DA, 'clienta'), (DB, 'clientb')): subprocess.run(['sudo', '-u', u, 'mkdir', '-p', d]) print(f'# {time.strftime("%H:%M:%S", time.gmtime())} UTC Erstabgleich A und B', flush=True) print(' A', sync('clienta', DA), ' B', sync('clientb', DB), flush=True) trigger = threading.Event(); stop = False; bsyncs = [] def b_loop(): while not stop: if P == 'nc': if not trigger.wait(1): continue trigger.clear() dur, rc = sync('clientb', DB); bsyncs.append(dur) threading.Thread(target=b_loop, daemon=True).start() events = [] if P == 'nc': import websockets async def listen(): async with websockets.connect(f'wss://{H}/push/ws') as ws: await ws.send(E['NC_USER']); await ws.send(E['NC_PASS']) while True: m = await ws.recv(); events.append((time.time(), m[:40])) if m.startswith('notify_file'): trigger.set() threading.Thread(target=lambda: asyncio.run(listen()), daemon=True).start(); time.sleep(3) print(' notify_push:', events[:1], flush=True) res = [] for i in range(1, N + 1): name = f'note-{int(time.time())}-{i}.txt' t0 = time.time() subprocess.run(['sudo', '-u', 'clienta', 'sh', '-c', f'echo run {i} > {DA}/{name}']) da, rc = sync('clienta', DA); ta = time.time() while not os.path.exists(f'{DB}/{name}'): if time.time() - t0 > 300: break time.sleep(0.05) tb = time.time(); ev = [round(t - ta, 2) for t, m in events if t >= ta and m.startswith('notify_file')][:1] res.append(tb - t0) print(f' Lauf {i}: A-Sync {da:.2f} s (rc {rc}), Datei bei B nach {tb - t0:.2f} s' + (f', Push an B {ev[0]} s nach A-Sync-Ende' if ev else ''), flush=True) time.sleep(5) stop = True print(f'Mittel {statistics.mean(res):.2f} s, Median {statistics.median(res):.2f} s, min {min(res):.2f}, max {max(res):.2f} (n={len(res)})') if bsyncs: print(f'B-Syncläufe: {len(bsyncs)}, Dauer Mittel {statistics.mean(bsyncs):.2f} s')