index.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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=192.168.100.102;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. $queries = $request->query->getInt('queries', 1);
  20. // possibility for micro enhancement could be the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
  21. $worlds = array();
  22. for($i = 0; $i < $queries; ++$i) {
  23. $worlds[] = $app['db']->fetchAssoc('SELECT * FROM World WHERE id = ?', array(mt_rand(1, 10000)));
  24. }
  25. if (count($worlds) == 1) {
  26. $worlds = $worlds[0];
  27. }
  28. return new JsonResponse($worlds);
  29. });
  30. $app->run();