index.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. use Symfony\Component\HttpFoundation\JsonResponse;
  3. use Symfony\Component\HttpFoundation\Request;
  4. error_reporting(-1);
  5. require_once __DIR__.'/../vendor/autoload.php';
  6. $app = new Silex\Application();
  7. $dbh = new PDO('mysql:host=localhost;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
  8. PDO::ATTR_PERSISTENT => true
  9. ));
  10. $app->register(new Silex\Provider\DoctrineServiceProvider(), array(
  11. 'db.options' => array(
  12. 'pdo' => $dbh
  13. ),
  14. ));
  15. $app->get('/json', function() {
  16. return new JsonResponse(array("message" => "Hello, World!"));
  17. });
  18. $app->get('/db', function(Request $request) use ($app) {
  19. $world = $app['db']->fetchAssoc('SELECT * FROM World WHERE id = ?', array(mt_rand(1, 10000)));
  20. $world['id'] = (int) $world['id'];
  21. $world['randomNumber'] = (int) $world['randomNumber'];
  22. return new JsonResponse($world);
  23. });
  24. $app->get('/queries', function(Request $request) use ($app) {
  25. $queries = $request->query->getInt('queries', 1);
  26. if ($queries < 1) {
  27. $queries = 1;
  28. }
  29. elseif ($queries > 500) {
  30. $queries = 500;
  31. }
  32. // possibility for micro enhancement could be the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
  33. $worlds = array();
  34. for($i = 0; $i < $queries; ++$i) {
  35. $world = $app['db']->fetchAssoc('SELECT * FROM World WHERE id = ?', array(mt_rand(1, 10000)));
  36. $world['id'] = (int) $world['id'];
  37. $world['randomNumber'] = (int) $world['randomNumber'];
  38. $worlds[] = $world;
  39. }
  40. return new JsonResponse($worlds);
  41. });
  42. $app->run();