app.php 2.1 KB

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