3 namespace Doctrine\Tests\Common\Annotations;
5 use Doctrine\Common\Annotations\FileCacheReader;
6 use Doctrine\Common\Cache\ArrayCache;
7 use Doctrine\Common\Annotations\CachedReader;
8 use Doctrine\Common\Annotations\DocLexer;
9 use Doctrine\Common\Annotations\DocParser;
10 use Doctrine\Common\Annotations\PhpParser;
11 use Doctrine\Common\Annotations\AnnotationReader;
13 require_once __DIR__ . '/Fixtures/Annotation/Route.php';
14 require_once __DIR__ . '/Fixtures/Annotation/Template.php';
15 require_once __DIR__ . '/Fixtures/Annotation/Secure.php';
16 require_once __DIR__ . '/Fixtures/SingleClassLOC1000.php';
18 class PerformanceTest extends \PHPUnit_Framework_TestCase
23 public function testCachedReadPerformanceWithInMemory()
25 $reader = new CachedReader(new AnnotationReader(), new ArrayCache());
26 $method = $this->getMethod();
28 $time = microtime(true);
29 for ($i=0,$c=500; $i<$c; $i++) {
30 $reader->getMethodAnnotations($method);
32 $time = microtime(true) - $time;
34 $this->printResults('cached reader (in-memory)', $time, $c);
40 public function testCachedReadPerformanceWithFileCache()
42 $method = $this->getMethod();
45 $reader = new FileCacheReader(new AnnotationReader(), sys_get_temp_dir());
46 $reader->getMethodAnnotations($method);
48 $time = microtime(true);
49 for ($i=0,$c=500; $i<$c; $i++) {
50 $reader = new FileCacheReader(new AnnotationReader(), sys_get_temp_dir());
51 $reader->getMethodAnnotations($method);
54 $time = microtime(true) - $time;
56 $this->printResults('cached reader (file)', $time, $c);
62 public function testReadPerformance()
64 $method = $this->getMethod();
66 $time = microtime(true);
67 for ($i=0,$c=150; $i<$c; $i++) {
68 $reader = new AnnotationReader();
69 $reader->getMethodAnnotations($method);
71 $time = microtime(true) - $time;
73 $this->printResults('reader', $time, $c);
79 public function testDocParsePerformance()
82 'ignorephpdoc' => 'Annotations\Annotation\IgnorePhpDoc',
83 'ignoreannotation' => 'Annotations\Annotation\IgnoreAnnotation',
84 'route' => 'Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Route',
85 'template' => 'Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Template',
86 '__NAMESPACE__' => 'Doctrine\Tests\Common\Annotations\Fixtures',
89 'access', 'author', 'copyright', 'deprecated', 'example', 'ignore',
90 'internal', 'link', 'see', 'since', 'tutorial', 'version', 'package',
91 'subpackage', 'name', 'global', 'param', 'return', 'staticvar',
92 'static', 'var', 'throws', 'inheritdoc',
95 $method = $this->getMethod();
96 $methodComment = $method->getDocComment();
97 $classComment = $method->getDeclaringClass()->getDocComment();
99 $time = microtime(true);
100 for ($i=0,$c=200; $i<$c; $i++) {
101 $parser = new DocParser();
102 $parser->setImports($imports);
103 $parser->setIgnoredAnnotationNames($ignored);
104 $parser->setIgnoreNotImportedAnnotations(true);
106 $parser->parse($methodComment);
107 $parser->parse($classComment);
109 $time = microtime(true) - $time;
111 $this->printResults('doc-parser', $time, $c);
117 public function testDocLexerPerformance()
119 $method = $this->getMethod();
120 $methodComment = $method->getDocComment();
121 $classComment = $method->getDeclaringClass()->getDocComment();
123 $time = microtime(true);
124 for ($i=0,$c=500; $i<$c; $i++) {
125 $lexer = new DocLexer();
126 $lexer->setInput($methodComment);
127 $lexer->setInput($classComment);
129 $time = microtime(true) - $time;
131 $this->printResults('doc-lexer', $time, $c);
137 public function testPhpParserPerformanceWithShortCut()
139 $class = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\NamespacedSingleClassLOC1000');
141 $time = microtime(true);
142 for ($i=0,$c=500; $i<$c; $i++) {
143 $parser = new PhpParser();
144 $parser->parseClass($class);
146 $time = microtime(true) - $time;
148 $this->printResults('doc-parser-with-short-cut', $time, $c);
154 public function testPhpParserPerformanceWithoutShortCut()
156 $class = new \ReflectionClass('SingleClassLOC1000');
158 $time = microtime(true);
159 for ($i=0,$c=500; $i<$c; $i++) {
160 $parser = new PhpParser();
161 $parser->parseClass($class);
163 $time = microtime(true) - $time;
165 $this->printResults('doc-parser-without-short-cut', $time, $c);
168 private function getMethod()
170 return new \ReflectionMethod('Doctrine\Tests\Common\Annotations\Fixtures\Controller', 'helloAction');
173 private function printResults($test, $time, $iterations)
175 if (0 == $iterations) {
176 throw new \InvalidArgumentException('$iterations cannot be zero.');
179 $title = $test." results:\n";
180 $iterationsText = sprintf("Iterations: %d\n", $iterations);
181 $totalTime = sprintf("Total Time: %.3f s\n", $time);
182 $iterationTime = sprintf("Time per iteration: %.3f ms\n", $time/$iterations * 1000);
184 $max = max(strlen($title), strlen($iterationTime)) - 1;
186 echo "\n".str_repeat('-', $max)."\n";
188 echo str_repeat('=', $max)."\n";
189 echo $iterationsText;
192 echo str_repeat('-', $max)."\n";