index.php 3.6 KB

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