(fixture, config, payload_bytes, turn, previous_ledger)
| 158 | |
| 159 | |
| 160 | def measure_turn(fixture, config, payload_bytes, turn, previous_ledger): |
| 161 | fixture.hook("user-prompt-submit", {"session_id": SESSION, "prompt": f"Implement measured turn {turn}"}) |
| 162 | running = fixture.status(turn) |
| 163 | assert running["state"] == "Running" |
| 164 | start_seq = running["next_event_seq"] |
| 165 | pid = running["provenance_id"] |
| 166 | generation = running["generation"] |
| 167 | count = config["events"] |
| 168 | |
| 169 | requests = [] |
| 170 | if config["transport"] == "rpc": |
| 171 | timestamp_ms = int(time.time() * 1000) |
| 172 | envelopes = [envelope(i, payload_bytes, turn, generation, timestamp_ms) for i in range(count)] |
| 173 | wire = [{"event_id": e["event_id"], "bytes": list(encoded(e))} for e in envelopes] |
| 174 | for offset in range(0, count, config["batch"]): |
| 175 | requests.append({"AppendProvenanceEnvelopes": { |
| 176 | "provenance_id": pid, "expected_generation": generation, |
| 177 | "envelopes": wire[offset:offset + config["batch"]], "now": timestamp_ms // 1000}}) |
| 178 | else: |
| 179 | requests = [tool_payload(i, payload_bytes, turn) for i in range(count)] |
| 180 | |
| 181 | def append(request): |
| 182 | started = time.perf_counter_ns() |
| 183 | if config["transport"] == "rpc": |
| 184 | response = fixture.rpc(request)["ProvenanceEnvelopesCommitted"] |
| 185 | acks = response["acknowledgements"] |
| 186 | else: |
| 187 | fixture.hook("post-tool", request) |
| 188 | acks = [] |
| 189 | return (time.perf_counter_ns() - started) / 1e6, acks |
| 190 | |
| 191 | # Pool creation is outside the timer; lazy thread startup is included in wall time. |
| 192 | with concurrent.futures.ThreadPoolExecutor(max_workers=config["workers"]) as pool: |
| 193 | started = time.perf_counter_ns() |
| 194 | outputs = list(pool.map(append, requests)) |
| 195 | append_wall_ms = (time.perf_counter_ns() - started) / 1e6 |
| 196 | latencies = [row[0] for row in outputs] |
| 197 | acks = [ack for row in outputs for ack in row[1]] |
| 198 | after_append = fixture.status(turn) |
| 199 | assert after_append["next_event_seq"] == start_seq + count, (start_seq, after_append) |
| 200 | if acks: |
| 201 | assert sorted(a["sequence"] for a in acks) == list(range(start_seq, start_seq + count)) |
| 202 | assert len({a["event_id"] for a in acks}) == count |
| 203 | # Duplicate delivery is checked outside the measured append phase. |
| 204 | append(requests[0]) |
| 205 | assert fixture.status(turn)["next_event_seq"] == start_seq + count |
| 206 | |
| 207 | # A real file change causes recording plus graph generation and ledger publication. |
| 208 | source = "\n".join(f"pub fn value_{i}() -> u64 {{ {i + turn} }}" for i in range(128)) + "\n" |
| 209 | writes_file = config.get("write_file", True) |
| 210 | if writes_file: |
| 211 | (fixture.root / "measured.rs").write_text(source) |
| 212 | result = {"turn": turn, "events": count, "append_wall_ms": append_wall_ms, |
| 213 | "events_s": count / (append_wall_ms / 1000), "append_request_ms": latencies, |
| 214 | "append_latency": summary(latencies), "starting_event_seq": start_seq, |
| 215 | "append_verified": True, "duplicate_verified": True, |
| 216 | "file_bytes": len(source.encode()) if writes_file else 0} |
| 217 | if config["transport"] == "rpc": |
no test coverage detected