Skip to content

Commit 8dd7a9c

Browse files
committed
perf: initial PHPBench integration
1 parent 60ca197 commit 8dd7a9c

File tree

6 files changed

+140
-13
lines changed

6 files changed

+140
-13
lines changed

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ endif
55
QA_DOCKER_IMAGE=jakzal/phpqa:${BUILD_ENV}-alpine
66
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}
77

8-
dist: composer-validate cs phpstan psalm test
9-
ci: check test
8+
dist: composer-validate cs phpstan psalm test benchmark
9+
ci: check test benchmark
1010
check: composer-validate cs-check phpstan psalm
1111
test: phpunit-coverage infection
1212

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

40-
phpunit:
40+
phpunit: ensure
4141
sh -c "${QA_DOCKER_COMMAND} phpunit --verbose"
4242

43+
benchmark: ensure
44+
sh -c "${QA_DOCKER_COMMAND} phpbench run --store --iterations=10 --report=aggregate"
45+
4346
ensure:
4447
mkdir -p ${HOME}/.composer /tmp/tmp-phpqa-$(shell id -u)
4548

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"require-dev": {
3535
"doctrine/annotations": "^1.6",
3636
"nicmart/tree": "^0.2, >= 0.2.4",
37+
"phpbench/phpbench": "^0.14.0",
3738
"phpunit/phpunit": "^7.1",
3839
"symfony/console": "^3.0 | ^4.0",
3940
"symfony/property-access": "^3.0 | ^4.0",

phpbench.json.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"bootstrap": "vendor/autoload.php",
3+
"path": "tests/Xezilaires/Performance",
4+
5+
"retry_threshold": 5,
6+
"progress": "dots",
7+
8+
"annotation_import_use": true,
9+
"php_disable_ini": true,
10+
"extensions": [
11+
"PhpBench\\Extensions\\Dbal\\DbalExtension",
12+
"PhpBench\\Extensions\\XDebug\\XDebugExtension"
13+
],
14+
"storage": "dbal",
15+
"storage.dbal.connection": {
16+
"driver": "pdo_sqlite",
17+
"path": "var/phpbench-history.db"
18+
}
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the xezilaires project.
7+
*
8+
* (c) Dalibor Karlović <dalibor@flexolabs.io>
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Xezilaires\Test;
15+
16+
/**
17+
* Trait FixtureLoaderTrait.
18+
*/
19+
trait FixtureLoaderTrait
20+
{
21+
/**
22+
* @param string $name
23+
*
24+
* @return \SplFileObject
25+
*/
26+
private function fixture(string $name): \SplFileObject
27+
{
28+
return new \SplFileObject(__DIR__.'/../../resources/fixtures/'.$name);
29+
}
30+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the xezilaires project.
7+
*
8+
* (c) Dalibor Karlović <dalibor@flexolabs.io>
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Xezilaires\Test\Performance;
15+
16+
use PhpBench\Benchmark\Metadata\Annotations as Bench;
17+
use Xezilaires\Metadata\ColumnReference;
18+
use Xezilaires\Metadata\Mapping;
19+
use Xezilaires\PhpSpreadsheetIterator;
20+
use Xezilaires\Test\FixtureLoaderTrait;
21+
use Xezilaires\Test\Model\Product;
22+
23+
/**
24+
* @Bench\BeforeMethods({"setUp"})
25+
*/
26+
class PhpSpreadsheetIteratorConstructionBench
27+
{
28+
use FixtureLoaderTrait;
29+
30+
/**
31+
* @var \SplFileObject
32+
*/
33+
private $fixture;
34+
35+
public function setUp(): void
36+
{
37+
$this->fixture = $this->fixture('products.xls');
38+
}
39+
40+
/**
41+
* @Bench\Assert(stat="mean", value="10")
42+
* @Bench\Revs(1000)
43+
*/
44+
public function benchIteratorConstruction(): void
45+
{
46+
new PhpSpreadsheetIterator(
47+
$this->fixture,
48+
new Mapping(
49+
Product::class,
50+
[
51+
'name' => new ColumnReference('A'),
52+
'price' => new ColumnReference('B'),
53+
],
54+
[
55+
'start' => 2,
56+
]
57+
)
58+
);
59+
}
60+
61+
/**
62+
* @Bench\Assert(stat="mean", value="1500")
63+
* @Bench\Revs(100)
64+
*/
65+
public function benchIteratorIteration(): void
66+
{
67+
$iterator = new PhpSpreadsheetIterator(
68+
$this->fixture,
69+
new Mapping(
70+
Product::class,
71+
[
72+
'name' => new ColumnReference('A'),
73+
'price' => new ColumnReference('B'),
74+
],
75+
[
76+
'start' => 2,
77+
]
78+
)
79+
);
80+
81+
iterator_to_array($iterator);
82+
}
83+
}

tests/Xezilaires/PhpSpreadsheetIteratorTest.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*/
3636
class PhpSpreadsheetIteratorTest extends TestCase
3737
{
38+
use FixtureLoaderTrait;
3839
use IteratorMatcherTrait;
3940

4041
public function testCanLoadFlatFixtureWithColumnReference(): void
@@ -289,16 +290,6 @@ public function testCanRewindIterator(): void
289290
static::assertEquals(0, $iterator->key());
290291
}
291292

292-
/**
293-
* @param string $name
294-
*
295-
* @return \SplFileObject
296-
*/
297-
private function fixture(string $name): \SplFileObject
298-
{
299-
return new \SplFileObject(__DIR__.'/../../resources/fixtures/'.$name);
300-
}
301-
302293
/**
303294
* @param string $name
304295
*

0 commit comments

Comments
 (0)