Skip to content

Commit 2c9ae0c

Browse files
committed
perf: initial PHPBench integration
1 parent 1225c64 commit 2c9ae0c

File tree

7 files changed

+136
-0
lines changed

7 files changed

+136
-0
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ psalm: ensure
4444
phpunit:
4545
sh -c "${PHPQA_DOCKER_COMMAND} phpunit-7 --verbose"
4646

47+
benchmark: ensure
48+
sh -c "${PHPQA_DOCKER_COMMAND} phpbench run --iterations=10 --report=aggregate -v"
49+
4750
phpunit-coverage: ensure
4851
sh -c "${PHPQA_DOCKER_COMMAND} phpdbg -qrr /tools/phpunit-7 --verbose --coverage-text --log-junit=var/phpunit.junit.xml --coverage-xml var/coverage-xml/"
4952

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"nicmart/tree": "^0.2,>= 0.2.4",
2727
"nyholm/nsa": "^1.1",
2828
"nyholm/symfony-bundle-test": "^1.4",
29+
"phpbench/phpbench": "^0.16",
2930
"phpoffice/phpspreadsheet": "^1.4,>= 1.4.1",
3031
"phpunit/phpunit": "^7.0",
3132
"symfony/console": "^3.4 || ^4.0",

phpbench.json.dist

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
"extensions": [
10+
"PhpBench\\Extensions\\Dbal\\DbalExtension",
11+
"PhpBench\\Extensions\\XDebug\\XDebugExtension"
12+
],
13+
"storage": "dbal",
14+
"storage.dbal.connection": {
15+
"driver": "pdo_sqlite",
16+
"path": "var/phpbench-history.db"
17+
}
18+
}

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ parameters:
2525
paths:
2626
- %currentWorkingDirectory%/tests/Xezilaires/Bridge/Spout/SpreadsheetTest.php
2727
- %currentWorkingDirectory%/tests/Xezilaires/Functional/FunctionalTestCase.php
28+
- %currentWorkingDirectory%/tests/Xezilaires/Performance/PerformanceTestCase.php

resources/fixtures/massive.xlsx

1.57 MB
Binary file not shown.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
18+
use Symfony\Component\Serializer\Serializer;
19+
use Xezilaires\Bridge\Symfony\Serializer\ObjectSerializer;
20+
use Xezilaires\Metadata\ColumnReference;
21+
use Xezilaires\Metadata\Mapping;
22+
use Xezilaires\Spreadsheet;
23+
use Xezilaires\SpreadsheetIterator;
24+
use Xezilaires\Test\FixtureTrait;
25+
use Xezilaires\Test\Model\Product;
26+
27+
/**
28+
* @Bench\BeforeMethods({"setUp"})
29+
*/
30+
abstract class PerformanceTestCase
31+
{
32+
use FixtureTrait;
33+
34+
/**
35+
* @var Mapping
36+
*/
37+
private $mapping;
38+
39+
public function setUp(): void
40+
{
41+
$this->mapping = new Mapping(
42+
Product::class,
43+
[
44+
'name' => new ColumnReference('A'),
45+
'price' => new ColumnReference('B'),
46+
],
47+
[
48+
'start' => 2,
49+
]
50+
);
51+
}
52+
53+
/**
54+
* @Bench\Assert(stat="mean", value="10")
55+
* @Bench\Revs(1000)
56+
*/
57+
public function benchIteratorConstruction(): void
58+
{
59+
$this->getSpreadsheet($this->fixture('massive.xlsx'));
60+
}
61+
62+
/**
63+
* @Bench\Assert(stat="mean", value="10")
64+
* @Bench\Revs(1000)
65+
*/
66+
public function benchIteratorIteration(): void
67+
{
68+
$iterator = $this->createIterator($this->getSpreadsheet($this->fixture('products.xlsx')), $this->mapping);
69+
70+
foreach ($iterator as $row) {
71+
}
72+
}
73+
74+
abstract protected function getSpreadsheet(\SplFileObject $file): Spreadsheet;
75+
76+
private function createIterator(Spreadsheet $spreadsheet, Mapping $mapping): SpreadsheetIterator
77+
{
78+
$serializer = new ObjectSerializer(new Serializer([new ObjectNormalizer()]));
79+
80+
return new SpreadsheetIterator($spreadsheet, $mapping, $serializer);
81+
}
82+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Spout;
15+
16+
use PhpBench\Benchmark\Metadata\Annotations as Bench;
17+
use Xezilaires\Spreadsheet;
18+
use Xezilaires\Test\Performance\PerformanceTestCase;
19+
20+
/**
21+
* @internal
22+
*
23+
* @small
24+
*/
25+
final class SpoutSpreadsheetIteratorBench extends PerformanceTestCase
26+
{
27+
protected function getSpreadsheet(\SplFileObject $file): Spreadsheet
28+
{
29+
return new \Xezilaires\Bridge\Spout\Spreadsheet($file);
30+
}
31+
}

0 commit comments

Comments
 (0)