Skip to content

Commit

Permalink
perf: initial PHPBench integration
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarlovi committed Sep 8, 2018
1 parent 60ca197 commit 8dd7a9c
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 13 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ endif
QA_DOCKER_IMAGE=jakzal/phpqa:${BUILD_ENV}-alpine
QA_DOCKER_COMMAND=docker run --init --interactive --tty --rm --env "COMPOSER_HOME=/composer" --user "$(shell id -u):$(shell id -g)" --volume /tmp/tmp-phpqa-$(shell id -u):/tmp --volume "$(shell pwd):/project" --volume "${HOME}/.composer:/composer" --workdir /project ${QA_DOCKER_IMAGE}

dist: composer-validate cs phpstan psalm test
ci: check test
dist: composer-validate cs phpstan psalm test benchmark
ci: check test benchmark
check: composer-validate cs-check phpstan psalm
test: phpunit-coverage infection

Expand Down Expand Up @@ -37,9 +37,12 @@ infection: phpunit-coverage
phpunit-coverage: ensure
sh -c "${QA_DOCKER_COMMAND} phpdbg -qrr vendor/bin/phpunit --verbose --coverage-text --log-junit=var/phpunit.junit.xml --coverage-xml var/coverage-xml/"

phpunit:
phpunit: ensure
sh -c "${QA_DOCKER_COMMAND} phpunit --verbose"

benchmark: ensure
sh -c "${QA_DOCKER_COMMAND} phpbench run --store --iterations=10 --report=aggregate"

ensure:
mkdir -p ${HOME}/.composer /tmp/tmp-phpqa-$(shell id -u)

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"require-dev": {
"doctrine/annotations": "^1.6",
"nicmart/tree": "^0.2, >= 0.2.4",
"phpbench/phpbench": "^0.14.0",
"phpunit/phpunit": "^7.1",
"symfony/console": "^3.0 | ^4.0",
"symfony/property-access": "^3.0 | ^4.0",
Expand Down
19 changes: 19 additions & 0 deletions phpbench.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"bootstrap": "vendor/autoload.php",
"path": "tests/Xezilaires/Performance",

"retry_threshold": 5,
"progress": "dots",

"annotation_import_use": true,
"php_disable_ini": true,
"extensions": [
"PhpBench\\Extensions\\Dbal\\DbalExtension",
"PhpBench\\Extensions\\XDebug\\XDebugExtension"
],
"storage": "dbal",
"storage.dbal.connection": {
"driver": "pdo_sqlite",
"path": "var/phpbench-history.db"
}
}
30 changes: 30 additions & 0 deletions tests/Xezilaires/FixtureLoaderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/*
* This file is part of the xezilaires project.
*
* (c) Dalibor Karlović <dalibor@flexolabs.io>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Xezilaires\Test;

/**
* Trait FixtureLoaderTrait.
*/
trait FixtureLoaderTrait
{
/**
* @param string $name
*
* @return \SplFileObject
*/
private function fixture(string $name): \SplFileObject
{
return new \SplFileObject(__DIR__.'/../../resources/fixtures/'.$name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

/*
* This file is part of the xezilaires project.
*
* (c) Dalibor Karlović <dalibor@flexolabs.io>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Xezilaires\Test\Performance;

use PhpBench\Benchmark\Metadata\Annotations as Bench;
use Xezilaires\Metadata\ColumnReference;
use Xezilaires\Metadata\Mapping;
use Xezilaires\PhpSpreadsheetIterator;
use Xezilaires\Test\FixtureLoaderTrait;
use Xezilaires\Test\Model\Product;

/**
* @Bench\BeforeMethods({"setUp"})
*/
class PhpSpreadsheetIteratorConstructionBench
{
use FixtureLoaderTrait;

/**
* @var \SplFileObject
*/
private $fixture;

public function setUp(): void
{
$this->fixture = $this->fixture('products.xls');
}

/**
* @Bench\Assert(stat="mean", value="10")
* @Bench\Revs(1000)
*/
public function benchIteratorConstruction(): void
{
new PhpSpreadsheetIterator(
$this->fixture,
new Mapping(
Product::class,
[
'name' => new ColumnReference('A'),
'price' => new ColumnReference('B'),
],
[
'start' => 2,
]
)
);
}

/**
* @Bench\Assert(stat="mean", value="1500")
* @Bench\Revs(100)
*/
public function benchIteratorIteration(): void
{
$iterator = new PhpSpreadsheetIterator(
$this->fixture,
new Mapping(
Product::class,
[
'name' => new ColumnReference('A'),
'price' => new ColumnReference('B'),
],
[
'start' => 2,
]
)
);

iterator_to_array($iterator);
}
}
11 changes: 1 addition & 10 deletions tests/Xezilaires/PhpSpreadsheetIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/
class PhpSpreadsheetIteratorTest extends TestCase
{
use FixtureLoaderTrait;
use IteratorMatcherTrait;

public function testCanLoadFlatFixtureWithColumnReference(): void
Expand Down Expand Up @@ -289,16 +290,6 @@ public function testCanRewindIterator(): void
static::assertEquals(0, $iterator->key());
}

/**
* @param string $name
*
* @return \SplFileObject
*/
private function fixture(string $name): \SplFileObject
{
return new \SplFileObject(__DIR__.'/../../resources/fixtures/'.$name);
}

/**
* @param string $name
*
Expand Down

0 comments on commit 8dd7a9c

Please sign in to comment.