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

Commit

Permalink
More and better benchmarking of new abstract factories
Browse files Browse the repository at this point in the history
This patch provides more benchmarking for the new abstract factories, pulling
them each into separate benchmark classes to ensure they operate in isolation to
other abstract factories, and to allow seeing baseline timings.

Additionally, each has two benchmarking suites: one demonstrating usage as an
abstract factory, another demonstrating usage as a mapped factory.

The basic results are that the `ConfigAbstractFactory` operates at roughly the
same speed as a normal abstract factory that contains logic for creating an
instance. The `ReflectionBasedAbstractFactory` is about 100% slower when at
least one dependency is present (and ever-so-slightly faster when no
dependencies are present).
  • Loading branch information
weierophinney committed Sep 15, 2016
1 parent f2036a8 commit 15712f9
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 49 deletions.
12 changes: 0 additions & 12 deletions benchmarks/BenchAsset/Baz.php

This file was deleted.

12 changes: 12 additions & 0 deletions benchmarks/BenchAsset/Dependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendBench\ServiceManager\BenchAsset;

class Dependency
{
}
21 changes: 21 additions & 0 deletions benchmarks/BenchAsset/ServiceDependingOnConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendBench\ServiceManager\BenchAsset;

class ServiceDependingOnConfig
{
/**
* @var array
*/
private $config;

public function __construct(array $config)
{
$this->config = $config;
}
}
24 changes: 24 additions & 0 deletions benchmarks/BenchAsset/ServiceWithDependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendBench\ServiceManager\BenchAsset;

class ServiceWithDependency
{
/**
* @var Dependency
*/
private $dependency;

/**
* @param Dependency $dependency
*/
public function __construct(Dependency $dependency)
{
$this->dependency = $dependency;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendBench\ServiceManager;

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

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

public function __construct()
{
$this->sm = new ServiceManager([
'services' => [
'config' => [
ConfigAbstractFactory::class => [
BenchAsset\Dependency::class => [],
BenchAsset\ServiceWithDependency::class => [
BenchAsset\Dependency::class,
],
BenchAsset\ServiceDependingOnConfig::class => [
'config',
],
],
],
],
'factories' => [
BenchAsset\Dependency::class => ConfigAbstractFactory::class,
BenchAsset\ServiceWithDependency::class => ConfigAbstractFactory::class,
BenchAsset\ServiceDependingOnConfig::class => ConfigAbstractFactory::class,
],
]);
}

public function benchFetchServiceWithNoDependencies()
{
$sm = clone $this->sm;

$sm->get(BenchAsset\Dependency::class);
}

public function benchBuildServiceWithNoDependencies()
{
$sm = clone $this->sm;

$sm->build(BenchAsset\Dependency::class);
}

public function benchFetchServiceDependingOnConfig()
{
$sm = clone $this->sm;

$sm->get(BenchAsset\ServiceDependingOnConfig::class);
}

public function benchBuildServiceDependingOnConfig()
{
$sm = clone $this->sm;

$sm->build(BenchAsset\ServiceDependingOnConfig::class);
}

public function benchFetchServiceWithDependency()
{
$sm = clone $this->sm;

$sm->get(BenchAsset\ServiceWithDependency::class);
}

public function benchBuildServiceWithDependency()
{
$sm = clone $this->sm;

$sm->build(BenchAsset\ServiceWithDependency::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendBench\ServiceManager;

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

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

public function __construct()
{
$this->sm = new ServiceManager([
'services' => [
'config' => [],
],
'factories' => [
BenchAsset\Dependency::class => ReflectionBasedAbstractFactory::class,
BenchAsset\ServiceWithDependency::class => ReflectionBasedAbstractFactory::class,
BenchAsset\ServiceDependingOnConfig::class => ReflectionBasedAbstractFactory::class,
],
]);
}

public function benchFetchServiceWithNoDependencies()
{
$sm = clone $this->sm;

$sm->get(BenchAsset\Dependency::class);
}

public function benchBuildServiceWithNoDependencies()
{
$sm = clone $this->sm;

$sm->build(BenchAsset\Dependency::class);
}

public function benchFetchServiceDependingOnConfig()
{
$sm = clone $this->sm;

$sm->get(BenchAsset\ServiceDependingOnConfig::class);
}

public function benchBuildServiceDependingOnConfig()
{
$sm = clone $this->sm;

$sm->build(BenchAsset\ServiceDependingOnConfig::class);
}

public function benchFetchServiceWithDependency()
{
$sm = clone $this->sm;

$sm->get(BenchAsset\ServiceWithDependency::class);
}

public function benchBuildServiceWithDependency()
{
$sm = clone $this->sm;

$sm->build(BenchAsset\ServiceWithDependency::class);
}
}
91 changes: 91 additions & 0 deletions benchmarks/FetchNewServiceViaConfigAbstractFactoryBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendBench\ServiceManager;

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

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

public function __construct()
{
$this->sm = new ServiceManager([
'services' => [
'config' => [
ConfigAbstractFactory::class => [
BenchAsset\Dependency::class => [],
BenchAsset\ServiceWithDependency::class => [
BenchAsset\Dependency::class,
],
BenchAsset\ServiceDependingOnConfig::class => [
'config',
],
],
],
],
'abstract_factories' => [
ConfigAbstractFactory::class,
],
]);
}

public function benchFetchServiceWithNoDependencies()
{
$sm = clone $this->sm;

$sm->get(BenchAsset\Dependency::class);
}

public function benchBuildServiceWithNoDependencies()
{
$sm = clone $this->sm;

$sm->build(BenchAsset\Dependency::class);
}

public function benchFetchServiceDependingOnConfig()
{
$sm = clone $this->sm;

$sm->get(BenchAsset\ServiceDependingOnConfig::class);
}

public function benchBuildServiceDependingOnConfig()
{
$sm = clone $this->sm;

$sm->build(BenchAsset\ServiceDependingOnConfig::class);
}

public function benchFetchServiceWithDependency()
{
$sm = clone $this->sm;

$sm->get(BenchAsset\ServiceWithDependency::class);
}

public function benchBuildServiceWithDependency()
{
$sm = clone $this->sm;

$sm->build(BenchAsset\ServiceWithDependency::class);
}
}
Loading

0 comments on commit 15712f9

Please sign in to comment.