Skip to content

Commit 946cb02

Browse files
committed
add benchmarks to php-graphql
1 parent 86b7905 commit 946cb02

File tree

4 files changed

+193
-2
lines changed

4 files changed

+193
-2
lines changed

benchmarks/ParserBench.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use GraphQL\Errors\UnexpectedTokenError;
4+
use GraphQL\Introspection\Introspection;
5+
use GraphQL\Parser\Parser;
6+
7+
/**
8+
* @Warmup(3)
9+
* @Revs(10)
10+
* @Iterations(2)
11+
*/
12+
class ParserBench{
13+
14+
/**
15+
* @throws UnexpectedTokenError
16+
*/
17+
public function benchQueryIntrospection()
18+
{
19+
$query = Introspection::getIntrospectionQuery();
20+
21+
$parser = new Parser();
22+
23+
$parser->parse($query);
24+
$parser->getParsedDocument();
25+
}
26+
}

benchmarks/StarWarsBench.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
use GraphQL\Errors\BadImplementationError;
4+
use GraphQL\Errors\GraphQLError;
5+
use GraphQL\Errors\UnexpectedTokenError;
6+
use GraphQL\Execution\Executor;
7+
use GraphQL\Introspection\Introspection;
8+
use GraphQL\Parser\Parser;
9+
use GraphQL\Tests\StarWarsSchema;
10+
use GraphQL\Validation\Validator;
11+
12+
/**
13+
* @Warmup(3)
14+
* @Revs(10)
15+
* @Iterations(2)
16+
*/
17+
class StarWarsBench{
18+
/**
19+
* @param $query
20+
* @throws BadImplementationError
21+
* @throws GraphQLError
22+
* @throws UnexpectedTokenError
23+
*/
24+
public function validateAndExecuteQuery($query)
25+
{
26+
$schema = StarWarsSchema::buildSchema();
27+
28+
$parser = new Parser();
29+
$validator = new Validator();
30+
$executor = new Executor();
31+
32+
$parser->parse($query);
33+
$document = $parser->getParsedDocument();
34+
35+
$validator->validate($schema, $document);
36+
37+
$executor->execute($schema, $document);
38+
}
39+
40+
/**
41+
* @throws BadImplementationError
42+
* @throws GraphQLError
43+
*/
44+
public function benchBuildSchema()
45+
{
46+
StarWarsSchema::buildSchema();
47+
}
48+
49+
/**
50+
* @throws BadImplementationError
51+
* @throws GraphQLError
52+
* @throws UnexpectedTokenError
53+
*/
54+
public function benchHeroQuery()
55+
{
56+
$query = '
57+
query HeroNameQuery {
58+
hero {
59+
name
60+
}
61+
}
62+
';
63+
64+
$this->validateAndExecuteQuery($query);
65+
}
66+
67+
/**
68+
* @throws BadImplementationError
69+
* @throws GraphQLError
70+
* @throws UnexpectedTokenError
71+
*/
72+
public function benchNestedQuery()
73+
{
74+
$query = '
75+
query NestedQuery {
76+
hero {
77+
name
78+
friends {
79+
name
80+
appearsIn
81+
friends {
82+
name
83+
}
84+
}
85+
}
86+
}
87+
';
88+
89+
$this->validateAndExecuteQuery($query);
90+
}
91+
92+
/**
93+
* @throws BadImplementationError
94+
* @throws GraphQLError
95+
* @throws UnexpectedTokenError
96+
*/
97+
public function benchQueryWithFragment()
98+
{
99+
$query = '
100+
query UseFragment {
101+
luke: human(id: "1000") {
102+
...HumanFragment
103+
}
104+
leia: human(id: "1003") {
105+
...HumanFragment
106+
}
107+
}
108+
fragment HumanFragment on Human {
109+
name
110+
homePlanet
111+
}
112+
';
113+
114+
$this->validateAndExecuteQuery($query);
115+
}
116+
117+
/**
118+
* @throws BadImplementationError
119+
* @throws GraphQLError
120+
* @throws UnexpectedTokenError
121+
*/
122+
public function benchQueryWithInterfaceFragment()
123+
{
124+
$query = '
125+
query UseInterfaceFragment {
126+
luke: human(id: "1000") {
127+
...CharacterFragment
128+
}
129+
leia: human(id: "1003") {
130+
...CharacterFragment
131+
}
132+
}
133+
fragment CharacterFragment on Character {
134+
name
135+
}
136+
';
137+
138+
$this->validateAndExecuteQuery($query);
139+
}
140+
141+
/**
142+
* @throws BadImplementationError
143+
* @throws GraphQLError
144+
* @throws UnexpectedTokenError
145+
*/
146+
public function benchQueryIntrospection()
147+
{
148+
$query = Introspection::getIntrospectionQuery();
149+
150+
$this->validateAndExecuteQuery($query);
151+
}
152+
153+
154+
}

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
},
2424
"autoload": {
2525
"psr-4": {
26-
"GraphQL\\": "src/",
27-
"GraphQL\\Tests\\": "tests/"
26+
"GraphQL\\": "src/"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"GraphQL\\Tests\\": "tests/",
32+
"GraphQL\\Benchmarks\\": "tests/"
2833
}
2934
},
3035
"scripts": {

phpbench.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"runner.bootstrap": "vendor/autoload.php",
3+
"runner.path": "benchmarks",
4+
"runner.retry_threshold": 5,
5+
"runner.time_unit": "milliseconds"
6+
}

0 commit comments

Comments
 (0)