app.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. function db()
  9. {
  10. global $statement;
  11. ngx_header_set('Content-Type', 'application/json');
  12. $statement->execute([mt_rand(1, 10000)]);
  13. echo json_encode($statement->fetch(), JSON_NUMERIC_CHECK);
  14. }
  15. function query()
  16. {
  17. global $statement;
  18. ngx_header_set('Content-Type', 'application/json');
  19. $query_count = 1;
  20. $params = ngx::query_args()['queries'];
  21. if ($params > 1) {
  22. $query_count = min($params, 500);
  23. }
  24. while ($query_count--) {
  25. $statement->execute([mt_rand(1, 10000)]);
  26. $arr[] = $statement->fetch();
  27. }
  28. echo json_encode($arr, JSON_NUMERIC_CHECK);
  29. }
  30. function update()
  31. {
  32. global $pdo;
  33. ngx_header_set('Content-Type', 'application/json');
  34. $query_count = 1;
  35. $params = ngx::query_args()['queries'];
  36. if ($params > 1) {
  37. $query_count = min($params, 500);
  38. }
  39. $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id=?');
  40. $update = '';
  41. while ($query_count--) {
  42. $id = mt_rand(1, 10000);
  43. $statement->execute([$id]);
  44. $world = ['id' => $id, 'randomNumber' => $statement->fetchColumn()];
  45. $world['randomNumber'] = mt_rand(1, 10000);
  46. $update .= "UPDATE World SET randomNumber={$world['randomNumber']} WHERE id=$id;";
  47. $arr[] = $world;
  48. }
  49. $pdo->exec($update);
  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>',
  66. $html,
  67. '</table></body></html>';
  68. }