* @license MIT * * @group Performance * @coversNothing */ class LazyLoadingGhostPerformanceTest extends BaseLazyLoadingPerformanceTest { /** * @outputBuffering * @dataProvider getTestedClasses * * @param string $className * @param array $methods * @param array $properties * @param \ReflectionProperty[] $reflectionProperties * * @return void */ public function testProxyInstantiationPerformance( $className, array $methods, array $properties, array $reflectionProperties ) { $proxyName = $this->generateProxy($className); $iterations = 20000; $instances = array(); /* @var $proxies \ProxyManager\Proxy\GhostObjectInterface[] */ $proxies = array(); $realInstance = new $className(); $initializer = function ( GhostObjectInterface $proxy, $method, $params, & $initializer ) use ( $reflectionProperties, $realInstance ) { $initializer = null; foreach ($reflectionProperties as $reflectionProperty) { $reflectionProperty->setValue($proxy, $reflectionProperty->getValue($realInstance)); } return true; }; $this->startCapturing(); for ($i = 0; $i < $iterations; $i += 1) { $instances[] = new $className(); } $baseProfile = $this->endCapturing( 'Instantiation for ' . $iterations . ' objects of type ' . $className . ': %fms / %fKb' ); $this->startCapturing(); for ($i = 0; $i < $iterations; $i += 1) { $proxies[] = new $proxyName($initializer); } $proxyProfile = $this->endCapturing( 'Instantiation for ' . $iterations . ' proxies of type ' . $className . ': %fms / %fKb' ); $this->compareProfile($baseProfile, $proxyProfile); $this->startCapturing(); foreach ($proxies as $proxy) { $proxy->initializeProxy(); } $this->endCapturing('Initialization of ' . $iterations . ' proxies of type ' . $className . ': %fms / %fKb'); foreach ($methods as $methodName => $parameters) { $this->profileMethodAccess($className, $instances, $proxies, $methodName, $parameters); } foreach ($properties as $property) { $this->profilePropertyWrites($className, $instances, $proxies, $property); $this->profilePropertyReads($className, $instances, $proxies, $property); $this->profilePropertyIsset($className, $instances, $proxies, $property); $this->profilePropertyUnset($className, $instances, $proxies, $property); } } /** * @return array */ public function getTestedClasses() { $testedClasses = array( array('stdClass', array(), array()), array('ProxyManagerTestAsset\\BaseClass', array('publicMethod' => array()), array('publicProperty')), ); foreach ($testedClasses as $key => $testedClass) { $reflectionProperties = array(); $reflectionClass = new ReflectionClass($testedClass[0]); foreach ($reflectionClass->getProperties() as $property) { $property->setAccessible(true); $reflectionProperties[$property->getName()] = $property; } $testedClasses[$key][] = $reflectionProperties; } return $testedClasses; } /** * {@inheritDoc} */ protected function generateProxy($parentClassName) { $generatedClassName = __NAMESPACE__ . '\\' . UniqueIdentifierGenerator::getIdentifier('Foo'); $generator = new LazyLoadingGhostGenerator(); $generatedClass = new ClassGenerator($generatedClassName); $strategy = new EvaluatingGeneratorStrategy(); $generator->generate(new ReflectionClass($parentClassName), $generatedClass); $strategy->generate($generatedClass); return $generatedClassName; } }