BenchController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Skamander\BenchmarkBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Skamander\BenchmarkBundle\Entity\Fortune;
  8. class BenchController extends Controller
  9. {
  10. public function plaintextAction()
  11. {
  12. return new Response("Hello, World!", 200, array('Content-Type' => 'text/plain'));
  13. }
  14. public function jsonAction()
  15. {
  16. return new JsonResponse(array('message' => 'Hello, World!'));
  17. }
  18. public function dbAction(Request $request)
  19. {
  20. $queries = $request->query->getInt('queries', 1);
  21. $queries = max(1, $queries);
  22. $queries = min(500, $queries);
  23. // possibility for enhancement is the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
  24. $worlds = array();
  25. $repo = $this->getDoctrine()
  26. ->getRepository('SkamanderBenchmarkBundle:World');
  27. for ($i = 0; $i < $queries; ++$i) {
  28. $worlds[] = $repo->find(mt_rand(1, 10000));
  29. }
  30. if ($queries == 1 && !$request->query->has('queries')) {
  31. $worlds = $worlds[0];
  32. }
  33. return new JsonResponse($worlds);
  34. }
  35. public function dbRawAction(Request $request)
  36. {
  37. $queries = $request->query->getInt('queries', 1);
  38. // possibility for enhancement is the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
  39. $worlds = array();
  40. $conn = $this->get('database_connection');
  41. for($i = 0; $i < $queries; ++$i) {
  42. $worlds[] = $conn->fetchAssoc('SELECT * FROM World WHERE id = ?', array(mt_rand(1, 10000)));
  43. }
  44. if ($queries == 1) {
  45. $worlds = $worlds[0];
  46. }
  47. return new JsonResponse($worlds);
  48. }
  49. public function updateAction(Request $request)
  50. {
  51. $queries = $request->query->getInt('queries', 1);
  52. $queries = min(500, max(1, $queries));
  53. $worlds = array();
  54. $em = $this->getDoctrine()->getManager();
  55. $repo = $this->getDoctrine()
  56. ->getRepository('SkamanderBenchmarkBundle:World');
  57. for ($i = 0; $i < $queries; ++$i) {
  58. $world = $repo->find(mt_rand(1, 10000));
  59. $random_number = mt_rand(1, 10000);
  60. $world->setRandomNumber($random_number);
  61. $em->persist($world);
  62. $worlds[] = $world;
  63. }
  64. $em->flush();
  65. return new JsonResponse($worlds);
  66. }
  67. public function updateRawAction(Request $request)
  68. {
  69. $queries = $request->query->getInt('queries', 1);
  70. $queries = min(500, max(1, $queries));
  71. $worlds = array();
  72. $conn = $this->get('database_connection');
  73. for($i = 0; $i < $queries; ++$i) {
  74. $id = mt_rand(1, 10000);
  75. $random_number = mt_rand(1, 10000);
  76. $conn->executeUpdate('UPDATE World SET randomNumber=? WHERE id=?', array($random_number, $id));
  77. $worlds[] = array('id' => $id, 'randomNumber' => $random_number);
  78. }
  79. return new JsonResponse($worlds);
  80. }
  81. public function fortunesAction()
  82. {
  83. $repo = $this->getDoctrine()
  84. ->getRepository('SkamanderBenchmarkBundle:Fortune');
  85. $fortunes = $repo->findAll();
  86. $runtimeFortune = new Fortune();
  87. $runtimeFortune->setId(0)
  88. ->setMessage('Additional fortune added at request time.');
  89. $fortunes[] = $runtimeFortune;
  90. usort($fortunes, function($left, $right) {
  91. return strcmp($left->message, $right->message);
  92. });
  93. return $this->render("SkamanderBenchmarkBundle:Bench:fortunes.html.twig", [
  94. 'fortunes' => $fortunes
  95. ]);
  96. }
  97. public function fortunesPhpAction()
  98. {
  99. $repo = $this->getDoctrine()
  100. ->getRepository('SkamanderBenchmarkBundle:Fortune');
  101. $fortunes = $repo->findAll();
  102. $runtimeFortune = new Fortune();
  103. $runtimeFortune->setId(0)
  104. ->setMessage('Additional fortune added at request time.');
  105. $fortunes[] = $runtimeFortune;
  106. usort($fortunes, function($left, $right) {
  107. return strcmp($left->message, $right->message);
  108. });
  109. return $this->render("SkamanderBenchmarkBundle:Bench:fortunes.html.php", [
  110. 'fortunes' => $fortunes
  111. ]);
  112. }
  113. public function fortunesRawAction()
  114. {
  115. $repo = $this->getDoctrine()
  116. ->getRepository('SkamanderBenchmarkBundle:Fortune');
  117. $fortunes = $repo->findAll();
  118. $runtimeFortune = new Fortune();
  119. $runtimeFortune->setId(0)
  120. ->setMessage('Additional fortune added at request time.');
  121. $fortunes[] = $runtimeFortune;
  122. usort($fortunes, function($left, $right) {
  123. return strcmp($left->message, $right->message);
  124. });
  125. // This is not the symfony way to work with templates! It's implemented to show users
  126. // who don't want to use template engines (like twig), or template sugar (like the slots etc.
  127. // from symfony 2), because in their opinion already built-in php constructs like foreach +
  128. // if else + include etc. are enough, that the performance impact should be neglectable, and
  129. // that the advantages outweigh the disadvantages (performance).
  130. $title = 'Fortunes';
  131. ob_start();
  132. include __DIR__ . '/../Resources/views/Bench/raw/content.php';
  133. $response = ob_get_clean();
  134. return new Response($response);
  135. }
  136. }