app.php 2.3 KB

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