app.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. global $pdo;
  3. $pdo = new PDO("mysql:host=tfb-database;dbname=hello_world", "benchmarkdbuser", "benchmarkdbpass");
  4. function db()
  5. {
  6. global $pdo;
  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. $statement = $pdo->prepare("SELECT id,randomNumber FROM World WHERE id=?");
  15. $query_count = 1;
  16. if ($_GET["queries"] > 1) {
  17. $query_count = min($_GET["queries"], 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. if ($_GET["queries"] > 1) {
  30. $query_count = min($_GET["queries"], 500);
  31. }
  32. $statement = $pdo->prepare("SELECT randomNumber FROM World WHERE id=?");
  33. $updateStatement = $pdo->prepare("UPDATE World SET randomNumber=? WHERE id=?");
  34. while ($query_count--) {
  35. $id = mt_rand(1, 10000);
  36. $statement->execute([$id]);
  37. $world = ["id" => $id, "randomNumber" => $statement->fetchColumn()];
  38. $updateStatement->execute(
  39. [$world["randomNumber"] = mt_rand(1, 10000), $id]
  40. );
  41. $arr[] = $world;
  42. }
  43. echo json_encode($arr, JSON_NUMERIC_CHECK);
  44. }
  45. function fortune()
  46. {
  47. global $pdo;
  48. $fortune = $pdo->prepare("SELECT id,message FROM Fortune");
  49. $fortune->execute();
  50. $arr = $fortune->fetchAll(PDO::FETCH_KEY_PAIR);
  51. $arr[0] = "Additional fortune added at request time.";
  52. asort($arr);
  53. $html = "";
  54. foreach ($arr as $id => $message) {
  55. $message = htmlspecialchars($message, ENT_QUOTES, "UTF-8");
  56. $html .= "<tr><td>$id</td><td>$message</td></tr>";
  57. }
  58. echo "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>",
  59. $html,
  60. "</table></body></html>";
  61. }