BenchController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use App\Entity\Fortune;
  9. use App\Entity\World;
  10. class BenchController extends Controller
  11. {
  12. use ControllerTrait;
  13. public function plaintextAction()
  14. {
  15. return new Response("Hello, World!", 200, array('Content-Type' => 'text/plain'));
  16. }
  17. public function jsonAction()
  18. {
  19. return new JsonResponse(array('message' => 'Hello, World!'));
  20. }
  21. public function dbAction(Request $request)
  22. {
  23. $queries = $request->query->getInt('queries', 1);
  24. $queries = min(max($queries, 1), 500);
  25. // possibility for enhancement is the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
  26. $worlds = array();
  27. $repo = $this->getDoctrine()->getRepository(World::class);
  28. for ($i = 0; $i < $queries; ++$i) {
  29. $worlds[] = $repo->find(mt_rand(1, 10000));
  30. }
  31. if ($queries == 1 && !$request->query->has('queries')) {
  32. $worlds = $worlds[0];
  33. }
  34. return new JsonResponse($worlds);
  35. }
  36. public function dbRawAction(Request $request)
  37. {
  38. $queries = $request->query->getInt('queries', 1);
  39. $queries = min(max($queries, 1), 500);
  40. // possibility for enhancement is the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
  41. $worlds = array();
  42. $conn = $this->get('database_connection');
  43. for ($i = 0; $i < $queries; ++$i) {
  44. $worlds[] = $conn->fetchAssoc('SELECT * FROM world WHERE id = ?', array(mt_rand(1, 10000)));
  45. }
  46. if ($queries == 1 && !$request->query->has('queries')) {
  47. $worlds = $worlds[0];
  48. }
  49. return new JsonResponse($worlds);
  50. }
  51. public function updateAction(Request $request)
  52. {
  53. $queries = $request->query->getInt('queries', 1);
  54. $queries = min(500, max(1, $queries));
  55. $worlds = array();
  56. $em = $this->getDoctrine()->getManager();
  57. $repo = $this->getDoctrine()->getRepository(World::class);
  58. for ($i = 0; $i < $queries; ++$i) {
  59. $world = $repo->find(mt_rand(1, 10000));
  60. $randomNumber = mt_rand(1, 10000);
  61. $world->setRandomNumber($randomNumber);
  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. $randomNumber = mt_rand(1, 10000);
  76. $conn->executeUpdate('UPDATE world SET randomNumber=? WHERE id=?', array($randomNumber, $id));
  77. $worlds[] = array('id' => $id, 'randomNumber' => $randomNumber);
  78. }
  79. return new JsonResponse($worlds);
  80. }
  81. public function fortunesAction()
  82. {
  83. $repo = $this->getDoctrine()->getRepository(Fortune::class);
  84. $fortunes = $repo->findAll();
  85. $runtimeFortune = new Fortune();
  86. $runtimeFortune->setId(0)
  87. ->setMessage('Additional fortune added at request time.');
  88. $fortunes[] = $runtimeFortune;
  89. usort($fortunes, function($left, $right) {
  90. return strcmp($left->message, $right->message);
  91. });
  92. return $this->render("bench/fortunes.html.twig", [
  93. 'fortunes' => $fortunes
  94. ]);
  95. }
  96. }