BenchController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = min(max($queries, 1), 500);
  22. // possibility for enhancement is the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
  23. $worlds = array();
  24. $repo = $this->getDoctrine()
  25. ->getRepository('SkamanderBenchmarkBundle:World');
  26. for ($i = 0; $i < $queries; ++$i) {
  27. $worlds[] = $repo->find(mt_rand(1, 10000));
  28. }
  29. if ($queries == 1 && !$request->query->has('queries')) {
  30. $worlds = $worlds[0];
  31. }
  32. return new JsonResponse($worlds);
  33. }
  34. public function dbRawAction(Request $request)
  35. {
  36. $queries = $request->query->getInt('queries', 1);
  37. $queries = min(max($queries, 1), 500);
  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 && !$request->query->has('queries')) {
  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. $worlds[] = $world;
  62. }
  63. $em->flush();
  64. return new JsonResponse($worlds);
  65. }
  66. public function updateRawAction(Request $request)
  67. {
  68. $queries = $request->query->getInt('queries', 1);
  69. $queries = min(500, max(1, $queries));
  70. $worlds = array();
  71. $conn = $this->get('database_connection');
  72. for($i = 0; $i < $queries; ++$i) {
  73. $id = mt_rand(1, 10000);
  74. $random_number = mt_rand(1, 10000);
  75. $conn->executeUpdate('UPDATE World SET randomNumber=? WHERE id=?', array($random_number, $id));
  76. $worlds[] = array('id' => $id, 'randomNumber' => $random_number);
  77. }
  78. return new JsonResponse($worlds);
  79. }
  80. public function fortunesAction()
  81. {
  82. $repo = $this->getDoctrine()
  83. ->getRepository('SkamanderBenchmarkBundle:Fortune');
  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("SkamanderBenchmarkBundle:Bench:fortunes.html.twig", [
  93. 'fortunes' => $fortunes
  94. ]);
  95. }
  96. }