index.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. error_reporting(-1);
  3. require_once __DIR__.'/vendor/autoload.php';
  4. $app = new \Slim\App;
  5. $container = $app->getContainer();
  6. $container['db'] = function ($c) {
  7. $db = $c['settings']['db'];
  8. $pdo = new PDO('mysql:host=127.0.0.1;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass');
  9. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  11. return $pdo;
  12. };
  13. $container['view'] = new \Slim\Views\PhpRenderer("templates/");
  14. // Test 1: Plaintext
  15. $app->get('/plaintext', function ($request, $response) {
  16. return $response
  17. ->write('Hello, World!')
  18. ->withHeader('Content-Type', 'text/plain')
  19. ;
  20. });
  21. // Test 2: JSON serialization
  22. $app->get('/json', function ($request, $response) {
  23. return $response
  24. ->withJson(array('message' => 'Hello, World!'))
  25. ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
  26. ;
  27. });
  28. // Test 3: Single database query
  29. $app->get('/db', function ($request, $response) {
  30. $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
  31. $sth->execute(array(mt_rand(1, 10000)));
  32. $world = $sth->fetch();
  33. # Cast fields to int so they don't get wrapped with quotes
  34. $world['id'] = (int) $world['id'];
  35. $world['randomNumber'] = (int) $world['randomNumber'];
  36. return $response
  37. ->withJson($world)
  38. ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
  39. ;
  40. });
  41. // Test 4: Multiple database queries
  42. $app->get('/dbs', function ($request, $response) {
  43. $queries = max(1, min($request->getParam('queries'), 500));
  44. $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
  45. $worlds = array();
  46. for ($i = 0; $i < $queries; ++$i) {
  47. $sth->execute(array(mt_rand(1, 10000)));
  48. $world = $sth->fetch();
  49. # Cast fields to int so they don't get wrapped with quotes
  50. $world['id'] = (int) $world['id'];
  51. $world['randomNumber'] = (int) $world['randomNumber'];
  52. $worlds[] = $world;
  53. }
  54. return $response
  55. ->withJson($worlds)
  56. ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
  57. ;
  58. });
  59. // Test 5: Updates
  60. $app->get('/updates', function ($request, $response) {
  61. $queries = max(1, min($request->getParam('queries'), 500));
  62. $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
  63. $worlds = array();
  64. for ($i = 0; $i < $queries; ++$i) {
  65. $id = mt_rand(1, 10000);
  66. $random_number = mt_rand(1, 10000);
  67. $sth->execute(array($id));
  68. $world = $sth->fetch();
  69. # Cast fields to int so they don't get wrapped with quotes
  70. $world['id'] = (int) $world['id'];
  71. $world['randomNumber'] = $random_number;
  72. $update_query = $this->db->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
  73. $update_query->execute(array($world['randomNumber'], $world['id']));
  74. $worlds[] = $world;
  75. }
  76. return $response
  77. ->withJson($worlds)
  78. ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
  79. ;
  80. });
  81. // Test 6: Fortunes
  82. $app->get('/fortunes', function ($request, $response) {
  83. $sth = $this->db->prepare('SELECT * FROM Fortune');
  84. $sth->execute();
  85. $fortunes = $sth->fetchAll();
  86. array_push($fortunes, array('id'=> 0, 'message' => 'Additional fortune added at request time.'));
  87. usort($fortunes, function($left, $right) {
  88. return strcmp($left['message'], $right['message']);
  89. });
  90. return $this->view->render($response, "fortunes.php", ["fortunes" => $fortunes]);
  91. });
  92. $app->run();