index.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. use Symfony\Component\HttpFoundation\JsonResponse;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider;
  5. use Silex\Provider\DoctrineServiceProvider;
  6. error_reporting(-1);
  7. $loader = require_once __DIR__.'/../vendor/autoload.php';
  8. \Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
  9. $app = new Silex\Application();
  10. $dbh = new PDO('mysql:host=127.0.0.1;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
  11. PDO::ATTR_PERSISTENT => true
  12. ));
  13. $app->register(new Silex\Provider\DoctrineServiceProvider(), array(
  14. 'db.options' => array(
  15. 'pdo' => $dbh
  16. ),
  17. ));
  18. $app->register(new DoctrineOrmServiceProvider, array(
  19. 'orm.proxies_dir' => __DIR__.'/../proxies', // Not sure how or if this needs handling...
  20. 'orm.em.options' => array(
  21. 'mappings' => array(
  22. array(
  23. 'type' => 'annotation',
  24. 'namespace' => 'Entity',
  25. 'path' => __DIR__.'/../src/Entity',
  26. 'use_simple_annotation_reader' => false,
  27. ),
  28. )
  29. ),
  30. ));
  31. $app->get('/json', function() {
  32. return new JsonResponse(array('message' => 'Hello, World!'));
  33. });
  34. $app->get('/db', function(Request $request) use ($app) {
  35. $repo = $app['orm.em']->getRepository('Entity\World');
  36. $worlds = $repo->find(mt_rand(1, 10000));
  37. return new JsonResponse($worlds);
  38. });
  39. $app->get('/queries', function(Request $request) use ($app) {
  40. $queries = $request->query->getInt('queries', 1);
  41. $queries = is_numeric($queries) ? min(max($queries, 1), 500) : 1;
  42. // possibility for micro enhancement could be the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
  43. $worlds = array();
  44. $repo = $app['orm.em']->getRepository('Entity\World');
  45. for ($i = 0; $i < $queries; ++$i) {
  46. $worlds[] = $repo->find(mt_rand(1, 10000));
  47. }
  48. return new JsonResponse($worlds);
  49. });
  50. $app->run();