From bb8449ff7584c9eaf4b3f7817ba0c4220db5ee9c Mon Sep 17 00:00:00 2001 From: Damir Mujic Date: Sun, 29 Nov 2026 22:35:35 +0100 Subject: [PATCH 1/3] test: make two benchmarks time a stable workload test_create_transaction and test_token_bucket_acquire timed out under pytest-benchmark's free-form calibration. Calibration measured the cheapest early calls (an empty MockRuntime store, a full token bucket) and scheduled thousands of rounds, while each later call got slower: the mock store is rewritten in full on every create, and the drained bucket waits for refill. Both hit the 30 s per-test timeout during warmup with no recorded rounds, which then made the JSON report fail with "min() arg is an empty sequence". Use benchmark.pedantic with a per-round setup, as test_state_transition already does: an empty, loaded store for each create, and a fresh primed bucket for each acquire. Each test now runs 100 rounds of the same workload. No SDK code changes. Co-Authored-By: Claude Opus 5.5 --- tests/benchmarks/test_performance.py | 38 +++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/tests/benchmarks/test_performance.py b/tests/benchmarks/test_performance.py index 25742cc..919eb70 100644 --- a/tests/benchmarks/test_performance.py +++ b/tests/benchmarks/test_performance.py @@ -349,12 +349,25 @@ def test_create_transaction( dispute_window=3600, ) + # MockRuntime persists the whole JSON store on every write, so each + # create on a shared store costs more than the one before it. Free-form + # calibration times the first, cheapest calls and then schedules + # thousands of rounds on an ever-growing store. Start every round from + # an empty, already-loaded store so each one times the same workload. + def setup() -> Any: + loop.run_until_complete(mock_runtime.reset()) + loop.run_until_complete(mock_runtime.get_transactions_by_provider(params.provider)) + return (), {} + def create() -> str: return loop.run_until_complete(mock_runtime.create_transaction(params)) - result = benchmark(create) + try: + result = benchmark.pedantic(create, setup=setup, rounds=100, iterations=1) + finally: + loop.run_until_complete(loop.shutdown_default_executor()) + loop.close() - loop.close() assert result.startswith("0x") assert len(result) == 66 @@ -513,15 +526,26 @@ def test_token_bucket_acquire(self, benchmark: BenchmarkFixture) -> None: from agirails.utils.security import TokenBucketRateLimiter - limiter = TokenBucketRateLimiter(max_rate=1000.0, burst_size=100) loop = asyncio.new_event_loop() - def do_acquire() -> bool: - return loop.run_until_complete(limiter.acquire()) + # Once the burst is spent, acquire() sleeps until the next token, so a + # shared limiter turns the benchmark into a measurement of the rate + # limit itself. Free-form calibration times the burst path and then + # schedules over 100k rounds at the refill rate. Give every round a + # fresh bucket, primed outside the timer so the timed call is the + # steady non-blocking path and not first-use lock creation. + def setup() -> Any: + limiter = TokenBucketRateLimiter(max_rate=1000.0, burst_size=100) + loop.run_until_complete(limiter.acquire()) + return (limiter,), {} - result = benchmark(do_acquire) + def do_acquire(limiter: TokenBucketRateLimiter) -> bool: + return loop.run_until_complete(limiter.acquire()) - loop.close() + try: + result = benchmark.pedantic(do_acquire, setup=setup, rounds=100, iterations=1) + finally: + loop.close() # Should be able to acquire with high rate limit assert result is True From 2016bbde9af2e419196b00a0b4ac62a91d8222a0 Mon Sep 17 00:00:00 2001 From: Damir Mujic Date: Sun, 29 Nov 2026 23:05:19 +0100 Subject: [PATCH 2/3] test: verify each timed create persisted its transaction The create benchmark only checked the shape of the last returned ID, so a create that returned an ID without saving anything would still pass and record a false speedup. Read every round's transaction back in a pedantic teardown, outside the timed region, and check its id, parties and amount. Co-Authored-By: Claude Opus 5.5 --- tests/benchmarks/test_performance.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/tests/benchmarks/test_performance.py b/tests/benchmarks/test_performance.py index 919eb70..69c4164 100644 --- a/tests/benchmarks/test_performance.py +++ b/tests/benchmarks/test_performance.py @@ -357,13 +357,27 @@ def test_create_transaction( def setup() -> Any: loop.run_until_complete(mock_runtime.reset()) loop.run_until_complete(mock_runtime.get_transactions_by_provider(params.provider)) - return (), {} - - def create() -> str: - return loop.run_until_complete(mock_runtime.create_transaction(params)) + return ({},), {} + + def create(created: Dict[str, str]) -> str: + tx_id = loop.run_until_complete(mock_runtime.create_transaction(params)) + created["tx_id"] = tx_id + return tx_id + + def verify(created: Dict[str, str]) -> None: + # Every round must have persisted the transaction it timed; read it + # back outside the timed region. + tx = loop.run_until_complete(mock_runtime.get_transaction(created["tx_id"])) + assert tx is not None + assert tx.id == created["tx_id"] + assert tx.provider.lower() == params.provider.lower() + assert tx.requester.lower() == params.requester.lower() + assert str(tx.amount) == params.amount try: - result = benchmark.pedantic(create, setup=setup, rounds=100, iterations=1) + result = benchmark.pedantic( + create, setup=setup, teardown=verify, rounds=100, iterations=1 + ) finally: loop.run_until_complete(loop.shutdown_default_executor()) loop.close() From 363afd62c9872ee53bcad007979000cf586e74e3 Mon Sep 17 00:00:00 2001 From: Damir Mujic Date: Sun, 29 Nov 2026 23:47:19 +0100 Subject: [PATCH 3/3] test: describe what the token bucket benchmark times The comment claimed that priming the bucket kept lock creation out of the timed call. It does not: acquire() can set up its lock on every call, so that cost is part of the measurement. Priming also made no measurable difference (median 8.04 us vs 8.09 us), so drop it and state plainly that each round times one complete non-blocking acquire() on a fresh, full bucket. Co-Authored-By: Claude Opus 5.5 --- tests/benchmarks/test_performance.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/benchmarks/test_performance.py b/tests/benchmarks/test_performance.py index 69c4164..e8b3cbe 100644 --- a/tests/benchmarks/test_performance.py +++ b/tests/benchmarks/test_performance.py @@ -546,12 +546,11 @@ def test_token_bucket_acquire(self, benchmark: BenchmarkFixture) -> None: # shared limiter turns the benchmark into a measurement of the rate # limit itself. Free-form calibration times the burst path and then # schedules over 100k rounds at the refill rate. Give every round a - # fresh bucket, primed outside the timer so the timed call is the - # steady non-blocking path and not first-use lock creation. + # fresh, full bucket built outside the timer. The timed call is one + # complete non-blocking acquire(), including any per-call setup that + # acquire() itself performs. def setup() -> Any: - limiter = TokenBucketRateLimiter(max_rate=1000.0, burst_size=100) - loop.run_until_complete(limiter.acquire()) - return (limiter,), {} + return (TokenBucketRateLimiter(max_rate=1000.0, burst_size=100),), {} def do_acquire(limiter: TokenBucketRateLimiter) -> bool: return loop.run_until_complete(limiter.acquire())