once.php.inc 4.3 KB

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