swoole-server-noasync.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. use Swoole\Http\Request;
  3. use Swoole\Http\Response;
  4. $server = new swoole_http_server('0.0.0.0', 8080, SWOOLE_BASE);
  5. $server->set([
  6. 'worker_num' => NUMCORES
  7. ]);
  8. $pdo = new PDO("mysql:host=tfb-database;dbname=hello_world", "benchmarkdbuser", "benchmarkdbpass", [
  9. PDO::ATTR_PERSISTENT => true
  10. ]);
  11. /**
  12. * The DB test
  13. *
  14. * @param int $queries
  15. *
  16. * @return string
  17. */
  18. $db = function (int $queries = 1) use ($pdo): string {
  19. if ( $queries === -1) {
  20. $statement = $pdo->prepare("SELECT id,randomNumber FROM World WHERE id=?");
  21. $statement->execute([mt_rand(1, 10000)]);
  22. return json_encode($statement->fetch(PDO::FETCH_ASSOC), JSON_NUMERIC_CHECK);
  23. }
  24. // Read number of queries to run from URL parameter
  25. $query_count = 1;
  26. if ($queries > 1) {
  27. $query_count = $queries > 500 ? 500 : $queries;
  28. }
  29. // Create an array with the response string.
  30. $arr = [];
  31. // Define query
  32. $db = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id = ?');
  33. // For each query, store the result set values in the response array
  34. while ($query_count--) {
  35. $db->execute([mt_rand(1, 10000)]);
  36. $arr[] = $db->fetch(PDO::FETCH_ASSOC);
  37. }
  38. // Use the PHP standard JSON encoder.
  39. // http://www.php.net/manual/en/function.json-encode.php
  40. return json_encode($arr, JSON_NUMERIC_CHECK);
  41. };
  42. /**
  43. * The Fortunes test
  44. *
  45. * @return string
  46. */
  47. $fortunes = function () use ($pdo): string {
  48. $fortune = [];
  49. $db = $pdo->prepare('SELECT id, message FROM Fortune');
  50. $db->execute();
  51. $fortune = $db->fetchAll(PDO::FETCH_KEY_PAIR);
  52. $fortune[0] = 'Additional fortune added at request time.';
  53. asort($fortune);
  54. $html = '';
  55. foreach ($fortune as $id => $message) {
  56. $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
  57. $html .= "<tr><td>$id</td><td>$message</td></tr>";
  58. }
  59. return '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>'.
  60. $html.
  61. '</table></body></html>';
  62. };
  63. /**
  64. * The Updates test
  65. *
  66. * @param int $queries
  67. *
  68. * @return string
  69. */
  70. $updates = function (int $queries) use ($pdo): string {
  71. $query_count = 1;
  72. if ($queries > 1) {
  73. $query_count = $queries > 500 ? 500 : $queries;
  74. }
  75. $statement = $pdo->prepare("SELECT randomNumber FROM World WHERE id=?");
  76. $updateStatement = $pdo->prepare("UPDATE World SET randomNumber=? WHERE id=?");
  77. while ($query_count--) {
  78. $id = mt_rand(1, 10000);
  79. $statement->execute([$id]);
  80. $world = ["id" => $id, "randomNumber" => $statement->fetchColumn()];
  81. $updateStatement->execute(
  82. [$world["randomNumber"] = mt_rand(1, 10000), $id]
  83. );
  84. $arr[] = $world;
  85. }
  86. return json_encode($arr, JSON_NUMERIC_CHECK);
  87. };
  88. /**
  89. * On start of the PHP worker. One worker per server process is started.
  90. */
  91. //$server->on('workerStart', function () use ($pool) {
  92. //});
  93. /**
  94. * On every request to the (web)server, execute the following code
  95. */
  96. $server->on('request', function (Request $req, Response $res) use ($db, $fortunes, $updates) {
  97. switch ($req->server['request_uri']) {
  98. case '/json':
  99. $res->header('Content-Type', 'application/json');
  100. $res->end(json_encode(['message' => 'Hello, World!']));
  101. break;
  102. case '/plaintext':
  103. $res->header('Content-Type', 'text/plain; charset=utf-8');
  104. $res->end('Hello, World!');
  105. break;
  106. case '/db':
  107. $res->header('Content-Type', 'application/json');
  108. if (isset($req->get['queries'])) {
  109. $res->end($db((int) $req->get['queries']));
  110. } else {
  111. $res->end($db(-1));
  112. }
  113. break;
  114. case '/fortunes':
  115. $res->header('Content-Type', 'text/html; charset=utf-8');
  116. $res->end($fortunes());
  117. break;
  118. case '/updates':
  119. $res->header('Content-Type', 'application/json');
  120. $res->end($updates((int) $req->get['queries'] ?? 1));
  121. break;
  122. }
  123. });
  124. $server->start();