Fixed codestyle in benchmarks

This commit is contained in:
Vladimir Razuvaev 2018-11-27 17:22:29 +07:00
parent c45fa1a4b1
commit 627cc786a5
3 changed files with 28 additions and 26 deletions

View File

@ -1,10 +1,11 @@
<?php <?php
namespace GraphQL\Benchmarks; namespace GraphQL\Benchmarks;
use GraphQL\GraphQL;
use GraphQL\Benchmarks\Utils\QueryGenerator; use GraphQL\Benchmarks\Utils\QueryGenerator;
use GraphQL\Benchmarks\Utils\SchemaGenerator; use GraphQL\Benchmarks\Utils\SchemaGenerator;
use GraphQL\GraphQL;
use GraphQL\Type\Schema; use GraphQL\Type\Schema;
use GraphQL\Type\SchemaConfig;
/** /**
* @BeforeMethods({"setUp"}) * @BeforeMethods({"setUp"})
@ -15,16 +16,12 @@ use GraphQL\Type\Schema;
*/ */
class HugeSchemaBench class HugeSchemaBench
{ {
/** /** @var SchemaGenerator */
* @var SchemaGenerator
*/
private $schemaBuilder; private $schemaBuilder;
private $schema; private $schema;
/** /** @var string */
* @var string
*/
private $smallQuery; private $smallQuery;
public function setUp() public function setUp()
@ -33,7 +30,7 @@ class HugeSchemaBench
'totalTypes' => 600, 'totalTypes' => 600,
'fieldsPerType' => 8, 'fieldsPerType' => 8,
'listFieldsPerType' => 2, 'listFieldsPerType' => 2,
'nestingLevel' => 10 'nestingLevel' => 10,
]); ]);
$this->schema = $this->schemaBuilder->buildSchema(); $this->schema = $this->schemaBuilder->buildSchema();
@ -46,8 +43,7 @@ class HugeSchemaBench
{ {
$this->schemaBuilder $this->schemaBuilder
->buildSchema() ->buildSchema()
->getTypeMap() ->getTypeMap();
;
} }
public function benchSchemaLazy() public function benchSchemaLazy()
@ -69,9 +65,9 @@ class HugeSchemaBench
private function createLazySchema() private function createLazySchema()
{ {
return new Schema( return new Schema(
\GraphQL\Type\SchemaConfig::create() SchemaConfig::create()
->setQuery($this->schemaBuilder->buildQueryType()) ->setQuery($this->schemaBuilder->buildQueryType())
->setTypeLoader(function($name) { ->setTypeLoader(function ($name) {
return $this->schemaBuilder->loadType($name); return $this->schemaBuilder->loadType($name);
}) })
); );

View File

@ -1,5 +1,6 @@
<?php <?php
namespace GraphQL\Benchmarks; namespace GraphQL\Benchmarks;
use GraphQL\GraphQL; use GraphQL\GraphQL;
use GraphQL\Tests\StarWarsSchema; use GraphQL\Tests\StarWarsSchema;
use GraphQL\Type\Introspection; use GraphQL\Type\Introspection;

View File

@ -13,6 +13,9 @@ use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\WrappingType; use GraphQL\Type\Definition\WrappingType;
use GraphQL\Type\Schema; use GraphQL\Type\Schema;
use GraphQL\Utils\Utils; use GraphQL\Utils\Utils;
use function count;
use function max;
use function round;
class QueryGenerator class QueryGenerator
{ {
@ -30,9 +33,11 @@ class QueryGenerator
$totalFields = 0; $totalFields = 0;
foreach ($schema->getTypeMap() as $type) { foreach ($schema->getTypeMap() as $type) {
if ($type instanceof ObjectType) { if (! ($type instanceof ObjectType)) {
$totalFields += count($type->getFields()); continue;
} }
$totalFields += count($type->getFields());
} }
$this->maxLeafFields = max(1, round($totalFields * $percentOfLeafFields)); $this->maxLeafFields = max(1, round($totalFields * $percentOfLeafFields));
@ -44,13 +49,12 @@ class QueryGenerator
$qtype = $this->schema->getQueryType(); $qtype = $this->schema->getQueryType();
$ast = new DocumentNode([ $ast = new DocumentNode([
'definitions' => [ 'definitions' => [new OperationDefinitionNode([
new OperationDefinitionNode([
'name' => new NameNode(['value' => 'TestQuery']), 'name' => new NameNode(['value' => 'TestQuery']),
'operation' => 'query', 'operation' => 'query',
'selectionSet' => $this->buildSelectionSet($qtype->getFields()) 'selectionSet' => $this->buildSelectionSet($qtype->getFields()),
]) ]),
] ],
]); ]);
return Printer::doPrint($ast); return Printer::doPrint($ast);
@ -58,12 +62,13 @@ class QueryGenerator
/** /**
* @param FieldDefinition[] $fields * @param FieldDefinition[] $fields
*
* @return SelectionSetNode * @return SelectionSetNode
*/ */
public function buildSelectionSet($fields) public function buildSelectionSet($fields)
{ {
$selections[] = new FieldNode([ $selections[] = new FieldNode([
'name' => new NameNode(['value' => '__typename']) 'name' => new NameNode(['value' => '__typename']),
]); ]);
$this->currentLeafFields++; $this->currentLeafFields++;
@ -87,12 +92,12 @@ class QueryGenerator
$selections[] = new FieldNode([ $selections[] = new FieldNode([
'name' => new NameNode(['value' => $field->name]), 'name' => new NameNode(['value' => $field->name]),
'selectionSet' => $selectionSet 'selectionSet' => $selectionSet,
]); ]);
} }
$selectionSet = new SelectionSetNode([ $selectionSet = new SelectionSetNode([
'selections' => $selections 'selections' => $selections,
]); ]);
return $selectionSet; return $selectionSet;