once.php.inc 4.2 KB

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