app.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass');
  3. function db()
  4. {
  5. global $pdo;
  6. ngx_header_set('Content-Type', 'application/json');
  7. $statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
  8. $statement->execute([mt_rand(1, 10000)]);
  9. echo json_encode($statement->fetch(PDO::FETCH_ASSOC), JSON_NUMERIC_CHECK);
  10. }
  11. function query()
  12. {
  13. global $pdo;
  14. ngx_header_set('Content-Type', 'application/json');
  15. $statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
  16. $query_count = 1;
  17. $params = ngx::query_args()['queries'];
  18. if ($params > 1) {
  19. $query_count = min($params, 500);
  20. }
  21. while ($query_count--) {
  22. $statement->execute([mt_rand(1, 10000)]);
  23. $arr[] = $statement->fetch(PDO::FETCH_ASSOC);
  24. }
  25. echo json_encode($arr, JSON_NUMERIC_CHECK);
  26. }
  27. function update()
  28. {
  29. global $pdo;
  30. ngx_header_set('Content-Type', 'application/json');
  31. $query_count = 1;
  32. $params = ngx::query_args()['queries'];
  33. if ($params > 1) {
  34. $query_count = min($params, 500);
  35. }
  36. $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id=?');
  37. $update = '';
  38. while ($query_count--) {
  39. $id = mt_rand(1, 10000);
  40. $statement->execute([$id]);
  41. $world = ['id' => $id, 'randomNumber' => $statement->fetchColumn()];
  42. $world['randomNumber'] = mt_rand(1, 10000);
  43. $update .= "UPDATE World SET randomNumber={$world['randomNumber']} WHERE id=$id;";
  44. $arr[] = $world;
  45. }
  46. $pdo->exec($update);
  47. echo json_encode($arr, JSON_NUMERIC_CHECK);
  48. }
  49. function fortune()
  50. {
  51. global $pdo;
  52. ngx_header_set('Content-Type', 'text/html;charset=UTF-8');
  53. $fortune = $pdo->prepare('SELECT id,message FROM Fortune');
  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. }