once.php.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. class Benchmark {
  3. var $pdo;
  4. public function setup_db()
  5. {
  6. $this->pdo = new PDO(
  7. 'mysql:host=TFB-database;dbname=hello_world;charset=utf8',
  8. 'benchmarkdbuser',
  9. 'benchmarkdbpass',
  10. array(PDO::ATTR_PERSISTENT => false));
  11. }
  12. public function bench_json()
  13. {
  14. header('Content-Type: application/json');
  15. echo json_encode(array('message' => 'Hello, World!'));
  16. }
  17. public function bench_plaintext()
  18. {
  19. header('Content-Type: text/plain; charset=utf-8');
  20. echo 'Hello, World!';
  21. }
  22. public function bench_db()
  23. {
  24. $this->setup_db();
  25. $id = mt_rand(1, 10000);
  26. // Define query
  27. $statement = $this->pdo->prepare('SELECT randomNumber FROM World WHERE id = :id');
  28. $statement->bindParam(':id', $id, PDO::PARAM_INT);
  29. $statement->execute();
  30. // Store result in array.
  31. $arr = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
  32. // Send the required parameters
  33. header('Content-Type: application/json');
  34. echo json_encode($arr);
  35. }
  36. public function bench_queries($query_count=1)
  37. {
  38. $this->setup_db();
  39. // Create an array with the response string.
  40. $arr = array();
  41. $id = mt_rand(1, 10000);
  42. // Define query
  43. $statement = $this->pdo->prepare('SELECT randomNumber FROM World WHERE id = :id');
  44. $statement->bindParam(':id', $id, PDO::PARAM_INT);
  45. // For each query, store the result set values in the response array
  46. while (0 < $query_count--) {
  47. $statement->execute();
  48. // Store result in array.
  49. $arr[] = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
  50. $id = mt_rand(1, 10000);
  51. }
  52. // Send the required parameters
  53. header('Content-Type: application/json');
  54. echo json_encode($arr);
  55. }
  56. public function bench_updates($query_count)
  57. {
  58. $this->setup_db();
  59. // Create an array with the response string.
  60. $arr = array();
  61. $id = mt_rand(1, 10000);
  62. $randomNumber = mt_rand(1, 1000);
  63. // Define query
  64. $statement = $this->pdo->prepare('SELECT randomNumber FROM World WHERE id = :id');
  65. $statement->bindParam(':id', $id, PDO::PARAM_INT);
  66. $updateStatement = $this->pdo->prepare('UPDATE World SET randomNumber = :randomNumber WHERE id = :id');
  67. $updateStatement->bindParam(':id', $id, PDO::PARAM_INT);
  68. $updateStatement->bindParam(':randomNumber', $randomNumber, PDO::PARAM_INT);
  69. // For each query, store the result set values in the response array
  70. while (0 < $query_count--) {
  71. $statement->execute();
  72. // Store result in array.
  73. $world = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
  74. $world['randomNumber'] = $randomNumber;
  75. $updateStatement->execute();
  76. $arr[] = $world;
  77. $id = mt_rand(1, 10000);
  78. $randomNumber = mt_rand(1, 10000);
  79. }
  80. // Send the required parameters
  81. header('Content-Type: application/json');
  82. echo json_encode($arr);
  83. }
  84. public function bench_fortunes()
  85. {
  86. $this->setup_db();
  87. // Define query
  88. $statement = $this->pdo->query( 'SELECT id, message FROM Fortune' );
  89. // Store result in array.
  90. $arr = $statement->fetchAll(PDO::FETCH_KEY_PAIR);
  91. $arr[0] = 'Additional fortune added at request time.';
  92. asort($arr);
  93. header("Content-Type: text/html; charset=utf-8");
  94. echo <<<EOM
  95. <!DOCTYPE html>
  96. <html>
  97. <head>
  98. <title>Fortunes</title>
  99. </head>
  100. <body>
  101. <table>
  102. <tr>
  103. <th>id</th>
  104. <th>message</th>
  105. </tr>
  106. EOM;
  107. foreach ( $arr as $id => $fortune ) {
  108. echo '<tr>';
  109. echo '<td>'.$id.'</td>';
  110. echo '<td>'.htmlspecialchars($fortune, ENT_QUOTES, 'utf-8').'</td>';
  111. echo '</tr>';
  112. }
  113. echo <<<EOM
  114. </table>
  115. </body>
  116. </html>
  117. EOM;
  118. }
  119. }