Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions tests/benchmarks/test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,39 @@ def test_create_transaction(
dispute_window=3600,
)

def create() -> str:
return loop.run_until_complete(mock_runtime.create_transaction(params))
# 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(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

result = benchmark(create)
try:
result = benchmark.pedantic(
create, setup=setup, teardown=verify, 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

Expand Down Expand Up @@ -513,15 +540,25 @@ 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, 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:
return (TokenBucketRateLimiter(max_rate=1000.0, burst_size=100),), {}

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

Expand Down
Loading