From 6b8c8510af3a08f91e5d60efb30454e46bf1cc9d Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Mon, 18 Oct 2021 11:49:29 +0100 Subject: [PATCH 1/2] Create async loop outside of benchmark --- tests/benchmarks/test_execution_async.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/benchmarks/test_execution_async.py b/tests/benchmarks/test_execution_async.py index f2d7eb32..e198e1ab 100644 --- a/tests/benchmarks/test_execution_async.py +++ b/tests/benchmarks/test_execution_async.py @@ -38,9 +38,15 @@ async def resolve_user(obj, info): def test_execute_basic_async(benchmark): + loop = asyncio.events.new_event_loop() + asyncio.events.set_event_loop(loop) result = benchmark( - lambda: asyncio.run(graphql(schema, "query { user { id, name }}")) + lambda: loop.run_until_complete(graphql(schema, "query { user { id, name }}")) ) + + asyncio.events.set_event_loop(None) + loop.close() + assert not result.errors assert result.data == { "user": { From 33def42c6e3c1c189ae6fd67a4a3abe3ce9e728b Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Sun, 28 Nov 2021 17:29:31 +0000 Subject: [PATCH 2/2] Add note to test --- tests/benchmarks/test_execution_async.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/benchmarks/test_execution_async.py b/tests/benchmarks/test_execution_async.py index e198e1ab..76f32b8d 100644 --- a/tests/benchmarks/test_execution_async.py +++ b/tests/benchmarks/test_execution_async.py @@ -38,6 +38,8 @@ async def resolve_user(obj, info): def test_execute_basic_async(benchmark): + # Note: we are creating the async loop outside of the benchmark code so that + # the setup is not included in the benchmark timings loop = asyncio.events.new_event_loop() asyncio.events.set_event_loop(loop) result = benchmark(