Skip to content

Commit 5691463

Browse files
authored
Merge pull request #366 from mcg-web/benchmarks
Add some code quality tools
2 parents b8822c6 + 9ae957d commit 5691463

12 files changed

+264
-38
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/tests export-ignore
2+
/benchmarks export-ignore
23
.gitattributes export-ignore
34
.gitignore export-ignore
45
.editorconfig export-ignore

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ composer.lock
66
composer.phar
77
.php_cs.cache
88
.php_cs
9+
10+
phpbench.phar
11+
phpbench.phar.pubkey
12+
/.phpbench_storage
13+
php-cs-fixer.phar

.travis.yml

+46-31
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,68 @@ branches:
1111
- master
1212
- /^\d+\.\d+$/
1313

14-
matrix:
15-
fast_finish: true
14+
cache:
15+
directories:
16+
- $HOME/.composer/cache
17+
- $HOME/.php_cs.cache
18+
- vendor/
19+
20+
before_install:
21+
- if [ "${STABILITY}" != "" ]; then perl -pi -e 's/^}$/,"minimum-stability":"'"${STABILITY}"'"}/' composer.json; fi;
22+
- if [ "${SYMFONY_VERSION}" != "" ]; then perl -pi -e 's#"(symfony/.*)":\s*".*"#"$1":"'"${SYMFONY_VERSION}"'"#' composer.json; fi;
23+
- if [ "${PHPUNIT_VERSION}" != "" ]; then composer req "phpunit/phpunit:${PHPUNIT_VERSION}" --dev --no-update; fi;
24+
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available"
25+
- composer selfupdate
26+
- if [ $GRAPHQLPHP_VERSION ]; then composer require "webonyx/graphql-php:${GRAPHQLPHP_VERSION}" --dev --no-update; fi;
27+
28+
install: travis_retry composer update --prefer-source --no-interaction --optimize-autoloader ${COMPOSER_UPDATE_FLAGS}
29+
30+
script: composer test
31+
32+
jobs:
1633
include:
17-
- php: 5.6
34+
- stage: Test
35+
php: 5.6
1836
env: COMPOSER_UPDATE_FLAGS=--prefer-lowest SYMFONY_DEPRECATIONS_HELPER=disabled GRAPHQLPHP_VERSION=^0.11.2
1937
- php: 5.6
2038
env: SYMFONY_VERSION=3.1.* SYMFONY_DEPRECATIONS_HELPER=disabled GRAPHQLPHP_VERSION=0.12
2139
- php: 7.0
2240
env: SYMFONY_VERSION=3.2.* PHPUNIT_VERSION=^5.7.26
2341
- php: 7.1
24-
- php: 7.2
25-
env: PHP_CS_FIXER=true
2642
- php: 7.2
2743
env: SYMFONY_VERSION=3.3.*
2844
- php: 7.2
2945
env: SYMFONY_VERSION=3.4.*
30-
- php: 7.2
31-
env: SYMFONY_VERSION=4.0.* TEST_COVERAGE=true
3246
- php: 7.2
3347
env: SYMFONY_VERSION=4.1.* STABILITY=beta
3448
- php: nightly
3549
env: COMPOSER_UPDATE_FLAGS=--ignore-platform-reqs
36-
allow_failures:
37-
- php: nightly
38-
env: COMPOSER_UPDATE_FLAGS=--ignore-platform-reqs
39-
40-
cache:
41-
directories:
42-
- $HOME/.composer/cache
43-
- $HOME/.php_cs.cache
44-
- vendor/
50+
- php: 7.2
51+
env: COVERAGE
52+
before_script:
53+
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
54+
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for coverage"; exit 1; fi
55+
script: bin/phpunit --color=always -v --debug -d xdebug.max_nesting_level=1000 --coverage-clover=build/logs/clover.xml
56+
after_script:
57+
- wget https://scrutinizer-ci.com/ocular.phar && travis_retry php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml
58+
- composer require "satooshi/php-coveralls:^1.0" && travis_retry php bin/coveralls -v
4559

46-
before_install:
47-
- if [ "${STABILITY}" != "" ]; then perl -pi -e 's/^}$/,"minimum-stability":"'"${STABILITY}"'"}/' composer.json; fi;
48-
- if [ "${SYMFONY_VERSION}" != "" ]; then perl -pi -e 's#"(symfony/.*)":\s*".*"#"$1":"'"${SYMFONY_VERSION}"'"#' composer.json; fi;
49-
- if [ "${PHPUNIT_VERSION}" != "" ]; then composer req "phpunit/phpunit:${PHPUNIT_VERSION}" --dev --no-update; fi;
50-
- if [ ${TEST_COVERAGE} != true ]; then phpenv config-rm xdebug.ini || true; fi
51-
- composer selfupdate
52-
- if [ $GRAPHQLPHP_VERSION ]; then composer require "webonyx/graphql-php:${GRAPHQLPHP_VERSION}" --dev --no-update; fi;
60+
- stage: Code Quality
61+
php: 7.2
62+
env: STATIC_ANALYSIS
63+
install: travis_retry composer install --prefer-dist
64+
script: composer static-analysis
5365

