1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\Tests\ORM\Performance; |
||
6 | |||
7 | use Doctrine\DBAL\Logging\DebugStack; |
||
8 | use Doctrine\ORM\EntityManagerInterface; |
||
9 | use Doctrine\Tests\Models\Cache\City; |
||
10 | use Doctrine\Tests\Models\Cache\Country; |
||
11 | use Doctrine\Tests\Models\Cache\State; |
||
12 | use Doctrine\Tests\OrmFunctionalTestCase; |
||
13 | use const PHP_EOL; |
||
14 | use function count; |
||
15 | use function microtime; |
||
16 | use function number_format; |
||
17 | use function printf; |
||
18 | use function sprintf; |
||
19 | use function str_repeat; |
||
20 | |||
21 | /** |
||
22 | * @group performance |
||
23 | * @group DDC-2183 |
||
24 | * @group performance |
||
25 | */ |
||
26 | class SecondLevelCacheTest extends OrmFunctionalTestCase |
||
27 | { |
||
28 | protected function setUp() : void |
||
29 | { |
||
30 | parent::useModelSet('cache'); |
||
31 | parent::setUp(); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return EntityManagerInterface |
||
36 | */ |
||
37 | public function createEntityManager() |
||
38 | { |
||
39 | $logger = new DebugStack(); |
||
40 | $em = $this->getEntityManager(); |
||
41 | |||
42 | $em->getConnection()->getConfiguration()->setSQLLogger($logger); |
||
43 | $em->getConfiguration()->setSQLLogger($logger); |
||
44 | |||
45 | return $em; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @return int |
||
50 | */ |
||
51 | public function countQuery(EntityManagerInterface $em) |
||
52 | { |
||
53 | return count($em->getConfiguration()->getSQLLogger()->queries); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
54 | } |
||
55 | |||
56 | public function testFindEntityWithoutCache() : void |
||
57 | { |
||
58 | $em = $this->createEntityManager(); |
||
59 | |||
60 | $this->findEntity($em, __FUNCTION__); |
||
61 | |||
62 | self::assertEquals(6002, $this->countQuery($em)); |
||
63 | } |
||
64 | |||
65 | public function testFindEntityWithCache() : void |
||
66 | { |
||
67 | parent::enableSecondLevelCache(false); |
||
68 | |||
69 | $em = $this->createEntityManager(); |
||
70 | |||
71 | $this->findEntity($em, __FUNCTION__); |
||
72 | |||
73 | self::assertEquals(502, $this->countQuery($em)); |
||
74 | } |
||
75 | |||
76 | public function testFindAllEntityWithoutCache() : void |
||
77 | { |
||
78 | $em = $this->createEntityManager(); |
||
79 | |||
80 | $this->findAllEntity($em, __FUNCTION__); |
||
81 | |||
82 | self::assertEquals(153, $this->countQuery($em)); |
||
83 | } |
||
84 | |||
85 | public function testFindAllEntityWithCache() : void |
||
86 | { |
||
87 | parent::enableSecondLevelCache(false); |
||
88 | |||
89 | $em = $this->createEntityManager(); |
||
90 | |||
91 | $this->findAllEntity($em, __FUNCTION__); |
||
92 | |||
93 | self::assertEquals(53, $this->countQuery($em)); |
||
94 | } |
||
95 | |||
96 | public function testFindEntityOneToManyWithoutCache() : void |
||
97 | { |
||
98 | $em = $this->createEntityManager(); |
||
99 | |||
100 | $this->findEntityOneToMany($em, __FUNCTION__); |
||
101 | |||
102 | self::assertEquals(502, $this->countQuery($em)); |
||
103 | } |
||
104 | |||
105 | public function testFindEntityOneToManyWithCache() : void |
||
106 | { |
||
107 | parent::enableSecondLevelCache(false); |
||
108 | |||
109 | $em = $this->createEntityManager(); |
||
110 | |||
111 | $this->findEntityOneToMany($em, __FUNCTION__); |
||
112 | |||
113 | self::assertEquals(472, $this->countQuery($em)); |
||
114 | } |
||
115 | |||
116 | public function testQueryEntityWithoutCache() : void |
||
117 | { |
||
118 | $em = $this->createEntityManager(); |
||
119 | |||
120 | $this->queryEntity($em, __FUNCTION__); |
||
121 | |||
122 | self::assertEquals(602, $this->countQuery($em)); |
||
123 | } |
||
124 | |||
125 | public function testQueryEntityWithCache() : void |
||
126 | { |
||
127 | parent::enableSecondLevelCache(false); |
||
128 | |||
129 | $em = $this->createEntityManager(); |
||
130 | |||
131 | $this->queryEntity($em, __FUNCTION__); |
||
132 | |||
133 | self::assertEquals(503, $this->countQuery($em)); |
||
134 | } |
||
135 | |||
136 | private function queryEntity(EntityManagerInterface $em, $label) |
||
137 | { |
||
138 | $times = 100; |
||
139 | $size = 500; |
||
140 | $startPersist = microtime(true); |
||
141 | |||
142 | echo PHP_EOL . $label; |
||
143 | |||
144 | for ($i = 0; $i < $size; $i++) { |
||
145 | $em->persist(new Country(sprintf('Country %d', $i))); |
||
146 | } |
||
147 | |||
148 | $em->flush(); |
||
149 | $em->clear(); |
||
150 | |||
151 | printf("\n[%s] persist %s countries", number_format(microtime(true) - $startPersist, 6), $size); |
||
152 | |||
153 | $dql = 'SELECT c FROM Doctrine\Tests\Models\Cache\Country c WHERE c.name LIKE :name'; |
||
154 | $startFind = microtime(true); |
||
155 | |||
156 | for ($i = 0; $i < $times; $i++) { |
||
157 | $em->createQuery($dql) |
||
158 | ->setParameter('name', '%Country%') |
||
159 | ->setCacheable(true) |
||
160 | ->getResult(); |
||
161 | } |
||
162 | |||
163 | printf("\n[%s] select %s countries (%s times)", number_format(microtime(true) - $startFind, 6), $size, $times); |
||
164 | printf("\n%s\n", str_repeat('-', 50)); |
||
165 | } |
||
166 | |||
167 | public function findEntityOneToMany(EntityManagerInterface $em, $label) |
||
168 | { |
||
169 | $times = 50; |
||
170 | $size = 30; |
||
171 | $states = []; |
||
172 | $cities = []; |
||
173 | $startPersist = microtime(true); |
||
174 | $country = new Country('Country'); |
||
175 | |||
176 | echo PHP_EOL . $label; |
||
177 | |||
178 | $em->persist($country); |
||
179 | $em->flush(); |
||
180 | |||
181 | for ($i = 0; $i < $size / 2; $i++) { |
||
182 | $state = new State('State ' . $i, $country); |
||
183 | |||
184 | $em->persist($state); |
||
185 | |||
186 | $states[] = $state; |
||
187 | } |
||
188 | |||
189 | $em->flush(); |
||
190 | |||
191 | foreach ($states as $key => $state) { |
||
192 | for ($i = 0; $i < $size; $i++) { |
||
193 | $city = new City(sprintf('City %s - %d', $key, $i), $state); |
||
194 | |||
195 | $em->persist($city); |
||
196 | |||
197 | $state->addCity($city); |
||
198 | |||
199 | $cities[] = $city; |
||
200 | } |
||
201 | } |
||
202 | |||
203 | $em->flush(); |
||
204 | $em->clear(); |
||
205 | |||
206 | printf("\n[%s] persist %s states and %s cities", number_format(microtime(true) - $startPersist, 6), count($states), count($cities)); |
||
207 | |||
208 | $startFind = microtime(true); |
||
209 | |||
210 | for ($i = 0; $i < $times; $i++) { |
||
211 | foreach ($states as $state) { |
||
212 | $state = $em->find(State::class, $state->getId()); |
||
213 | |||
214 | foreach ($state->getCities() as $city) { |
||
215 | $city->getName(); |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | |||
220 | printf("\n[%s] find %s states and %s cities (%s times)", number_format(microtime(true) - $startFind, 6), count($states), count($cities), $times); |
||
221 | printf("\n%s\n", str_repeat('-', 50)); |
||
222 | } |
||
223 | |||
224 | private function findEntity(EntityManagerInterface $em, $label) |
||
225 | { |
||
226 | $times = 10; |
||
227 | $size = 500; |
||
228 | $countries = []; |
||
229 | $startPersist = microtime(true); |
||
230 | |||
231 | echo PHP_EOL . $label; |
||
232 | |||
233 | for ($i = 0; $i < $size; $i++) { |
||
234 | $country = new Country('Country ' . $i); |
||
235 | |||
236 | $em->persist($country); |
||
237 | |||
238 | $countries[] = $country; |
||
239 | } |
||
240 | |||
241 | $em->flush(); |
||
242 | $em->clear(); |
||
243 | |||
244 | printf("\n[%s] persist %s countries", number_format(microtime(true) - $startPersist, 6), $size); |
||
245 | |||
246 | $startFind = microtime(true); |
||
247 | |||
248 | for ($i = 0; $i <= $times; $i++) { |
||
249 | foreach ($countries as $country) { |
||
250 | $em->find(Country::class, $country->getId()); |
||
251 | $em->clear(); |
||
252 | } |
||
253 | } |
||
254 | |||
255 | printf("\n[%s] find %s countries (%s times)", number_format(microtime(true) - $startFind, 6), $size, $times); |
||
256 | printf("\n%s\n", str_repeat('-', 50)); |
||
257 | } |
||
258 | |||
259 | private function findAllEntity(EntityManagerInterface $em, $label) |
||
260 | { |
||
261 | $times = 100; |
||
262 | $size = 50; |
||
263 | $startPersist = microtime(true); |
||
264 | $rep = $em->getRepository(Country::class); |
||
265 | |||
266 | echo PHP_EOL . $label; |
||
267 | |||
268 | for ($i = 0; $i < $size; $i++) { |
||
269 | $em->persist(new Country('Country ' . $i)); |
||
270 | } |
||
271 | |||
272 | $em->flush(); |
||
273 | $em->clear(); |
||
274 | |||
275 | printf("\n[%s] persist %s countries", number_format(microtime(true) - $startPersist, 6), $size); |
||
276 | |||
277 | $startFind = microtime(true); |
||
278 | |||
279 | for ($i = 0; $i <= $times; $i++) { |
||
280 | $list = $rep->findAll(); |
||
281 | $em->clear(); |
||
282 | |||
283 | self::assertCount($size, $list); |
||
284 | } |
||
285 | |||
286 | printf("\n[%s] find %s countries (%s times)", number_format(microtime(true) - $startFind, 6), $size, $times); |
||
287 | printf("\n%s\n", str_repeat('-', 50)); |
||
288 | } |
||
289 | } |
||
290 |