interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / tests / measure.py

45 lines · 3389 bytes Raw Blame History
1"""Small end-to-end observations; includes process startup, not a speed claim."""
2import pathlib, subprocess, statistics, time, json, sys, os, importlib.util, ctypes
3root=pathlib.Path(__file__).resolve().parents[1];os.chdir(root)
4b=root/'build/measure';b.mkdir(exist_ok=True)
5def run(args,data=b''):
6 p=subprocess.run([str(x) for x in args],input=data,capture_output=True,timeout=30)
7 assert p.returncode==0,(args,p.returncode,p.stderr[:200])
8 return p.stdout
9rows=[]
10for name in ('fib','sort','echo'):
11 src=pathlib.Path(f'tests/fixtures/{name}.om')
12 inp=bytes(range(256))*195 if name=='echo' else b''
13 for target in ('c','python'):
14 times=[]
15 for _ in range(3):
16 start=time.perf_counter();out=run(['tools/bridge',target,src]);times.append(time.perf_counter()-start)
17 file=b/(name+('.c' if target=='c' else '.py'));file.write_bytes(out)
18 rows.append(dict(workload=name,phase='checked_conversion',target=target,median_seconds=statistics.median(times),samples_seconds=times,source_bytes=src.stat().st_size,output_bytes=len(out)))
19 if target=='c':run(['gcc','-O2','-std=gnu11',file,'-o',b/name])
20 expected=run([root/'build/tests'/f'{name}.native'],inp)
21 for target,args in [('native',[root/'build/tests'/f'{name}.native']),('c',[b/name]),('python',[sys.executable,b/(name+'.py')])]:
22 times=[]
23 for _ in range(5):
24 start=time.perf_counter();got=run(args,inp);times.append(time.perf_counter()-start);assert got==expected
25 rows.append(dict(workload=name,phase='process_execution',target=target,median_seconds=statistics.median(times),samples_seconds=times,input_bytes=len(inp)))
26# Direct Python and ctypes calls use the same fixed-size sum and include boundary checks.
27run(['gcc','-O2','-std=gnu11','-fPIC','-shared','build/attachments/attachment.c','-o',b/'attachment.so'])
28spec=importlib.util.spec_from_file_location('measured_attachment','build/attachments/attachment.py');module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
29lib=ctypes.CDLL(str(b/'attachment.so'));f=lib.omega_total
30# Deliberate demo-specific ABI declaration; not a general generated ctypes adapter.
31f.argtypes=[ctypes.POINTER(ctypes.c_uint64),ctypes.c_void_p,ctypes.c_uint64];f.restype=ctypes.c_uint32
32arr=(ctypes.c_uint64*4)(2,3,5,7);out=ctypes.c_uint64()
33for target in ('python_module','ctypes_c'):
34 samples=[]
35 for _ in range(5):
36 start=time.perf_counter()
37 for i in range(10000):
38 if target=='python_module':assert module.omega_total([2,3,5,7],4)==(0,17)
39 else:assert f(ctypes.byref(out),arr,4)==0 and out.value==17
40 samples.append(time.perf_counter()-start)
41 rows.append(dict(workload='total4',phase='10000_checked_calls',target=target,median_seconds=statistics.median(samples),samples_seconds=samples))
42report=dict(context='Shared Linux AArch64 host; quiet-build coordination, not an isolated performance window. Startup included for program runs. Python module call loop includes list construction; ctypes loop reuses C storage. Do not treat the two caller timings as a fair language speed comparison.',results=rows)
43pathlib.Path('evidence/measurements.json').write_text(json.dumps(report,indent=2)+'\n')
44for r in rows:print(r['phase'],r['workload'],r['target'],f"{r['median_seconds']:.6f}s")
45print('measurement correctness checks RC=0')