Skip to content

Commit

Permalink
Add simple execute benchmarks for both sync and async execution
Browse files Browse the repository at this point in the history
  • Loading branch information
jkimbo committed Oct 13, 2021
1 parent 8e72bfc commit dcf2672
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/benchmarks/test_execution_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import asyncio
from graphql import (
GraphQLSchema,
GraphQLObjectType,
GraphQLField,
GraphQLString,
graphql,
)


user = GraphQLObjectType(
name="User",
fields={
"id": GraphQLField(GraphQLString),
"name": GraphQLField(GraphQLString),
},
)


async def resolve_user(obj, info):
return {
"id": "1",
"name": "Sarah",
}


schema = GraphQLSchema(
query=GraphQLObjectType(
name="Query",
fields={
"user": GraphQLField(
user,
resolve=resolve_user,
)
},
)
)


def test_execute_basic_async(benchmark):
result = benchmark(
lambda: asyncio.run(graphql(schema, "query { user { id, name }}"))
)
assert not result.errors
assert result.data == {
"user": {
"id": "1",
"name": "Sarah",
},
}
47 changes: 47 additions & 0 deletions tests/benchmarks/test_execution_sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from graphql import (
GraphQLSchema,
GraphQLObjectType,
GraphQLField,
GraphQLString,
graphql_sync,
)


user = GraphQLObjectType(
name="User",
fields={
"id": GraphQLField(GraphQLString),
"name": GraphQLField(GraphQLString),
},
)


def resolve_user(obj, info):
return {
"id": "1",
"name": "Sarah",
}


schema = GraphQLSchema(
query=GraphQLObjectType(
name="Query",
fields={
"user": GraphQLField(
user,
resolve=resolve_user,
)
},
)
)


def test_execute_basic_sync(benchmark):
result = benchmark(lambda: graphql_sync(schema, "query { user { id, name }}"))
assert not result.errors
assert result.data == {
"user": {
"id": "1",
"name": "Sarah",
},
}

0 comments on commit dcf2672

Please sign in to comment.