BenchController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 jsonAction()
  11. {
  12. return new JsonResponse(array('message' => 'Hello, World!'));
  13. }
  14. public function dbAction(Request $request)
  15. {
  16. $queries = $request->query->getInt('queries', 1);
  17. $queries = max(1, $queries);
  18. $queries = min(500, $queries);
  19. // possibility for enhancement is the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
  20. $worlds = array();
  21. $repo = $this->getDoctrine()
  22. ->getRepository('SkamanderBenchmarkBundle:World');
  23. for($i = 0; $i < $queries; ++$i) {
  24. $worlds[] = $repo->find(mt_rand(1, 10000));
  25. }
  26. if ($queries == 1) {
  27. $worlds = $worlds[0];
  28. }
  29. return new JsonResponse($worlds);
  30. }
  31. public function dbRawAction(Request $request)
  32. {
  33. $queries = $request->query->getInt('queries', 1);
  34. // possibility for enhancement is the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
  35. $worlds = array();
  36. $conn = $this->get('database_connection');
  37. for($i = 0; $i < $queries; ++$i) {
  38. $worlds[] = $conn->fetchAssoc('SELECT * FROM World WHERE id = ?', array(mt_rand(1, 10000)));
  39. }
  40. if ($queries == 1) {
  41. $worlds = $worlds[0];
  42. }
  43. return new JsonResponse($worlds);
  44. }
  45. public function fortunesAction()
  46. {
  47. $repo = $this->getDoctrine()
  48. ->getRepository('SkamanderBenchmarkBundle:Fortune');
  49. $fortunes = $repo->findAll();
  50. $runtimeFortune = new Fortune();
  51. $runtimeFortune->setId(0)
  52. ->setMessage('Additional fortune added at request time.');
  53. $fortunes[] = $runtimeFortune;
  54. usort($fortunes, function($left, $right) {
  55. return strcmp($left->message, $right->message);
  56. });
  57. return $this->render("SkamanderBenchmarkBundle:Bench:fortunes.html.twig", [
  58. 'fortunes' => $fortunes
  59. ]);
  60. }
  61. public function fortunesPhpAction()
  62. {
  63. $repo = $this->getDoctrine()
  64. ->getRepository('SkamanderBenchmarkBundle:Fortune');
  65. $fortunes = $repo->findAll();
  66. $runtimeFortune = new Fortune();
  67. $runtimeFortune->setId(0)
  68. ->setMessage('Additional fortune added at request time.');
  69. $fortunes[] = $runtimeFortune;
  70. usort($fortunes, function($left, $right) {
  71. return strcmp($left->message, $right->message);
  72. });
  73. return $this->render("SkamanderBenchmarkBundle:Bench:fortunes.html.php", [
  74. 'fortunes' => $fortunes
  75. ]);
  76. }
  77. public function fortunesRawAction()
  78. {
  79. $repo = $this->getDoctrine()
  80. ->getRepository('SkamanderBenchmarkBundle:Fortune');
  81. $fortunes = $repo->findAll();
  82. $runtimeFortune = new Fortune();
  83. $runtimeFortune->setId(0)
  84. ->setMessage('Additional fortune added at request time.');
  85. $fortunes[] = $runtimeFortune;
  86. usort($fortunes, function($left, $right) {
  87. return strcmp($left->message, $right->message);
  88. });
  89. // This is not the symfony way to work with templates! It's implemented to show users
  90. // who don't want to use template engines (like twig), or template sugar (like the slots etc.
  91. // from symfony 2), because in their opinion already built-in php constructs like foreach +
  92. // if else + include etc. are enough, that the performance impact should be neglectable, and
  93. // that the advantages outweigh the disadvantages (performance).
  94. $title = 'Fortunes';
  95. ob_start();
  96. include __DIR__ . '/../Resources/views/Bench/raw/content.php';
  97. $response = ob_get_clean();
  98. return new Response($response);
  99. }
  100. }