index.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. error_reporting(-1);
  3. require_once __DIR__.'/vendor/autoload.php';
  4. $app = new \Slim\App;
  5. // Test 1: JSON serialization
  6. $app->get('/json', function ($request, $response) {
  7. return $response
  8. ->withJson(array('message' => 'Hello, World!'))
  9. ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
  10. ;
  11. });
  12. $container = $app->getContainer();
  13. $container['db'] = function ($c) {
  14. $db = $c['settings']['db'];
  15. $pdo = new PDO('mysql:host=localhost;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass');
  16. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  17. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  18. return $pdo;
  19. };
  20. // Test 2: Single database query
  21. $app->get('/db', function ($request, $response) {
  22. $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
  23. $sth->execute(array(mt_rand(1, 10000)));
  24. $world = $sth->fetch();
  25. # Cast fields to int so they don't get wrapped with quotes
  26. $world['id'] = (int) $world['id'];
  27. $world['randomNumber'] = (int) $world['randomNumber'];
  28. return $response
  29. ->withJson($world)
  30. ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
  31. ;
  32. });
  33. // Test 3: Multiple database queries
  34. $app->get('/dbs', function ($request, $response) {
  35. $queries = max(1, min($request->getParam('queries'), 500));
  36. $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
  37. $worlds = array();
  38. for ($i = 0; $i < $queries; ++$i) {
  39. $sth->execute(array(mt_rand(1, 10000)));
  40. $world = $sth->fetch();
  41. # Cast fields to int so they don't get wrapped with quotes
  42. $world['id'] = (int) $world['id'];
  43. $world['randomNumber'] = (int) $world['randomNumber'];
  44. $worlds[] = $world;
  45. }
  46. return $response
  47. ->withJson($worlds)
  48. ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
  49. ;
  50. });
  51. $app->run();