Skip to content

Commit 6f51c70

Browse files
authored
Add sync and async execution benchmarks (#141)
1 parent 8e72bfc commit 6f51c70

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import asyncio
2+
from graphql import (
3+
GraphQLSchema,
4+
GraphQLObjectType,
5+
GraphQLField,
6+
GraphQLString,
7+
graphql,
8+
)
9+
10+
11+
user = GraphQLObjectType(
12+
name="User",
13+
fields={
14+
"id": GraphQLField(GraphQLString),
15+
"name": GraphQLField(GraphQLString),
16+
},
17+
)
18+
19+
20+
async def resolve_user(obj, info):
21+
return {
22+
"id": "1",
23+
"name": "Sarah",
24+
}
25+
26+
27+
schema = GraphQLSchema(
28+
query=GraphQLObjectType(
29+
name="Query",
30+
fields={
31+
"user": GraphQLField(
32+
user,
33+
resolve=resolve_user,
34+
)
35+
},
36+
)
37+
)
38+
39+
40+
def test_execute_basic_async(benchmark):
41+
result = benchmark(
42+
lambda: asyncio.run(graphql(schema, "query { user { id, name }}"))
43+
)
44+
assert not result.errors
45+
assert result.data == {
46+
"user": {
47+
"id": "1",
48+
"name": "Sarah",
49+
},
50+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from graphql import (
2+
GraphQLSchema,
3+
GraphQLObjectType,
4+
GraphQLField,
5+
GraphQLString,
6+
graphql_sync,
7+
)
8+
9+
10+
user = GraphQLObjectType(
11+
name="User",
12+
fields={
13+
"id": GraphQLField(GraphQLString),
14+
"name": GraphQLField(GraphQLString),
15+
},
16+
)
17+
18+
19+
def resolve_user(obj, info):
20+
return {
21+
"id": "1",
22+
"name": "Sarah",
23+
}
24+
25+
26+
schema = GraphQLSchema(
27+
query=GraphQLObjectType(
28+
name="Query",
29+
fields={
30+
"user": GraphQLField(
31+
user,
32+
resolve=resolve_user,
33+
)
34+
},
35+
)
36+
)
37+
38+
39+
def test_execute_basic_sync(benchmark):
40+
result = benchmark(lambda: graphql_sync(schema, "query { user { id, name }}"))
41+
assert not result.errors
42+
assert result.data == {
43+
"user": {
44+
"id": "1",
45+
"name": "Sarah",
46+
},
47+
}

0 commit comments

Comments
 (0)