swoole-server.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. $server = new swoole_http_server('0.0.0.0', 8080, SWOOLE_BASE);
  3. $server->set(array(
  4. 'worker_num' => NUMCORES
  5. ));
  6. $server->on('request', function ($req, $res) {
  7. switch ($req->server['request_uri'])
  8. {
  9. case "/json":
  10. $res->header('Content-type', 'application/json');
  11. $res->end(json_encode(array('message' => 'Hello, World!')));
  12. break;
  13. case "/plaintext":
  14. $res->header('Content-Type', 'text/plain');
  15. $res->end('Hello, World!');
  16. break;
  17. case "/db":
  18. $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
  19. PDO::ATTR_PERSISTENT => true
  20. ));
  21. // Read number of queries to run from URL parameter
  22. $query_count = 1;
  23. if (isset($req->get['queries']) && $req->get['queries'] > 0)
  24. $query_count = $req->get['queries'] > 500 ? 500 : $req->get['queries'];
  25. // Create an array with the response string.
  26. $arr = array();
  27. // Define query
  28. $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = ?');
  29. // For each query, store the result set values in the response array
  30. while (0 < $query_count--)
  31. {
  32. $id = mt_rand(1, 10000);
  33. $statement->execute(array($id));
  34. // Store result in array.
  35. $arr[] = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
  36. }
  37. // Use the PHP standard JSON encoder.
  38. // http://www.php.net/manual/en/function.json-encode.php
  39. if (count($arr) === 1)
  40. $arr = $arr[0];
  41. $res->header('Content-Type', 'application/json');
  42. $res->end(json_encode($arr));
  43. break;
  44. case "/fortunes":
  45. $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world;charset=utf8', 'benchmarkdbuser', 'benchmarkdbpass', array(
  46. PDO::ATTR_PERSISTENT => true
  47. ));
  48. // Define query
  49. $statement = $pdo->query('SELECT id, message FROM Fortune');
  50. // Store result in array.
  51. $arr = $statement->fetchAll(PDO::FETCH_KEY_PAIR);
  52. $arr[0] = 'Additional fortune added at request time.';
  53. asort($arr);
  54. $html = "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
  55. foreach ($arr as $id => $fortune)
  56. $html .= "<tr><td>" . $id . "</td><td>" . htmlspecialchars($fortune, ENT_QUOTES, 'UTF-8') . "</td></tr>";
  57. $html .= "</table></body></html>";
  58. $res->header('Content-Type', 'text/html; charset=utf-8');
  59. $res->end($html);
  60. break;
  61. case "/updates":
  62. $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
  63. PDO::ATTR_PERSISTENT => true
  64. ));
  65. $query_count = 1;
  66. if (isset($req->get['queries']) && $req->get['queries'] > 0)
  67. {
  68. $query_count = $req->get['queries'] > 500 ? 500 : $req->get['queries'];
  69. }
  70. $arr = array();
  71. $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = ?');
  72. $updateStatement = $pdo->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
  73. while (0 < $query_count--)
  74. {
  75. $id = mt_rand(1, 10000);
  76. $randomNumber = mt_rand(1, 10000);
  77. $statement->execute(array($id));
  78. // Store result in array.
  79. $world = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
  80. $world['randomNumber'] = $randomNumber;
  81. $updateStatement->execute(array($randomNumber, $id));
  82. $arr[] = $world;
  83. }
  84. $res->header('Content-Type', 'application/json');
  85. $res->end(json_encode($arr));
  86. break;
  87. }
  88. });
  89. $server->start();