benchmark_prepared_import.py
1"""Compare the former Python export loop with native batches, then import a full day.23Outputs small JSON evidence; all large generated files live in an owned temporary4workspace and are cleaned on exit. No network, model calls or source modification.5"""67import argparse8import csv9import json10from pathlib import Path11import random12import resource13import sys14import tempfile15import time1617ROOT = Path(__file__).resolve().parents[1]18sys.path.insert(0, str(ROOT))19from sim_platform.market.session import MarketSession20from sim_platform.market.prepared.builder import build21from sim_platform.market.prepared.codec import encode_event, encode_state22from sim_platform.market.prepared.reader import PreparedMarket23from benchmark_prepared_runtime import distribution as milliseconds242526def distribution(samples):27 values = milliseconds(samples)28 return dict(29 count=values['count'],30 **{31 key.replace('_ms', '_us'): value * 1000 if value is not None else None32 for key, value in values.items()33 if key != 'count'34 },35 )363738def prefix(source, destination, count):39 """Copy complete BinaryFILE frames and add an explicit fixture terminator."""40 with source.open('rb') as incoming, destination.open('wb') as outgoing:41 for _ in range(count):42 header = incoming.read(2)43 if len(header) != 2:44 raise ValueError('source ends before the requested prefix')45 length = int.from_bytes(header, 'big')46 if not length:47 break48 payload = incoming.read(length)49 if len(payload) != length:50 raise ValueError('source has a truncated frame')51 outgoing.write(header + payload)52 outgoing.write(b'\0\0')535455def reference_export(source, target):56 """Former per-frame JSON/Python path, retained only as a benchmark reference."""57 market = MarketSession('AAPL')58 market.load(source)59 market.play()60 started = time.perf_counter()61 pump_ns = encode_write_ns = frames = versions = 062 previous = encode_state(market.snapshot, 5)63 try:64 with (65 (target / 'events.bin').open('wb') as events,66 (target / 'states.bin').open('wb') as states,67 ):68 states.write(previous)69 while True:70 tick = time.perf_counter_ns()71 event = market.pump(1)[0]72 pump_ns += time.perf_counter_ns() - tick73 if market.status == 'completed':74 break75 if market.status == 'failed':76 raise RuntimeError(event.message)77 tick = time.perf_counter_ns()78 raw = encode_state(event.snapshot, 5)79 if raw != previous:80 versions += 181 states.write(raw)82 previous = raw83 events.write(encode_event(event, versions))84 frames += 185 encode_write_ns += time.perf_counter_ns() - tick86 return dict(87 seconds=time.perf_counter() - started,88 source_frames=frames,89 native_json_python_pump_seconds=pump_ns / 1e9,90 python_encode_write_seconds=encode_write_ns / 1e9,91 artifact_data_bytes=sum(p.stat().st_size for p in target.iterdir()),92 instrumented=True,93 )94 finally:95 market.close()969798def queries(reader, count=5000):99 m = reader.manifest100 rng = random.Random(731)101 points = [rng.randint(m['first_ns'], m['last_ns']) for _ in range(count)]102 runs = []103 for _ in range(2):104 samples = []105 for point in points:106 started = time.perf_counter_ns()107 reader.snapshot_at(point)108 samples.append(time.perf_counter_ns() - started)109 runs.append(distribution(samples))110 return dict(111 first=runs[0],112 repeated=runs[1],113 os_cold=False,114 native_calls_per_query=0,115 replayed_frames_per_query=0,116 )117118119def native_import(source, target, *, implicit=False, profile=False):120 stages = {}121 last_progress = [time.monotonic()]122123 def progress(frames):124 if time.monotonic() - last_progress[0] >= 10:125 print('import progress: ' + str(frames) + ' source frames', flush=True)126 last_progress[0] = time.monotonic()127128 started = time.perf_counter()129 build(130 source,131 target,132 allow_implicit_eof=implicit,133 timings=stages,134 profile_native=profile,135 progress=progress,136 )137 seconds = time.perf_counter() - started138 opens = []139 for _ in range(5):140 started = time.perf_counter_ns()141 with PreparedMarket(target) as reader:142 opens.append(time.perf_counter_ns() - started)143 with PreparedMarket(target) as reader:144 m = reader.manifest145 return dict(146 seconds=seconds,147 source_bytes=source.stat().st_size,148 source_frames=m['source_frames'],149 retained_events=m['frames'],150 state_versions=m['states'],151 artifact_bytes=sum(p.stat().st_size for p in target.iterdir()),152 open=distribution(opens),153 queries=queries(reader),154 instrumented=profile,155 native_stage_timings=stages,156 lifetime_peak_rss_bytes=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss,157 )158159160def compare_csv(target, csv_path):161 """All book events against existing pre-change CSV, including depth and source ID."""162163 def price(value):164 return f'{value // 10000}.{value % 10000:04d}'165166 def levels(values):167 return '|'.join(f'{price(p)}:{q}' for p, q in values)168169 count = 0170 with PreparedMarket(target) as reader, csv_path.open(newline='') as stream:171 rows = csv.DictReader(stream)172 for index in range(reader.manifest['frames']):173 event = reader.event(index)174 if event.source_type not in 'AFECXDU':175 continue176 row = next(rows)177 s = event.snapshot178 actual = [179 str(reader.source_sequence(index + 1)),180 str(event.time_ns),181 event.source_type,182 price(s.bids[0][0]) if s.bids else '',183 str(s.bids[0][1]) if s.bids else '',184 price(s.asks[0][0]) if s.asks else '',185 str(s.asks[0][1]) if s.asks else '',186 str(s.bid_levels),187 str(s.ask_levels),188 levels(s.bids),189 levels(s.asks),190 ]191 if actual != list(row.values()):192 raise AssertionError('prepared differs from existing CSV at row ' + str(count + 1))193 count += 1194 if next(rows, None) is not None:195 raise AssertionError('prepared omitted CSV events')196 return dict(197 rows=count,198 all_fields_equal=True,199 note='pre-existing CSV from original native core; not a second independent book',200 )201202203def main():204 parser = argparse.ArgumentParser(description=__doc__)205 parser.add_argument('--source', type=Path, required=True)206 parser.add_argument('--output', type=Path, required=True)207 parser.add_argument('--csv', type=Path)208 parser.add_argument('--prefix-frames', type=int, default=1_000_000)209 parser.add_argument('--full-day', action='store_true')210 args = parser.parse_args()211 report = dict(212 symbol='AAPL',213 depth=5,214 source=str(args.source.resolve()),215 cold_cache=False,216 scope='local import feasibility, not live or financial throughput',217 )218219 def save():220 args.output.write_text(json.dumps(report, indent=2) + '\n')221222 with tempfile.TemporaryDirectory(prefix='prepared-import-') as workspace:223 root = Path(workspace)224 sample = root / 'prefix.bin'225 prefix(args.source, sample, args.prefix_frames)226 old = root / 'reference'227 old.mkdir()228 report['prefix_reference'] = reference_export(sample, old)229 save()230 print('reference prefix complete', flush=True)231 report['prefix_native'] = native_import(sample, root / 'native')232 save()233 print('native prefix complete', flush=True)234 report['prefix_profiled'] = native_import(sample, root / 'profiled', profile=True)235 save()236 if args.full_day:237 report['full_day'] = native_import(args.source, root / 'full', implicit=True)238 save()239 print('full-day import complete', flush=True)240 if args.csv:241 report['full_day']['existing_csv_comparison'] = compare_csv(root / 'full', args.csv)242 save()243 print('full-day CSV comparison complete', flush=True)244 print(json.dumps(report, indent=2), flush=True)245246247if __name__ == '__main__':248 main()