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+ }
0 commit comments