Skip to content

Commit

Permalink
chore: Add benchmark test for long running subscription (strawberry-g…
Browse files Browse the repository at this point in the history
  • Loading branch information
nrbnlulu authored Aug 8, 2024
1 parent 9de5ec2 commit 42749f6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/benchmarks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Subscription:
async def something(self) -> AsyncIterator[str]:
yield "Hello World!"

@strawberry.subscription
async def long_running(self, count: int) -> AsyncIterator[int]:
for i in range(count):
yield i


@strawberry.directive(locations=[DirectiveLocation.FIELD])
def uppercase(value: str) -> str:
Expand Down
24 changes: 24 additions & 0 deletions tests/benchmarks/test_subscriptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
from typing import AsyncIterator

import pytest
from graphql import ExecutionResult
from pytest_codspeed.plugin import BenchmarkFixture

from .api import schema
Expand All @@ -24,3 +26,25 @@ async def _run():
assert value.data["something"] == "Hello World!"

benchmark(lambda: asyncio.run(_run()))


@pytest.mark.benchmark
@pytest.mark.parametrize("count", [1000, 20000])
def test_subscription_long_run(benchmark: BenchmarkFixture, count: int) -> None:
s = """#graphql
subscription LongRunning($count: Int!) {
longRunning(count: $count)
}
"""

async def _run():
i = 0
aiterator: AsyncIterator[ExecutionResult] = await schema.subscribe(
s, variable_values={"count": count}
) # type: ignore[assignment]
async for res in aiterator:
assert res.data is not None
assert res.data["longRunning"] == i
i += 1

benchmark(lambda: asyncio.run(_run()))

0 comments on commit 42749f6

Please sign in to comment.