once.php.inc 3.8 KB

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