/tests/Doctrine/Tests/ORM/Performance/SecondLevelCacheTest.php

https://github.com/adrienbrault/doctrine2 · PHP · 283 lines · 191 code · 80 blank · 12 comment · 9 complexity · e80f75c9086ff5b3bc59f850d0592be6 MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Tests\ORM\Performance;
  4. use Doctrine\DBAL\Logging\DebugStack;
  5. use Doctrine\Tests\OrmFunctionalTestCase;
  6. use Doctrine\Tests\Models\Cache\Country;
  7. use Doctrine\Tests\Models\Cache\State;
  8. use Doctrine\Tests\Models\Cache\City;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. /**
  11. * @group performance
  12. * @group DDC-2183
  13. * @group performance
  14. */
  15. class SecondLevelCacheTest extends OrmFunctionalTestCase
  16. {
  17. protected function setUp()
  18. {
  19. parent::useModelSet('cache');
  20. parent::setUp();
  21. }
  22. /**
  23. * @return \Doctrine\ORM\EntityManagerInterface
  24. */
  25. public function createEntityManager()
  26. {
  27. $logger = new DebugStack();
  28. $em = $this->getEntityManager();
  29. $em->getConnection()->getConfiguration()->setSQLLogger($logger);
  30. $em->getConfiguration()->setSQLLogger($logger);
  31. return $em;
  32. }
  33. /**
  34. * @param \Doctrine\ORM\EntityManagerInterface $em
  35. * @return integer
  36. */
  37. public function countQuery(EntityManagerInterface $em)
  38. {
  39. return count($em->getConfiguration()->getSQLLogger()->queries);
  40. }
  41. public function testFindEntityWithoutCache()
  42. {
  43. $em = $this->createEntityManager();
  44. $this->findEntity($em, __FUNCTION__);
  45. self::assertEquals(6002, $this->countQuery($em));
  46. }
  47. public function testFindEntityWithCache()
  48. {
  49. parent::enableSecondLevelCache(false);
  50. $em = $this->createEntityManager();
  51. $this->findEntity($em, __FUNCTION__);
  52. self::assertEquals(502, $this->countQuery($em));
  53. }
  54. public function testFindAllEntityWithoutCache()
  55. {
  56. $em = $this->createEntityManager();
  57. $this->findAllEntity($em, __FUNCTION__);
  58. self::assertEquals(153, $this->countQuery($em));
  59. }
  60. public function testFindAllEntityWithCache()
  61. {
  62. parent::enableSecondLevelCache(false);
  63. $em = $this->createEntityManager();
  64. $this->findAllEntity($em, __FUNCTION__);
  65. self::assertEquals(53, $this->countQuery($em));
  66. }
  67. public function testFindEntityOneToManyWithoutCache()
  68. {
  69. $em = $this->createEntityManager();
  70. $this->findEntityOneToMany($em, __FUNCTION__);
  71. self::assertEquals(502, $this->countQuery($em));
  72. }
  73. public function testFindEntityOneToManyWithCache()
  74. {
  75. parent::enableSecondLevelCache(false);
  76. $em = $this->createEntityManager();
  77. $this->findEntityOneToMany($em, __FUNCTION__);
  78. self::assertEquals(472, $this->countQuery($em));
  79. }
  80. public function testQueryEntityWithoutCache()
  81. {
  82. $em = $this->createEntityManager();
  83. $this->queryEntity($em, __FUNCTION__);
  84. self::assertEquals(602, $this->countQuery($em));
  85. }
  86. public function testQueryEntityWithCache()
  87. {
  88. parent::enableSecondLevelCache(false);
  89. $em = $this->createEntityManager();
  90. $this->queryEntity($em, __FUNCTION__);
  91. self::assertEquals(503, $this->countQuery($em));
  92. }
  93. private function queryEntity(EntityManagerInterface $em, $label)
  94. {
  95. $times = 100;
  96. $size = 500;
  97. $startPersist = microtime(true);
  98. echo PHP_EOL . $label;
  99. for ($i = 0; $i < $size; $i++) {
  100. $em->persist(new Country("Country $i"));
  101. }
  102. $em->flush();
  103. $em->clear();
  104. printf("\n[%s] persist %s countries", number_format(microtime(true) - $startPersist, 6), $size);
  105. $dql = 'SELECT c FROM Doctrine\Tests\Models\Cache\Country c WHERE c.name LIKE :name';
  106. $startFind = microtime(true);
  107. for ($i = 0; $i < $times; $i++) {
  108. $em->createQuery($dql)
  109. ->setParameter('name', "%Country%")
  110. ->setCacheable(true)
  111. ->getResult();
  112. }
  113. printf("\n[%s] select %s countries (%s times)", number_format(microtime(true) - $startFind, 6), $size, $times);
  114. printf("\n%s\n", str_repeat('-', 50));
  115. }
  116. public function findEntityOneToMany(EntityManagerInterface $em, $label)
  117. {
  118. $times = 50;
  119. $size = 30;
  120. $states = [];
  121. $cities = [];
  122. $startPersist = microtime(true);
  123. $country = new Country("Country");
  124. echo PHP_EOL . $label;
  125. $em->persist($country);
  126. $em->flush();
  127. for ($i = 0; $i < $size / 2; $i++) {
  128. $state = new State("State $i", $country);
  129. $em->persist($state);
  130. $states[] = $state;
  131. }
  132. $em->flush();
  133. foreach ($states as $key => $state) {
  134. for ($i = 0; $i < $size; $i++) {
  135. $city = new City("City $key - $i", $state);
  136. $em->persist($city);
  137. $state->addCity($city);
  138. $cities[] = $city;
  139. }
  140. }
  141. $em->flush();
  142. $em->clear();
  143. printf("\n[%s] persist %s states and %s cities", number_format( microtime(true) - $startPersist, 6), count($states), count($cities));
  144. $startFind = microtime(true);
  145. for ($i = 0; $i < $times; $i++) {
  146. foreach ($states as $state) {
  147. $state = $em->find(State::class, $state->getId());
  148. foreach ($state->getCities() as $city) {
  149. $city->getName();
  150. }
  151. }
  152. }
  153. printf("\n[%s] find %s states and %s cities (%s times)", number_format(microtime(true) - $startFind, 6), count($states), count($cities), $times);
  154. printf("\n%s\n", str_repeat('-', 50));
  155. }
  156. private function findEntity(EntityManagerInterface $em, $label)
  157. {
  158. $times = 10;
  159. $size = 500;
  160. $countries = [];
  161. $startPersist = microtime(true);
  162. echo PHP_EOL . $label;
  163. for ($i = 0; $i < $size; $i++) {
  164. $country = new Country("Country $i");
  165. $em->persist($country);
  166. $countries[] = $country;
  167. }
  168. $em->flush();
  169. $em->clear();
  170. printf("\n[%s] persist %s countries", number_format(microtime(true) - $startPersist, 6), $size);
  171. $startFind = microtime(true);
  172. for ($i = 0; $i <= $times; $i++) {
  173. foreach ($countries as $country) {
  174. $em->find(Country::class, $country->getId());
  175. $em->clear();
  176. }
  177. }
  178. printf("\n[%s] find %s countries (%s times)", number_format(microtime(true) - $startFind, 6), $size, $times);
  179. printf("\n%s\n", str_repeat('-', 50));
  180. }
  181. private function findAllEntity(EntityManagerInterface $em, $label)
  182. {
  183. $times = 100;
  184. $size = 50;
  185. $startPersist = microtime(true);
  186. $rep = $em->getRepository(Country::class);
  187. echo PHP_EOL . $label;
  188. for ($i = 0; $i < $size; $i++) {
  189. $em->persist(new Country("Country $i"));
  190. }
  191. $em->flush();
  192. $em->clear();
  193. printf("\n[%s] persist %s countries", number_format(microtime(true) - $startPersist, 6), $size);
  194. $startFind = microtime(true);
  195. for ($i = 0; $i <= $times; $i++) {
  196. $list = $rep->findAll();
  197. $em->clear();
  198. self::assertCount($size, $list);
  199. }
  200. printf("\n[%s] find %s countries (%s times)", number_format(microtime(true) - $startFind, 6), $size, $times);
  201. printf("\n%s\n", str_repeat('-', 50));
  202. }
  203. }