-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_performance.py
executable file
·272 lines (211 loc) · 7.51 KB
/
test_performance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!./venv/bin/python
import asyncio
import time
import typing
import ariadne
import ariadne.asgi
import cannula
import cannula.contrib.asgi
import httpx
import fastapi
import pydantic
NUM_RUNS = 1000
class Widget(pydantic.BaseModel):
name: str
use: str
quantity: int
WIDGETS: typing.List[dict] = [
{"name": "screw driver", "use": "tighten", "quantity": 10},
{"name": "hammer", "use": "nail", "quantity": 15},
{"name": "saw", "use": "cut", "quantity": 6},
{"name": "wrench", "use": "tighten", "quantity": 20},
{"name": "bolt", "use": "fasten", "quantity": 13},
]
schema = """
type Widget {
name: String
use: String
quantity: Int
}
type Query {
get_widgets(use: String): [Widget]
}
"""
document = """
query widgets ($use: String) {
get_widgets(use: $use) {
name
use
quantity
}
}
"""
invalid_document = """
query blah ( { }
"""
invalid_query = """
query widgets ($use: String) {
get_nonexistent(use: $use) {
foo
}
}
"""
api = fastapi.FastAPI()
def _get_widgets(use: str) -> typing.List[dict]:
matches = []
for widget in WIDGETS:
if widget.get("use") == use:
matches.append(widget)
return matches
@api.get("/api/fastapi", response_model=typing.List[Widget])
async def get_widgets_with_fastapi(use: str) -> typing.Any:
return _get_widgets(use)
async def resolve_get_widgets(info, use: str) -> typing.List[dict]:
return _get_widgets(use)
# Create executable schema instance
exe_schema = ariadne.make_executable_schema(schema)
# Use the root value for our simple resolver. This way both
# Ariadne and Cannula use the same logic to resolve a query
ariadne_app = ariadne.asgi.GraphQL(
exe_schema, root_value={"get_widgets": resolve_get_widgets}
)
cannula_app = cannula.CannulaAPI(
schema=schema, root_value={"get_widgets": resolve_get_widgets}
)
@api.post("/api/ariadne")
async def get_ariadne_app(request: fastapi.Request) -> typing.Any:
return await ariadne_app.handle_request(request)
@api.post("/api/cannula")
async def get_cannula_app(
request: fastapi.Request, payload: cannula.contrib.asgi.GraphQLPayload
) -> typing.Any:
results = await cannula_app.call(payload.query, variables=payload.variables)
errors = [e.formatted for e in results.errors] if results.errors else None
return {"data": results.data, "errors": errors}
async def test_performance():
transport = httpx.ASGITransport(app=api)
client = httpx.AsyncClient(transport=transport, base_url="http://localhost")
start = time.perf_counter()
for x in range(NUM_RUNS):
resp = await client.get("/api/fastapi?use=tighten")
assert resp.status_code == 200, resp.text
assert resp.json() == [
{"name": "screw driver", "quantity": 10, "use": "tighten"},
{"name": "wrench", "quantity": 20, "use": "tighten"},
]
stop = time.perf_counter()
fast_results = stop - start
print("\nperformance test results:")
print(f"fastapi: {fast_results}")
start = time.perf_counter()
for _x in range(NUM_RUNS):
resp = await client.post(
"/api/ariadne",
json={"query": document, "variables": {"use": "tighten"}},
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 200, resp.text
assert resp.json()["data"]["get_widgets"] == [
{"name": "screw driver", "quantity": 10, "use": "tighten"},
{"name": "wrench", "quantity": 20, "use": "tighten"},
]
stop = time.perf_counter()
ariadne_results = stop - start
print(f"ariadne results: {ariadne_results}")
start = time.perf_counter()
for _x in range(NUM_RUNS):
resp = await client.post(
"/api/cannula",
json={"query": document, "variables": {"use": "tighten"}},
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 200, resp.text
assert resp.json()["data"]["get_widgets"] == [
{"name": "screw driver", "quantity": 10, "use": "tighten"},
{"name": "wrench", "quantity": 20, "use": "tighten"},
]
stop = time.perf_counter()
cannula_results = stop - start
print(f"cannula results: {cannula_results}")
async def test_performance_invalid_request():
client = httpx.AsyncClient(app=api, base_url="http://localhost")
start = time.perf_counter()
for x in range(NUM_RUNS):
resp = await client.get("/api/fastapi")
assert resp.status_code == 422, resp.text
stop = time.perf_counter()
fast_results = stop - start
print("\nperformance test results:")
print(f"fastapi: {fast_results}")
start = time.perf_counter()
for _x in range(NUM_RUNS):
resp = await client.post(
"/api/ariadne",
json={"query": invalid_document, "variables": {"use": "tighten"}},
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400, resp.text
errors = resp.json()["errors"]
assert len(errors) == 1
assert errors[0]["message"] == "Syntax Error: Expected '$', found '{'."
stop = time.perf_counter()
ariadne_results = stop - start
print(f"ariadne results: {ariadne_results}")
start = time.perf_counter()
for _x in range(NUM_RUNS):
resp = await client.post(
"/api/cannula",
json={"query": invalid_document, "variables": {"use": "tighten"}},
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 200, resp.text
errors = resp.json()["errors"]
assert len(errors) == 1
assert errors[0]["message"] == "Syntax Error: Expected '$', found '{'."
stop = time.perf_counter()
cannula_results = stop - start
print(f"cannula results: {cannula_results}")
async def test_performance_invalid_query():
client = httpx.AsyncClient(app=api, base_url="http://localhost")
start = time.perf_counter()
for x in range(NUM_RUNS):
resp = await client.get("/api/fastapi")
assert resp.status_code == 422, resp.text
stop = time.perf_counter()
fast_results = stop - start
print("\nperformance test results:")
print(f"fastapi: {fast_results}")
start = time.perf_counter()
for _x in range(NUM_RUNS):
resp = await client.post(
"/api/ariadne",
json={"query": invalid_query, "variables": {"use": "tighten"}},
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 400, resp.text
errors = resp.json()["errors"]
assert len(errors) == 1
assert (
errors[0]["message"]
== "Cannot query field 'get_nonexistent' on type 'Query'."
)
stop = time.perf_counter()
ariadne_results = stop - start
print(f"ariadne results: {ariadne_results}")
start = time.perf_counter()
for _x in range(NUM_RUNS):
resp = await client.post(
"/api/cannula",
json={"query": invalid_query, "variables": {"use": "tighten"}},
headers={"Content-Type": "application/json"},
)
assert resp.status_code == 200, resp.text
errors = resp.json()["errors"]
assert len(errors) == 1
assert (
errors[0]["message"]
== "Cannot query field 'get_nonexistent' on type 'Query'."
)
stop = time.perf_counter()
cannula_results = stop - start
print(f"cannula results: {cannula_results}")