app.php 3.5 KB

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