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 #38 from ezimuel/feature/bench
Browse files Browse the repository at this point in the history
Add the benchmarks
  • Loading branch information
ezimuel committed Oct 7, 2015
2 parents c6a6575 + 77c3e8d commit 675931f
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ retrieving other objects.

- File issues at https://github.com/zendframework/zend-servicemanager/issues
- Documentation is at http://framework.zend.com/manual/current/en/index.html#zend-servicemanager

## Benchmarks

We provide scripts for benchmarking zend-servicemanager using the
[Athletic](https://github.com/polyfractal/athletic) 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
```
21 changes: 21 additions & 0 deletions benchmarks/BenchAsset/AbstractFactoryFoo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace ZendBench\ServiceManager\BenchAsset;

use Zend\ServiceManager\Factory\AbstractFactoryInterface;
use Interop\Container\ContainerInterface;

class AbstractFactoryFoo implements AbstractFactoryInterface
{
public function canCreateServiceWithName(ContainerInterface $container, $requestedName)
{
return ($requestedName === 'foo');
}

public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
if ($requestedName === 'foo') {
return new Foo($options);
}
return false;
}
}
13 changes: 13 additions & 0 deletions benchmarks/BenchAsset/FactoryFoo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace ZendBench\ServiceManager\BenchAsset;

use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;

class FactoryFoo implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new Foo($options);
}
}
12 changes: 12 additions & 0 deletions benchmarks/BenchAsset/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace ZendBench\ServiceManager\BenchAsset;

class Foo
{
protected $options;

public function __construct($options = null)
{
$this->options = $options;
}
}
84 changes: 84 additions & 0 deletions benchmarks/FetchServices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace ZendBench\ServiceManager;

use Athletic\AthleticEvent;
use Zend\ServiceManager\ServiceManager;

class FetchServices extends AthleticEvent
{
const NUM_SERVICES = 1000;

/**
* @var ServiceManager
*/
protected $sm;

protected function getConfig()
{
$config = [];
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"] = $this;
$config['aliases']["alias_$i"] = "service_$i";
}
$config['abstract_factories'] = [ BenchAsset\AbstractFactoryFoo::class ];
return $config;
}

public function classSetUp()
{
$this->sm = new ServiceManager($this->getConfig());
}

/**
* Fetch the factory services
*
* @iterations 5000
*/
public function fetchFactoryService()
{
$result = $this->sm->get('factory_' . rand(0, self::NUM_SERVICES));
}

/**
* Fetch the invokable services
*
* @iterations 5000
*/
public function fetchInvokableService()
{
$result = $this->sm->get('invokable_' . rand(0, self::NUM_SERVICES));
}

/**
* Fetch the services
*
* @iterations 5000
*/
public function fetchService()
{
$result = $this->sm->get('service_' . rand(0, self::NUM_SERVICES));
}

/**
* Fetch the alias services
*
* @iterations 5000
*/
public function fetchAliasService()
{
$result = $this->sm->get('alias_' . rand(0, self::NUM_SERVICES));
}

/**
* Fetch the abstract factory services
*
* @iterations 5000
*/
public function fetchAbstractFactoryService()
{
$result = $this->sm->get('foo');
}
}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"require-dev": {
"phpunit/phpunit": "~4.6",
"ocramius/proxy-manager": "~1.0",
"squizlabs/php_codesniffer": "^2.0@dev"
"squizlabs/php_codesniffer": "^2.0@dev",
"athletic/athletic": "dev-master"
},
"suggest": {
"ocramius/proxy-manager": "ProxyManager 1.* to handle lazy initialization of services"
Expand All @@ -35,7 +36,8 @@
},
"autoload-dev": {
"psr-4": {
"ZendTest\\ServiceManager\\": "test/"
"ZendTest\\ServiceManager\\": "test/",
"ZendBench\\ServiceManager\\": "benchmarks/"
}
}
}

0 comments on commit 675931f

Please sign in to comment.