app.php 2.2 KB

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