index.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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=tfb-database;dbname=hello_world;charset=utf8', 'benchmarkdbuser', 'benchmarkdbpass', array(
  7. PDO::ATTR_PERSISTENT => true,
  8. ));
  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. 'view' => function ($c) {
  14. return new Slim\Views\PhpRenderer("templates/");
  15. },
  16. 'settings' => [
  17. 'outputBuffering' => false,
  18. ]
  19. ));
  20. // Test 1: Plaintext
  21. $app->get('/plaintext', function ($request, $response) {
  22. return $response
  23. ->write('Hello, World!')
  24. ->withHeader('Content-Type', 'text/plain')
  25. ;
  26. });
  27. // Test 2: JSON serialization
  28. $app->get('/json', function ($request, $response) {
  29. return $response
  30. ->withJson(array('message' => 'Hello, World!'))
  31. ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
  32. ;
  33. });
  34. // Test 3: Single database query
  35. $app->get('/db', function ($request, $response) {
  36. $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
  37. $sth->execute(array(mt_rand(1, 10000)));
  38. $world = $sth->fetch();
  39. # Cast fields to int so they don't get wrapped with quotes
  40. $world['id'] = (int) $world['id'];
  41. $world['randomNumber'] = (int) $world['randomNumber'];
  42. return $response
  43. ->withJson($world)
  44. ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
  45. ;
  46. });
  47. // Test 4: Multiple database queries
  48. $app->get('/dbs', function ($request, $response) {
  49. $queries = $request->getParam('queries');
  50. if (is_numeric($queries)) {
  51. $queries = max(1, min($queries, 500));
  52. } else {
  53. $queries = 1;
  54. }
  55. $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
  56. $worlds = array();
  57. for ($i = 0; $i < $queries; ++$i) {
  58. $sth->execute(array(mt_rand(1, 10000)));
  59. $world = $sth->fetch();
  60. # Cast fields to int so they don't get wrapped with quotes
  61. $world['id'] = (int) $world['id'];
  62. $world['randomNumber'] = (int) $world['randomNumber'];
  63. $worlds[] = $world;
  64. }
  65. return $response
  66. ->withJson($worlds)
  67. ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
  68. ;
  69. });
  70. // Test 5: Updates
  71. $app->get('/updates', function ($request, $response) {
  72. $queries = $request->getParam('queries');
  73. if (is_numeric($queries)) {
  74. $queries = max(1, min($queries, 500));
  75. } else {
  76. $queries = 1;
  77. }
  78. $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
  79. $updateSth = $this->db->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
  80. $worlds = array();
  81. for ($i = 0; $i < $queries; ++$i) {
  82. $id = mt_rand(1, 10000);
  83. $random_number = mt_rand(1, 10000);
  84. $sth->execute(array($id));
  85. $world = $sth->fetch();
  86. # Cast fields to int so they don't get wrapped with quotes
  87. $world['id'] = (int) $world['id'];
  88. $world['randomNumber'] = $random_number;
  89. $updateSth->execute(array($world['randomNumber'], $world['id']));
  90. $worlds[] = $world;
  91. }
  92. return $response
  93. ->withJson($worlds)
  94. ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
  95. ;
  96. });
  97. // Test 6: Fortunes
  98. $app->get('/fortunes', function ($request, $response) {
  99. $fortunes = $this->db->query('SELECT * FROM Fortune')->fetchAll(PDO::FETCH_KEY_PAIR);
  100. $fortunes[0] = 'Additional fortune added at request time.';
  101. asort($fortunes);
  102. return $this->view->render($response, "fortunes.php", ["fortunes" => $fortunes]);
  103. });
  104. $app->run();