diff --git a/README.md b/README.md index 9e48dfc1..b2ed9afc 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/benchmarks/FetchCachedServicesBench.php b/benchmarks/FetchCachedServicesBench.php new file mode 100644 index 00000000..c10f2929 --- /dev/null +++ b/benchmarks/FetchCachedServicesBench.php @@ -0,0 +1,108 @@ +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'); + } +} diff --git a/benchmarks/FetchNewServiceManagerBench.php b/benchmarks/FetchNewServiceManagerBench.php new file mode 100644 index 00000000..0ffa63db --- /dev/null +++ b/benchmarks/FetchNewServiceManagerBench.php @@ -0,0 +1,51 @@ + [], + '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); + } +} diff --git a/benchmarks/FetchNewServicesBench.php b/benchmarks/FetchNewServicesBench.php new file mode 100644 index 00000000..caf1b3d1 --- /dev/null +++ b/benchmarks/FetchNewServicesBench.php @@ -0,0 +1,148 @@ +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'); + } +} diff --git a/benchmarks/FetchServiceManager.php b/benchmarks/FetchServiceManager.php deleted file mode 100644 index bf53cb3a..00000000 --- a/benchmarks/FetchServiceManager.php +++ /dev/null @@ -1,39 +0,0 @@ -config = $this->getConfig(); - } - - /** - * @iterations 500 - */ - public function fetchServiceManagerCreation() - { - $retult = new ServiceManager($this->config); - } -} diff --git a/benchmarks/FetchServices.php b/benchmarks/FetchServices.php deleted file mode 100644 index 9f86289e..00000000 --- a/benchmarks/FetchServices.php +++ /dev/null @@ -1,84 +0,0 @@ -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'); - } -} diff --git a/composer.json b/composer.json index 1261f5e9..65e7a7d8 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "phpunit/phpunit": "~4.6", "ocramius/proxy-manager": "~1.0", "squizlabs/php_codesniffer": "^2.0@dev", - "athletic/athletic": "dev-master" + "phpbench/phpbench": "^0.10.0" }, "suggest": { "ocramius/proxy-manager": "ProxyManager 1.* to handle lazy initialization of services", diff --git a/phpbench.json b/phpbench.json new file mode 100644 index 00000000..f782945a --- /dev/null +++ b/phpbench.json @@ -0,0 +1,5 @@ +{ + "bootstrap": "vendor/autoload.php", + "path": "benchmarks", + "retry_threshold": 5 +} diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 0fef5d82..f6d4d97d 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -563,31 +563,19 @@ private function resolveInitializers(array $initializers) } } - /** - * Recursively resolve an alias name to a service name - * - * @param string $alias - * @return string - */ - private function resolveAlias($alias) - { - $name = $alias; - - do { - $canBeResolved = isset($this->aliases[$name]); - $name = $canBeResolved ? $this->aliases[$name] : $name; - } while ($canBeResolved); - - return $name; - } - /** * Resolve all aliases to their canonical service names. */ private function resolveAliases(array $aliases) { foreach ($aliases as $alias => $service) { - $this->resolvedAliases[$alias] = $this->resolveAlias($alias); + $name = $alias; + + while (isset($this->aliases[$name])) { + $name = $this->aliases[$name]; + } + + $this->resolvedAliases[$alias] = $name; } }