Skip to content

Commit

Permalink
Adding basic service manager test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Mar 28, 2013
1 parent abd8be6 commit 232c278
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions tests/ZendTest/ServiceManager/ServiceManagerPerformanceTest.php
@@ -0,0 +1,121 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_ServiceManager
*/

namespace ZendTest\ServiceManager;

use Zend\ServiceManager\ServiceManager;

use ZendTest\ServiceManager\TestAsset\FooCounterAbstractFactory;

class ServiceManagerPerformanceTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Zend\ServiceManager\ServiceManager
*/
protected $serviceManager;

/**
* @var int
*/
protected $iterations = 100000;

/**
* @var float
*/
protected $lastSnapshotTime;

/**
* {@inheritDoc}
*/
public function setUp()
{
$this->serviceManager = new ServiceManager();
}

/**
* @bufferedOutput
*/
public function testGetPerformance()
{
$this->serviceManager->setService('foo', new \stdClass());
$this->startProfiling();

for ($i = 0; $i < $this->iterations; $i += 1) {
$this->serviceManager->get('foo');
}

var_dump(__METHOD__, $this->stopProfiling());
}

/**
* @bufferedOutput
*/
public function testHasPerformance()
{
$this->serviceManager->setService('foo', new \stdClass());
$this->startProfiling();

for ($i = 0; $i < $this->iterations; $i += 1) {
$this->serviceManager->has('foo');
}

var_dump(__METHOD__, $this->stopProfiling());
}

/**
* @bufferedOutput
*/
public function testHasPerformanceWithNoService()
{
$this->startProfiling();

for ($i = 0; $i < $this->iterations; $i += 1) {
$this->serviceManager->has('foo');
}

var_dump(__METHOD__, $this->stopProfiling());
}

/**
* @bufferedOutput
*/
public function testCreatePerformance()
{
$this->serviceManager->setInvokableClass('foo', 'stdClass');
$this->startProfiling();

for ($i = 0; $i < $this->iterations; $i += 1) {
$this->serviceManager->create('foo');
}

var_dump(__METHOD__, $this->stopProfiling());
}

/**
* Start profiling execution
*/
private function startProfiling()
{
$this->lastSnapshotTime = microtime(true);
}

/**
* Get current profiler snapshot and stop profiling
*
* @return array
*/
private function stopProfiling()
{
return array(
'time' => microtime(true) - $this->lastSnapshotTime,
'iterations' => $this->iterations,
);
}
}

0 comments on commit 232c278

Please sign in to comment.