-
-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathTrustResultBench.php
More file actions
294 lines (257 loc) · 9.29 KB
/
Copy pathTrustResultBench.php
File metadata and controls
294 lines (257 loc) · 9.29 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php declare(strict_types=1);
namespace GraphQL\Benchmarks;
use GraphQL\GraphQL;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\Parser;
use GraphQL\Language\Source;
use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
/**
* Benchmarks comparing query execution performance with and without $trustResult.
*
* $trustResult=true skips per-field/per-object:
* - Leaf value serialization (scalar type conversions)
* - isTypeOf validation checks
*
* Scenarios:
* 1. Built-in scalars at scale — 500 items × 8 fields = 4 000 serialize() calls
* 2. Custom scalar with non-trivial serialization — shows benefit when serialize() does real work
* 3. isTypeOf validation — 100 objects each triggering an isTypeOf() callback
* 4. Realistic combined — scalars + nested objects in a typical "list page" query
*
* @BeforeMethods({"setUp"})
*
* @OutputTimeUnit("milliseconds", precision=3)
*
* @Warmup(5)
*
* @Revs(200)
*
* @Iterations(10)
*/
class TrustResultBench
{
private Schema $builtinScalarSchema;
private DocumentNode $builtinScalarQuery;
private Schema $customScalarSchema;
private DocumentNode $customScalarQuery;
private Schema $typeOfSchema;
private DocumentNode $typeOfQuery;
private Schema $combinedSchema;
private DocumentNode $combinedQuery;
public function setUp(): void
{
$this->setupBuiltinScalarScenario();
$this->setupCustomScalarScenario();
$this->setupTypeOfScenario();
$this->setupCombinedScenario();
}
/**
* Scenario 1: 500 items × 8 built-in scalar fields = 4 000 serialize() calls.
* Built-in serialize() is cheap (type coercion), so gains here are modest.
*/
private function setupBuiltinScalarScenario(): void
{
$items = array_fill(0, 500, [
'id' => 1,
'name' => 'Widget',
'price' => 9.99,
'active' => true,
'stock' => 42,
'rating' => 4.5,
'views' => 1337,
'score' => 99,
]);
$productType = new ObjectType([
'name' => 'Product',
'fields' => [
'id' => ['type' => Type::int()],
'name' => ['type' => Type::string()],
'price' => ['type' => Type::float()],
'active' => ['type' => Type::boolean()],
'stock' => ['type' => Type::int()],
'rating' => ['type' => Type::float()],
'views' => ['type' => Type::int()],
'score' => ['type' => Type::int()],
],
]);
$this->builtinScalarSchema = new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'products' => [
'type' => Type::listOf($productType),
'resolve' => static fn (): array => $items,
],
],
]),
]);
$this->builtinScalarQuery = Parser::parse(new Source(
'{ products { id name price active stock rating views score } }'
));
}
public function benchBuiltinScalarFields(): void
{
GraphQL::executeQuery($this->builtinScalarSchema, $this->builtinScalarQuery);
}
public function benchBuiltinScalarFieldsTrusted(): void
{
GraphQL::executeQuery($this->builtinScalarSchema, $this->builtinScalarQuery, null, null, null, null, null, null, true);
}
/**
* Scenario 2: 100 items × 5 custom scalar fields. The custom serialize() does real work
* (slug normalisation + validation), making the per-field cost measurable.
*/
private function setupCustomScalarScenario(): void
{
$items = array_fill(0, 100, [
'code' => 'WIDGET-001',
'slug' => 'my-product-slug',
'ref' => 'REF-ABC-123',
'tag' => 'electronics/gadgets',
'sku' => 'SKU-XYZ-999',
]);
// A scalar that lowercases and strips non-alphanumeric characters on serialize.
$slugScalar = new CustomScalarType([
'name' => 'Slug',
'serialize' => static fn ($value): string => strtolower(
preg_replace('/[^a-z0-9\-]/i', '-', (string) $value) ?? (string) $value
),
'parseValue' => static fn ($value): string => (string) $value,
'parseLiteral' => static fn ($ast): string => $ast->value,
]);
$itemType = new ObjectType([
'name' => 'Item',
'fields' => [
'code' => ['type' => $slugScalar],
'slug' => ['type' => $slugScalar],
'ref' => ['type' => $slugScalar],
'tag' => ['type' => $slugScalar],
'sku' => ['type' => $slugScalar],
],
]);
$this->customScalarSchema = new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'items' => [
'type' => Type::listOf($itemType),
'resolve' => static fn (): array => $items,
],
],
]),
]);
$this->customScalarQuery = Parser::parse(new Source(
'{ items { code slug ref tag sku } }'
));
}
public function benchCustomScalarFields(): void
{
GraphQL::executeQuery($this->customScalarSchema, $this->customScalarQuery);
}
public function benchCustomScalarFieldsTrusted(): void
{
GraphQL::executeQuery($this->customScalarSchema, $this->customScalarQuery, null, null, null, null, null, null, true);
}
/**
* Scenario 3: 100 objects each triggering an isTypeOf() callback.
* trustResult skips all isTypeOf checks.
*/
private function setupTypeOfScenario(): void
{
$users = array_fill(0, 100, [
'__typename' => 'User',
'id' => 1,
'name' => 'Alice',
'email' => 'alice@example.com',
]);
$userType = new ObjectType([
'name' => 'User',
'fields' => [
'id' => ['type' => Type::int()],
'name' => ['type' => Type::string()],
'email' => ['type' => Type::string()],
],
'isTypeOf' => static fn ($value): bool => is_array($value)
&& ($value['__typename'] ?? null) === 'User',
]);
$this->typeOfSchema = new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'users' => [
'type' => Type::listOf($userType),
'resolve' => static fn (): array => $users,
],
],
]),
]);
$this->typeOfQuery = Parser::parse(new Source('{ users { id name email } }'));
}
public function benchIsTypeOf(): void
{
GraphQL::executeQuery($this->typeOfSchema, $this->typeOfQuery);
}
public function benchIsTypeOfTrusted(): void
{
GraphQL::executeQuery($this->typeOfSchema, $this->typeOfQuery, null, null, null, null, null, null, true);
}
/**
* Scenario 4: 30 orders with a nested customer object and 5 scalar fields each.
* Combines scalar serialization (30 × 8 = 240 calls) and nested object resolution.
*/
private function setupCombinedScenario(): void
{
$orders = array_fill(0, 30, [
'id' => 1,
'status' => 'shipped',
'total' => 99.99,
'quantity' => 3,
'note' => 'fragile',
'customer' => ['id' => 42, 'name' => 'Bob', 'tier' => 'gold'],
]);
$customerType = new ObjectType([
'name' => 'Customer',
'fields' => [
'id' => ['type' => Type::int()],
'name' => ['type' => Type::string()],
'tier' => ['type' => Type::string()],
],
]);
$orderType = new ObjectType([
'name' => 'Order',
'fields' => [
'id' => ['type' => Type::int()],
'status' => ['type' => Type::string()],
'total' => ['type' => Type::float()],
'quantity' => ['type' => Type::int()],
'note' => ['type' => Type::string()],
'customer' => ['type' => $customerType],
],
]);
$this->combinedSchema = new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'orders' => [
'type' => Type::listOf($orderType),
'resolve' => static fn (): array => $orders,
],
],
]),
]);
$this->combinedQuery = Parser::parse(new Source(
'{ orders { id status total quantity note customer { id name tier } } }'
));
}
public function benchCombinedQuery(): void
{
GraphQL::executeQuery($this->combinedSchema, $this->combinedQuery);
}
public function benchCombinedQueryTrusted(): void
{
GraphQL::executeQuery($this->combinedSchema, $this->combinedQuery, null, null, null, null, null, null, true);
}
}