app.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. use Psr\Http\Message\ServerRequestInterface as Request;
  3. use React\Http\Message\Response;
  4. function init()
  5. {
  6. global $world, $fortune, $update;
  7. $pdo = new PDO(
  8. 'mysql:host=tfb-database;dbname=hello_world',
  9. 'benchmarkdbuser',
  10. 'benchmarkdbpass',
  11. [
  12. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  13. PDO::ATTR_EMULATE_PREPARES => false
  14. ]
  15. );
  16. $world = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
  17. $update = $pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
  18. $fortune = $pdo->prepare('SELECT id,message FROM Fortune');
  19. $fortune->setFetchMode(PDO::FETCH_KEY_PAIR);
  20. }
  21. function router(Request $request)
  22. {
  23. return match($request->getUri()->getPath()) {
  24. '/plaintext' => text(),
  25. '/json' => json(),
  26. '/db' => db(),
  27. '/fortunes' => fortune(),
  28. '/query' => query($request),
  29. '/update' => updateraw($request),
  30. // '/info' => info(),
  31. default => new Response(404, [], 'Error 404'),
  32. };
  33. }
  34. function text()
  35. {
  36. return new Response(200, [
  37. 'Content-Type' => 'text/plain'
  38. ], 'Hello, World!');
  39. }
  40. function json()
  41. {
  42. return new Response(200, [
  43. 'Content-Type' => 'application/json'
  44. ], json_encode(['message' => 'Hello, World!']));
  45. }
  46. function db()
  47. {
  48. global $world;
  49. $world->execute([mt_rand(1, 10000)]);
  50. return new Response(200, [
  51. 'Content-Type' => 'application/json'
  52. ], json_encode($world->fetch()));
  53. }
  54. function query($request)
  55. {
  56. global $world;
  57. $query_count = 1;
  58. $q = (int) $request->getQueryParams()['q'];
  59. if ($q > 1) {
  60. $query_count = min($q, 500);
  61. }
  62. while ($query_count--) {
  63. $world->execute([mt_rand(1, 10000)]);
  64. $arr[] = $world->fetch();
  65. }
  66. return new Response(200, [
  67. 'Content-Type' => 'application/json'
  68. ], json_encode($arr));
  69. }
  70. function updateraw($request)
  71. {
  72. global $world, $update;
  73. $query_count = 1;
  74. $q = (int) $request->getQueryParams()['q'];
  75. if ($q > 1) {
  76. $query_count = min($q, 500);
  77. }
  78. while ($query_count--) {
  79. $id = mt_rand(1, 10000);
  80. $world->execute([$id]);
  81. $item = $world->fetch();
  82. $update->execute(
  83. [$item['randomNumber'] = mt_rand(1, 10000), $id]
  84. );
  85. $arr[] = $item;
  86. }
  87. // $pdo->beginTransaction();
  88. // foreach($arr as $world) {
  89. // $update->execute([$world['randomNumber'], $world['id']]);
  90. // }
  91. // $pdo->commit();
  92. return new Response(200, [
  93. 'Content-Type' => 'application/json'
  94. ], json_encode($arr));
  95. }
  96. function fortune()
  97. {
  98. global $fortune;
  99. $fortune->execute();
  100. $arr = $fortune->fetchAll();
  101. $arr[0] = 'Additional fortune added at request time.';
  102. asort($arr);
  103. $html = '';
  104. foreach ($arr as $id => $message) {
  105. $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
  106. $html .= "<tr><td>$id</td><td>$message</td></tr>";
  107. }
  108. return new Response(200, [
  109. 'Content-Type' => 'text/html; charset=UTF-8',
  110. ], "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>"
  111. );
  112. }
  113. /* function info()
  114. {
  115. ob_start();
  116. phpinfo();
  117. return new Response(200, ['Content-Type' => 'text/plain'], ob_get_clean());
  118. }
  119. */