BenchController.php 3.9 KB

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