54-
install: composer update --prefer-source --no-interaction --optimize-autoloader ${COMPOSER_UPDATE_FLAGS}
66+
- stage: Code Quality
67+
php: 7.2
68+
env: CODING_STANDARDS
69+
script: composer check-cs
5570

56-
script:
57-
- composer validate --no-check-lock --strict
58-
- bin/phpunit --debug $( if [ $TEST_COVERAGE = true ]; then echo "-d xdebug.max_nesting_level=1000 --coverage-clover=build/logs/clover.xml"; fi; )
59-
- if [ ${PHP_CS_FIXER} = true ]; then composer require --dev 'friendsofphp/php-cs-fixer:^2.0' && bin/php-cs-fixer fix --diff --dry-run -v; fi;
71+
- stage: Code Quality
72+
php: 7.2
73+
env: BENCHMARK
74+
script: composer bench
6075

61-
after_script:
62-
- if [ ${TEST_COVERAGE} = true ]; then wget https://scrutinizer-ci.com/ocular.phar && travis_retry php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml; fi
63-
- if [ ${TEST_COVERAGE} = true ]; then composer require "satooshi/php-coveralls:^1.0" && travis_retry php bin/coveralls -v; fi
76+
allow_failures:
77+
- php: nightly
78+
env: COMPOSER_UPDATE_FLAGS=--ignore-platform-reqs

CONTRIBUTING.md

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
Contributing
22
============
33

4-
Running tests
4+
Thank you for contributing to this bundle!
5+
6+
Before we can merge your pull request here are some guidelines that you need to follow.
7+
These guidelines exist not to annoy you, but to keep the code base clean,
8+
unified and future proof.
9+
10+
Tests
511
--------------
612

