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

Commit 675931f

Browse files
committed
Merge pull request #38 from ezimuel/feature/bench
Add the benchmarks
2 parents c6a6575 + 77c3e8d commit 675931f

File tree

6 files changed

+146
-2
lines changed

6 files changed

+146
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@ retrieving other objects.
1414

1515
- File issues at https://github.com/zendframework/zend-servicemanager/issues
1616
- Documentation is at http://framework.zend.com/manual/current/en/index.html#zend-servicemanager
17+
18+
## Benchmarks
19+
20+
We provide scripts for benchmarking zend-servicemanager using the
21+
[Athletic](https://github.com/polyfractal/athletic) framework; these can be
22+
found in the `benchmarks/` directory.
23+
24+
To execute the benchmarks you can run the following command:
25+
26+
```bash
27+
$ vendor/bin/athletic -p benchmarks
28+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace ZendBench\ServiceManager\BenchAsset;
3+
4+
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
5+
use Interop\Container\ContainerInterface;
6+
7+
class AbstractFactoryFoo implements AbstractFactoryInterface
8+
{
9+
public function canCreateServiceWithName(ContainerInterface $container, $requestedName)
10+
{
11+
return ($requestedName === 'foo');
12+
}
13+
14+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
15+
{
16+
if ($requestedName === 'foo') {
17+
return new Foo($options);
18+
}
19+
return false;
20+
}
21+
}

benchmarks/BenchAsset/FactoryFoo.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace ZendBench\ServiceManager\BenchAsset;
3+
4+
use Zend\ServiceManager\Factory\FactoryInterface;
5+
use Interop\Container\ContainerInterface;
6+
7+
class FactoryFoo implements FactoryInterface
8+
{
9+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
10+
{
11+
return new Foo($options);
12+
}
13+
}

benchmarks/BenchAsset/Foo.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace ZendBench\ServiceManager\BenchAsset;
3+
4+
class Foo
5+
{
6+
protected $options;
7+
8+
public function __construct($options = null)
9+
{
10+
$this->options = $options;
11+
}
12+
}

benchmarks/FetchServices.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace ZendBench\ServiceManager;
4+
5+
use Athletic\AthleticEvent;
6+
use Zend\ServiceManager\ServiceManager;
7+
8+
class FetchServices extends AthleticEvent
9+
{
10+
const NUM_SERVICES = 1000;
11+
12+
/**
13+
* @var ServiceManager
14+
*/
15+
protected $sm;
16+
17+
protected function getConfig()
18+
{
19+
$config = [];
20+
for ($i = 0; $i <= self::NUM_SERVICES; $i++) {
21+
$config['factories']["factory_$i"] = BenchAsset\FactoryFoo::class;
22+
$config['invokables']["invokable_$i"] = BenchAsset\Foo::class;
23+
$config['services']["service_$i"] = $this;
24+
$config['aliases']["alias_$i"] = "service_$i";
25+
}
26+
$config['abstract_factories'] = [ BenchAsset\AbstractFactoryFoo::class ];
27+
return $config;
28+
}
29+
30+
public function classSetUp()
31+
{
32+
$this->sm = new ServiceManager($this->getConfig());
33+
}
34+
35+
/**
36+
* Fetch the factory services
37+
*
38+
* @iterations 5000
39+
*/
40+
public function fetchFactoryService()
41+
{
42+
$result = $this->sm->get('factory_' . rand(0, self::NUM_SERVICES));
43+
}
44+
45+
/**
46+
* Fetch the invokable services
47+
*
48+
* @iterations 5000
49+
*/
50+
public function fetchInvokableService()
51+
{
52+
$result = $this->sm->get('invokable_' . rand(0, self::NUM_SERVICES));
53+
}
54+
55+
/**
56+
* Fetch the services
57+
*
58+
* @iterations 5000
59+
*/
60+
public function fetchService()
61+
{
62+
$result = $this->sm->get('service_' . rand(0, self::NUM_SERVICES));
63+
}
64+
65+
/**
66+
* Fetch the alias services
67+
*
68+
* @iterations 5000
69+
*/
70+
public function fetchAliasService()
71+
{
72+
$result = $this->sm->get('alias_' . rand(0, self::NUM_SERVICES));
73+
}
74+
75+
/**
76+
* Fetch the abstract factory services
77+
*
78+
* @iterations 5000
79+
*/
80+
public function fetchAbstractFactoryService()
81+
{
82+
$result = $this->sm->get('foo');
83+
}
84+
}

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"require-dev": {
2121
"phpunit/phpunit": "~4.6",
2222
"ocramius/proxy-manager": "~1.0",
23-
"squizlabs/php_codesniffer": "^2.0@dev"
23+
"squizlabs/php_codesniffer": "^2.0@dev",
24+
"athletic/athletic": "dev-master"
2425
},
2526
"suggest": {
2627
"ocramius/proxy-manager": "ProxyManager 1.* to handle lazy initialization of services"
@@ -35,7 +36,8 @@
3536
},
3637
"autoload-dev": {
3738
"psr-4": {
38-
"ZendTest\\ServiceManager\\": "test/"
39+
"ZendTest\\ServiceManager\\": "test/",
40+
"ZendBench\\ServiceManager\\": "benchmarks/"
3941
}
4042
}
4143
}

0 commit comments

Comments
 (0)