#!/usr/bin/env python3 """Verify metric decoding; optionally verify a live JMeter-to-agent connection.""" import argparse, csv, json, subprocess, tempfile, time from pathlib import Path import xml.etree.ElementTree as E from perfmon import METRICS, check_agent, summarize p=argparse.ArgumentParser();p.add_argument('--host');p.add_argument('--port',type=int,default=4444);p.add_argument('--output');a=p.parse_args() with tempfile.TemporaryDirectory(prefix='healthapp-perfmon-unit-') as name: root=Path(name);raw=root/'raw.csv' def fixture(skip=None,gap=False): with raw.open('w') as f: writer=csv.DictWriter(f,fieldnames=['timeStamp','elapsed','label']);writer.writeheader() for timestamp in ([5000,25000] if gap else range(0,30001,5000)): for kind,param,unit in METRICS: if (kind,param)==skip:continue writer.writerow({'timeStamp':timestamp,'elapsed':73250 if kind=='CPU' else 123000,'label':'fixture '+kind+' '+param}) fixture();stats=summarize(raw,root/'measured.csv',5000,25000,5000) assert stats['fixture CPU combined']['mean']==73.25 assert len(stats)==9 and all(s['samples']==5 for s in stats.values()) for skipped,gap in [(('CPU','combined'),False),(None,True)]: fixture(skipped,gap) try:summarize(raw,root/'invalid.csv',5000,25000,5000) except ValueError:pass else:raise AssertionError('Missing metrics/collection gaps were accepted') print('Metric decoding, warmup exclusion and gap detection passed') if a.host: if not a.output:p.error('--output is required for a live check') check_agent(a.host,a.port) out=Path(a.output).resolve();out.mkdir(parents=True,mode=0o700,exist_ok=False) document=E.parse(Path(__file__).parent/'plans/idle.jmx') for group in document.iter('ThreadGroup'): if group.get('testname')=='Shelly live power':group.set('enabled','false') plan=out/'idle.jmx';document.write(plan) cmd=[str(Path.home()/'.local/bin/jmeter'),'-n','-t',str(plan),'-l',str(out/'samples.csv'),'-j',str(out/'jmeter.log'),'-Jperfmon_host='+a.host,'-Jperfmon_port='+str(a.port),'-Jperfmon_file='+str(out/'perfmon.csv'),'-JjmeterPlugin.perfmon.interval=5000','-JjmeterPlugin.perfmon.useUDP=false','-Jjmeter.save.saveservice.autoflush=true','-Jwarmup_seconds=5','-Jduration_seconds=20','-Jtotal_duration_seconds=30','-Jtimeline_file='+str(out/'timeline.json')] process=subprocess.Popen(cmd);saved_live=False while process.poll() is None: raw=out/'perfmon.csv' if raw.exists() and raw.stat().st_size>0:saved_live=True time.sleep(.25) assert process.returncode==0 and saved_live, 'Metrics were not saved while JMeter was running' timeline=json.loads((out/'timeline.json').read_text()) stats=summarize(out/'perfmon.csv',out/'perfmon-measured.csv',timeline['measurement_start_ms'],timeline['measurement_end_ms'],5000) (out/'summary.json').write_text(json.dumps(stats,indent=2)) print('Live JMeter collection passed: nine metrics written during the run; no application load sent') print('Results:',out)