app.php 3.7 KB

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