Skip to content

Commit

Permalink
Use asyncio.run instead of asgiref for benchmarks (#3325)
Browse files Browse the repository at this point in the history
* Use asyncio.run instead of asgiref

* Move more tests to asyncio.run
  • Loading branch information
patrick91 authored Jan 7, 2024
1 parent 6cba67d commit ba65949
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
17 changes: 11 additions & 6 deletions tests/benchmarks/test_complex_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio

import pytest
from asgiref.sync import async_to_sync
from pytest_codspeed.plugin import BenchmarkFixture

from .schema import schema
Expand Down Expand Up @@ -87,10 +88,14 @@

@pytest.mark.parametrize("number", [50])
def test_execute_complex_schema(benchmark: BenchmarkFixture, number: int):
result = benchmark(
async_to_sync(schema.execute),
query,
variable_values={"query": "test", "first": number},
)
def run():
coroutine = schema.execute(
query,
variable_values={"query": "test", "first": number},
)

return asyncio.run(coroutine)

result = benchmark(run)

assert not result.errors
27 changes: 18 additions & 9 deletions tests/benchmarks/test_execute.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import asyncio
import datetime
import random
from datetime import date
from typing import List, Type, cast

import pytest
from asgiref.sync import async_to_sync
from pytest_codspeed.plugin import BenchmarkFixture

import strawberry
Expand Down Expand Up @@ -72,7 +72,10 @@ def patrons(self) -> List[Patron]:
}
"""

benchmark(async_to_sync(schema.execute), query)
def run():
return asyncio.run(schema.execute(query))

benchmark(run)


@pytest.mark.parametrize("ntypes", [2**k for k in range(0, 13, 4)])
Expand All @@ -92,10 +95,16 @@ class Query:
schema = strawberry.Schema(query=Query, types=CONCRETE_TYPES)
query = "query { items { id } }"

benchmark(
async_to_sync(schema.execute),
query,
root_value=Query(
items=[CONCRETE_TYPES[i % ntypes](id=cast(ID, i)) for i in range(1000)]
),
)
def run():
return asyncio.run(
schema.execute(
query,
root_value=Query(
items=[
CONCRETE_TYPES[i % ntypes](id=cast(ID, i)) for i in range(1000)
]
),
)
)

benchmark(run)

0 comments on commit ba65949

Please sign in to comment.