Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from Ocramius/feature/#64-additional-aliases-pe…
Browse files Browse the repository at this point in the history
…rformance-optimization

Feature - #64 additional aliases performance optimization
  • Loading branch information
snapshotpl authored and Ocramius committed Jan 24, 2016
2 parents 39f87dc + 8ad06a8 commit 7471eba
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 145 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ retrieving other objects.
## Benchmarks

We provide scripts for benchmarking zend-servicemanager using the
[Athletic](https://github.com/polyfractal/athletic) framework; these can be
[PHPBench](https://github.com/phpbench/phpbench) framework; these can be
found in the `benchmarks/` directory.

To execute the benchmarks you can run the following command:

```bash
$ vendor/bin/athletic -p benchmarks
$ vendor/bin/phpbench run --report=aggregate
```
108 changes: 108 additions & 0 deletions benchmarks/FetchCachedServicesBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace ZendBench\ServiceManager;

use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use Zend\ServiceManager\ServiceManager;

/**
* @Revs(1000)
* @Iterations(20)
* @Warmup(2)
*/
class FetchCachedServicesBench
{
/**
* @var ServiceManager
*/
private $sm;

public function __construct()
{
$this->sm = new ServiceManager([
'factories' => [
'factory1' => BenchAsset\FactoryFoo::class,
],
'invokables' => [
'invokable1' => BenchAsset\Foo::class,
],
'services' => [
'service1' => new \stdClass(),
],
'aliases' => [
'alias1' => 'service1',
'recursiveAlias1' => 'alias1',
'recursiveAlias2' => 'recursiveAlias1',
],
'abstract_factories' => [
BenchAsset\AbstractFactoryFoo::class
]
]);

// forcing initialization of all the services
$this->sm->get('factory1');
$this->sm->get('invokable1');
$this->sm->get('service1');
$this->sm->get('alias1');
$this->sm->get('recursiveAlias1');
$this->sm->get('recursiveAlias2');
}

public function benchFetchFactory1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->get('factory1');
}

public function benchFetchInvokable1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->get('invokable1');
}

public function benchFetchService1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->get('service1');
}

public function benchFetchAlias1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->get('alias1');
}

public function benchFetchRecursiveAlias1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->get('recursiveAlias1');
}

public function benchFetchRecursiveAlias2()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->get('recursiveAlias2');
}

public function benchFetchAbstractFactoryService()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->get('foo');
}
}
51 changes: 51 additions & 0 deletions benchmarks/FetchNewServiceManagerBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace ZendBench\ServiceManager;

use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use Zend\ServiceManager\ServiceManager;

/**
* @Revs(100)
* @Iterations(20)
* @Warmup(2)
*/
class FetchNewServiceManagerBench
{
const NUM_SERVICES = 1000;

/**
* @var array
*/
private $config = [];

public function __construct()
{
$config = [
'factories' => [],
'invokables' => [],
'services' => [],
'aliases' => [],
'abstract_factories' => [
BenchAsset\AbstractFactoryFoo::class,
],
];

$service = new \stdClass();

for ($i = 0; $i <= self::NUM_SERVICES; $i++) {
$config['factories']["factory_$i"] = BenchAsset\FactoryFoo::class;
$config['invokables']["invokable_$i"] = BenchAsset\Foo::class;
$config['services']["service_$i"] = $service;
$config['aliases']["alias_$i"] = "service_$i";
}
$this->config = $config;
}

public function benchFetchServiceManagerCreation()
{
new ServiceManager($this->config);
}
}
148 changes: 148 additions & 0 deletions benchmarks/FetchNewServicesBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace ZendBench\ServiceManager;

use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use Zend\ServiceManager\ServiceManager;

/**
* @Revs(1000)
* @Iterations(10)
* @Warmup(2)
*/
class FetchNewServicesBench
{
/**
* @var ServiceManager
*/
private $sm;

public function __construct()
{
$this->sm = new ServiceManager([
'factories' => [
'factory1' => BenchAsset\FactoryFoo::class,
],
'invokables' => [
'invokable1' => BenchAsset\Foo::class,
],
'services' => [
'service1' => new \stdClass(),
],
'aliases' => [
'factoryAlias1' => 'factory1',
'recursiveFactoryAlias1' => 'factoryAlias1',
'recursiveFactoryAlias2' => 'recursiveFactoryAlias1',
],
'abstract_factories' => [
BenchAsset\AbstractFactoryFoo::class
],
]);
}

public function benchFetchFactory1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->get('factory1');
}

public function benchBuildFactory1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->build('factory1');
}

public function benchFetchInvokable1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->get('invokable1');
}

public function benchBuildInvokable1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->build('invokable1');
}

public function benchFetchService1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->get('service1');
}

public function benchFetchFactoryAlias1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->build('factoryAlias1');
}

public function benchBuildFactoryAlias1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->build('factoryAlias1');
}

public function benchFetchRecursiveFactoryAlias1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->build('recursiveFactoryAlias1');
}

public function benchBuildRecursiveFactoryAlias1()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->build('recursiveFactoryAlias1');
}

public function benchFetchRecursiveFactoryAlias2()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->build('recursiveFactoryAlias2');
}

public function benchBuildRecursiveFactoryAlias2()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->build('recursiveFactoryAlias2');
}

public function benchFetchAbstractFactoryFoo()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->get('foo');
}

public function benchBuildAbstractFactoryFoo()
{
// @todo @link https://github.com/phpbench/phpbench/issues/304
$sm = clone $this->sm;

$sm->build('foo');
}
}
39 changes: 0 additions & 39 deletions benchmarks/FetchServiceManager.php

This file was deleted.

Loading

0 comments on commit 7471eba

Please sign in to comment.