Skip to content

Commit

Permalink
Added benchmarks (Graph vs. native SimpleXML)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayne committed Mar 10, 2017
1 parent 4783ce9 commit 2c5f795
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 2 deletions.
1 change: 1 addition & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

$finder = Symfony\CS\Finder\DefaultFinder::create()
->in([
__DIR__ . DIRECTORY_SEPARATOR . 'benchmarks',
__DIR__ . DIRECTORY_SEPARATOR . 'src',
__DIR__ . DIRECTORY_SEPARATOR . 'tests',
]);
Expand Down
36 changes: 36 additions & 0 deletions benchmarks/BaseCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* (c) Dennis Meckel
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rayne\wz2008\Graph\Benchmark;

use RuntimeException;

abstract class BaseCase
{
/**
* @param $expected
* @param $actual
* @throws RuntimeException
*/
protected function assertSame($expected, $actual)
{
if ($expected !== $actual) {
throw new RuntimeException(sprintf('Expected `%s` but got `%s`.', $expected, $actual));
}
}

/**
* @param string $file
* @return string
*/
protected function getAssetFile($file)
{
return dirname(__DIR__) . '/assets/' . $file;
}
}
24 changes: 24 additions & 0 deletions benchmarks/Graph/GraphInitializationBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* (c) Dennis Meckel
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rayne\wz2008\Graph\Benchmark\Graph;

use Rayne\wz2008\Graph\Benchmark\InitializationCase;
use Rayne\wz2008\Graph\Factory\WzClassificationFactory;

class GraphInitializationBench extends InitializationCase
{
/**
* @inheritdoc
*/
public function benchInitialization()
{
WzClassificationFactory::build();
}
}
72 changes: 72 additions & 0 deletions benchmarks/Graph/GraphParentItemAccessBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* (c) Dennis Meckel
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rayne\wz2008\Graph\Benchmark\Graph;

use Rayne\wz2008\Graph\Benchmark\ParentItemAccessCase;
use Rayne\wz2008\Graph\Factory\WzClassificationFactory;
use Rayne\wz2008\Graph\WzClassificationInterface;
use Rayne\wz2008\Graph\WzItemInterface;

class GraphParentItemAccessBench extends ParentItemAccessCase
{
/**
* @var null|WzItemInterface
*/
private $child;

/**
* @var WzClassificationInterface
*/
private $graph;

/**
*
*/
public function __construct()
{
$this->graph = WzClassificationFactory::build();
}

/**
* @inheritdoc
*/
public function benchColdParentItemAccess(array $params)
{
$this->assertSame(
$params['parent'],
$this->graph->get($params['child'])->getParent()->getId());
}

/**
* @inheritdoc
*/
public function beforeWarmParentItemAccess(array $params)
{
$this->child = $this->graph->get($params['child']);
}

/**
* @inheritdoc
*/
public function benchWarmParentItemAccess(array $params)
{
$this->assertSame(
$params['parent'],
$this->child->getParent()->getId());
}

/**
* @inheritdoc
*/
public function afterWarmParentItemAccess(array $params)
{
$this->child = null;
}
}
38 changes: 38 additions & 0 deletions benchmarks/Graph/GraphRandomItemAccessBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* (c) Dennis Meckel
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rayne\wz2008\Graph\Benchmark\Graph;

use Rayne\wz2008\Graph\Benchmark\RandomItemAccessCase;
use Rayne\wz2008\Graph\Factory\WzClassificationFactory;
use Rayne\wz2008\Graph\WzClassificationInterface;

class GraphRandomItemAccessBench extends RandomItemAccessCase
{
/**
* @var WzClassificationInterface
*/
public $graph;

/**
*
*/
public function __construct()
{
$this->graph = WzClassificationFactory::build();
}

/**
* @inheritdoc
*/
public function benchRandomItemAccess($params)
{
$this->assertSame($params['child'], $this->graph->get('id'));
}
}
27 changes: 27 additions & 0 deletions benchmarks/InitializationCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* (c) Dennis Meckel
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rayne\wz2008\Graph\Benchmark;

use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;

abstract class InitializationCase extends BaseCase
{
/**
* @Groups({"Initialization"})
*
* @Warmup(1)
* @Revs(33)
* @Iterations(3)
*/
abstract public function benchInitialization();
}
70 changes: 70 additions & 0 deletions benchmarks/ParentItemAccessCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* (c) Dennis Meckel
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rayne\wz2008\Graph\Benchmark;

use PhpBench\Benchmark\Metadata\Annotations\AfterMethods;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;

abstract class ParentItemAccessCase extends BaseCase
{
/**
* @return array[]
*/
public function provideChildAndParentIds()
{
return [
['child' => '74.10.2', 'parent' => '74.10'],
['child' => '74.10', 'parent' => '74.1'],
['child' => '74.1', 'parent' => '74'],
['child' => '74', 'parent' => 'M'],
];
}

/**
* @Groups({"Parent Access"})
* @ParamProviders({"provideChildAndParentIds"})
*
* @Warmup(1)
* @Revs(33)
* @Iterations(3)
*
* @param array $params
*/
abstract public function benchColdParentItemAccess(array $params);

/**
* @param array $params
*/
abstract public function beforeWarmParentItemAccess(array $params);

/**
* @Groups({"Parent Access"})
* @ParamProviders({"provideChildAndParentIds"})
* @BeforeMethods({"beforeWarmParentItemAccess"})
* @AfterMethods({"afterWarmParentItemAccess"})
*
* @Warmup(1)
* @Revs(33)
* @Iterations(3)
*
* @param array $params
*/
abstract public function benchWarmParentItemAccess(array $params);

/**
* @param array $params
*/
abstract public function afterWarmParentItemAccess(array $params);
}
45 changes: 45 additions & 0 deletions benchmarks/RandomItemAccessCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* (c) Dennis Meckel
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rayne\wz2008\Graph\Benchmark;

use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;

abstract class RandomItemAccessCase extends BaseCase
{
/**
* @Groups({"Random Access"})
* @ParamProviders({"provideRandomItemAccessIds"})
*
* @Warmup(1)
* @Revs(33)
* @Iterations(3)
*
* @param $params
*/
abstract public function benchRandomItemAccess($params);

/**
* @return array[]
*/
public function provideRandomItemAccessIds()
{
$result = [];

foreach (range('A', 'U') as $levelOneId) {
$result[] = ['id' => $levelOneId];
}

return $result;
}
}
23 changes: 23 additions & 0 deletions benchmarks/SimpleXml/SimpleXmlInitializationBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* (c) Dennis Meckel
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rayne\wz2008\Graph\Benchmark\Graph;

use Rayne\wz2008\Graph\Benchmark\InitializationCase;

class SimpleXmlInitializationBench extends InitializationCase
{
/**
* @inheritdoc
*/
public function benchInitialization()
{
simplexml_load_file($this->getAssetFile('WZ2008-2016-07-29-Classification_(complete).xml'));
}
}
Loading

0 comments on commit 2c5f795

Please sign in to comment.