Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / tests / Doctrine / Tests / Common / Annotations / PerformanceTest.php
1 <?php
2
3 namespace Doctrine\Tests\Common\Annotations;
4
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;
12
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';
17
18 class PerformanceTest extends \PHPUnit_Framework_TestCase
19 {
20     /**
21      * @group performance
22      */
23     public function testCachedReadPerformanceWithInMemory()
24     {
25         $reader = new CachedReader(new AnnotationReader(), new ArrayCache());
26         $method = $this->getMethod();
27
28         $time = microtime(true);
29         for ($i=0,$c=500; $i<$c; $i++) {
30             $reader->getMethodAnnotations($method);
31         }
32         $time = microtime(true) - $time;
33
34         $this->printResults('cached reader (in-memory)', $time, $c);
35     }
36
37     /**
38      * @group performance
39      */
40     public function testCachedReadPerformanceWithFileCache()
41     {
42         $method = $this->getMethod();
43
44         // prime cache
45         $reader = new FileCacheReader(new AnnotationReader(), sys_get_temp_dir());
46         $reader->getMethodAnnotations($method);
47
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);
52             clearstatcache();
53         }
54         $time = microtime(true) - $time;
55
56         $this->printResults('cached reader (file)', $time, $c);
57     }
58
59     /**
60      * @group performance
61      */
62     public function testReadPerformance()
63     {
64         $method = $this->getMethod();
65
66         $time = microtime(true);
67         for ($i=0,$c=150; $i<$c; $i++) {
68             $reader = new AnnotationReader();
69             $reader->getMethodAnnotations($method);
70         }
71         $time = microtime(true) - $time;
72
73         $this->printResults('reader', $time, $c);
74     }
75
76     /**
77      * @group performance
78      */
79     public function testDocParsePerformance()
80     {
81         $imports = array(
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',
87         );
88         $ignored = array(
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',
93         );
94
95         $method = $this->getMethod();
96         $methodComment = $method->getDocComment();
97         $classComment = $method->getDeclaringClass()->getDocComment();
98
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);
105
106             $parser->parse($methodComment);
107             $parser->parse($classComment);
108         }
109         $time = microtime(true) - $time;
110
111         $this->printResults('doc-parser', $time, $c);
112     }
113
114     /**
115      * @group performance
116      */
117     public function testDocLexerPerformance()
118     {
119         $method = $this->getMethod();
120         $methodComment = $method->getDocComment();
121         $classComment = $method->getDeclaringClass()->getDocComment();
122
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);
128         }
129         $time = microtime(true) - $time;
130
131         $this->printResults('doc-lexer', $time, $c);
132     }
133
134     /**
135      * @group performance
136      */
137     public function testPhpParserPerformanceWithShortCut()
138     {
139         $class = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\NamespacedSingleClassLOC1000');
140
141         $time = microtime(true);
142         for ($i=0,$c=500; $i<$c; $i++) {
143             $parser = new PhpParser();
144             $parser->parseClass($class);
145         }
146         $time = microtime(true) - $time;
147
148         $this->printResults('doc-parser-with-short-cut', $time, $c);
149     }
150
151     /**
152      * @group performance
153      */
154     public function testPhpParserPerformanceWithoutShortCut()
155     {
156         $class = new \ReflectionClass('SingleClassLOC1000');
157
158         $time = microtime(true);
159         for ($i=0,$c=500; $i<$c; $i++) {
160             $parser = new PhpParser();
161             $parser->parseClass($class);
162         }
163         $time = microtime(true) - $time;
164
165         $this->printResults('doc-parser-without-short-cut', $time, $c);
166     }
167
168     private function getMethod()
169     {
170         return new \ReflectionMethod('Doctrine\Tests\Common\Annotations\Fixtures\Controller', 'helloAction');
171     }
172
173     private function printResults($test, $time, $iterations)
174     {
175         if (0 == $iterations) {
176             throw new \InvalidArgumentException('$iterations cannot be zero.');
177         }
178
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);
183
184         $max = max(strlen($title), strlen($iterationTime)) - 1;
185
186         echo "\n".str_repeat('-', $max)."\n";
187         echo $title;
188         echo str_repeat('=', $max)."\n";
189         echo $iterationsText;
190         echo $totalTime;
191         echo $iterationTime;
192         echo str_repeat('-', $max)."\n";
193     }
194 }