app.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass',
  3. [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]);
  4. $statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
  5. $fortune = $pdo->prepare('SELECT id,message FROM Fortune');
  6. function db()
  7. {
  8. global $statement;
  9. ngx_header_set('Content-Type', 'application/json');
  10. $statement->execute([mt_rand(1, 10000)]);
  11. echo json_encode($statement->fetch(), JSON_NUMERIC_CHECK);
  12. }
  13. function query()
  14. {
  15. global $statement;
  16. ngx_header_set('Content-Type', 'application/json');
  17. $query_count = 1;
  18. $params = ngx::query_args()['queries'];
  19. if ($params > 1) {
  20. $query_count = min($params, 500);
  21. }
  22. while ($query_count--) {
  23. $statement->execute([mt_rand(1, 10000)]);
  24. $arr[] = $statement->fetch();
  25. }
  26. echo json_encode($arr, JSON_NUMERIC_CHECK);
  27. }
  28. function update()
  29. {
  30. global $pdo;
  31. ngx_header_set('Content-Type', 'application/json');
  32. $query_count = 1;
  33. $params = ngx::query_args()['queries'];
  34. if ($params > 1) {
  35. $query_count = min($params, 500);
  36. }
  37. $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id=?');
  38. $update = '';
  39. while ($query_count--) {
  40. $id = mt_rand(1, 10000);
  41. $statement->execute([$id]);
  42. $world = ['id' => $id, 'randomNumber' => $statement->fetchColumn()];
  43. $world['randomNumber'] = mt_rand(1, 10000);
  44. $update .= "UPDATE World SET randomNumber={$world['randomNumber']} WHERE id=$id;";
  45. $arr[] = $world;
  46. }
  47. $pdo->exec($update);
  48. echo json_encode($arr, JSON_NUMERIC_CHECK);
  49. }
  50. function fortune()
  51. {
  52. global $fortune;
  53. ngx_header_set('Content-Type', 'text/html;charset=UTF-8');
  54. $fortune->execute();
  55. $arr = $fortune->fetchAll(PDO::FETCH_KEY_PAIR);
  56. $arr[0] = 'Additional fortune added at request time.';
  57. asort($arr);
  58. $html = '';
  59. foreach ($arr as $id => $message) {
  60. $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
  61. $html .= "<tr><td>$id</td><td>$message</td></tr>";
  62. }
  63. echo '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>',
  64. $html,
  65. '</table></body></html>';
  66. }