7-
Install [phpunit](https://phpunit.de/manual/current/en/installation.html).
13+
Please try to add a test for your pull request.
814

9-
In the bundle directory:
15+
You can run the tests by calling:
1016

1117
```bash
12-
bin/phpunit
18+
composer test
1319
```
1420

15-
Giving some love to PHP CS
21+
Code quality
1622
---------------------------
1723

24+
Checking code standard, benchmark, and more.
25+
26+
```bash
27+
composer code-quality
28+
```
29+
30+
Coding Standard
31+
----------------
32+
33+
You can use to fix coding standard if needed:
34+
1835
```bash
19-
composer require --dev 'friendsofphp/php-cs-fixer:^2.0'
20-
bin/php-cs-fixer fix
36+
composer fix-cs
2137
```

benchmarks/Benchmark.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Overblog\GraphQLBundle\Benchmarks;
4+
5+
/**
6+
* @BeforeMethods({"setUp"})
7+
* @AfterMethods({"tearDown"})
8+
*/
9+
abstract class Benchmark
10+
{
11+
public function setUp()
12+
{
13+
}
14+
15+
public function tearDown()
16+
{
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Overblog\GraphQLBundle\Benchmarks\Config;
4+
5+
use Overblog\GraphQLBundle\Benchmarks\Benchmark;
6+
use Overblog\GraphQLBundle\Benchmarks\Mock\ContainerBuilder;
7+
use Overblog\GraphQLBundle\Config\Parser\GraphQLParser;
8+
9+
/**
10+
* @Warmup(2)
11+
* @Revs(100)
12+
*/
13+
final class ParserGraphQLParserBench extends Benchmark
14+
{
15+
/** @var ContainerBuilder */
16+
private $container;
17+
18+
public function setUp()
19+
{
20+
$this->container = new ContainerBuilder();
21+
}
22+
23+
public function benchParse()
24+
{
25+
GraphQLParser::parse(new \SplFileInfo(__DIR__.'/../fixtures/schema.graphql'), $this->container);
26+
}
27+
}

benchmarks/Mock/ContainerBuilder.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Overblog\GraphQLBundle\Benchmarks\Mock;
4+
5+
use Symfony\Component\Config\Resource\ResourceInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder as BaseContainerBuilder;
7+
8+
class ContainerBuilder extends BaseContainerBuilder
9+
{
10+
public function __construct()
11+
{
12+
}
13+
14+
public function addResource(ResourceInterface $resource)
15+
{
16+
return $this;
17+
}
18+
}

benchmarks/Request/ParserBench.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Overblog\GraphQLBundle\Benchmarks\Request;
4+
5+
use Overblog\GraphQLBundle\Benchmarks\Benchmark;
6+
use Overblog\GraphQLBundle\Request\Parser;
7+
use Symfony\Component\HttpFoundation\Request;
8+
9+
/**
10+
* @Warmup(2)
11+
* @Revs(100)
12+
*/
13+
final class ParserBench extends Benchmark
14+
{
15+
/** @var Parser */
16+
private $parser;
17+
18+
public function setUp()
19+
{
20+
$this->parser = new Parser();
21+
}
22+
23+
/**
24+
* @ParamProviders({"requestProvider"})
25+
*
26+
* @param array $args
27+
*/
28+
public function benchParse(array $args)
29+
{
30+
$this->parser->parse(new Request(...$args));
31+
}
32+
33+
public function requestProvider()
34+
{
35+
yield [['query' => '{ foo }']];
36+
yield [['query' => 'query bar { foo }', 'variables' => '{"baz": "bar"}', 'operationName' => 'bar']];
37+
yield [[], ['variables' => '{"baz": "bar"}', 'operationName' => 'bar'], [], [], [], ['CONTENT_TYPE' => 'application/graphql'], '{ foo }'];
38+
}
39+
}

benchmarks/fixtures/schema.graphql

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Root Query"""
2+
type Query {
3+
hero(
4+
"""Episode list to use to filter"""
5+
episodes: [Episode!]! = [NEWHOPE, EMPIRE]
6+
): Character
7+
"""search for a droid"""
8+
droid(id: ID!): Droid
9+
}
10+
11+
type Starship {
12+
id: ID!
13+
name: String!
14+
length(unit: LengthUnit = METER): Float
15+
}
16+
17+
enum Episode {
18+
NEWHOPE
19+
EMPIRE
20+
JEDI
21+
}
22+
23+
interface Character {
24+
id: ID!
25+
name: String!
26+
friends: [Character]
27+
appearsIn: [Episode]!
28+
}
29+
30+
type Human implements Character {
31+
id: ID!
32+
name: String!
33+
friends: [Character]
34+
appearsIn: [Episode]!
35+
starships: [Starship]
36+
totalCredits: Int
37+
}
38+
39+
type Droid implements Character {
40+
id: ID!
41+
name: String!
42+
friends: [Character]
43+
appearsIn: [Episode]!
44+
primaryFunction: String
45+
}
46+
47+
union SearchResult = Human | Droid | Starship
48+
49+
input ReviewInput {
50+
stars: Int! = 5
51+
commentary: String = null
52+
}
53+
54+
scalar Year

composer.json

+29
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"autoload-dev": {
1919
"psr-4": {
20+
"Overblog\\GraphQLBundle\\Benchmarks\\": "benchmarks",
2021
"Overblog\\GraphQLBundle\\Tests\\": "tests"
2122
},
2223
"files": [
@@ -65,5 +66,33 @@
6566
"branch-alias": {
6667
"dev-master": "0.11-dev"
6768
}
69+
},
70+
"scripts": {
71+
"test": "bin/phpunit --color=always -v --debug",
72+
"static-analysis": [
73+
"@composer req --ansi --dev phpstan/phpstan-shim",
74+
"bin/phpstan.phar analyse --ansi -l 1 -c phpstan.neon src",
75+
"@composer rem --ansi --dev phpstan/phpstan-shim"
76+
],
77+
"bench": [
78+
"test -f phpbench.phar || wget https://phpbench.github.io/phpbench/phpbench.phar https://phpbench.github.io/phpbench/phpbench.phar.pubkey",
79+
"@php phpbench.phar run -l dots --ansi -vvv --report='generator: \"table\", cols: [\"benchmark\", \"subject\", \"params\", \"best\", \"mean\", \"mode\", \"worst\", \"diff\"], break: [\"benchmark\"], sort: {mean: \"asc\"}'"
80+
],
81+
"install-cs": "test -f php-cs-fixer.phar || wget https://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -O php-cs-fixer.phar",
82+
"fix-cs": [
83+
"@install-cs",
84+
"@php php-cs-fixer.phar fix --diff -v --ansi"
85+
],
86+
"check-cs": [
87+
"@install-cs",
88+
"@php php-cs-fixer.phar fix --dry-run --diff -v --ansi"
89+
],
90+
"code-quality": [
91+
"rm composer.lock",
92+
"@composer install --ansi",
93+
"@static-analysis",
94+
"@bench",
95+
"@check-cs"
96+
]
6897
}
6998
}

phpbench.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"bootstrap": "vendor/autoload.php",
3+
"path": "benchmarks"
4+
}

phpstan.neon

Whitespace-only changes.

0 commit comments

Comments
 (0)