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