BenchController.php 4.0 KB

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