app.php 3.3 KB

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