app.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. if ($_GET['queries'] > 1) {
  16. $query_count = min($_GET['queries'], 500);
  17. }
  18. while ($query_count--) {
  19. $statement->execute([mt_rand(1, 10000)]);
  20. $arr[] = $statement->fetch(PDO::FETCH_ASSOC);
  21. }
  22. echo json_encode($arr, JSON_NUMERIC_CHECK);
  23. }
  24. function update()
  25. {
  26. global $pdo;
  27. $query_count = 1;
  28. if ($_GET['queries'] > 1) {
  29. $query_count = min($_GET['queries'], 500);
  30. }
  31. $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id=?');
  32. $updateStatement = $pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
  33. while ($query_count--) {
  34. $id = mt_rand(1, 10000);
  35. $statement->execute([$id]);
  36. $world = ['id' => $id, 'randomNumber' => $statement->fetchColumn()];
  37. $updateStatement->execute(
  38. [$world['randomNumber'] = mt_rand(1, 10000), $id]
  39. );
  40. $arr[] = $world;
  41. }
  42. echo json_encode($arr, JSON_NUMERIC_CHECK);
  43. }
  44. function fortune()
  45. {
  46. global $pdo;
  47. $fortune = $pdo->prepare('SELECT id,message FROM Fortune');
  48. $fortune->execute();
  49. $arr = $fortune->fetchAll(PDO::FETCH_KEY_PAIR);
  50. $arr[0] = 'Additional fortune added at request time.';
  51. asort($arr);
  52. $html = '';
  53. foreach ($arr as $id => $message) {
  54. $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
  55. $html .= "<tr><td>$id</td><td>$message</td></tr>";
  56. }
  57. echo '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>',
  58. $html,
  59. '</table></body></html>';
  60. }