index-micro.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. use Phalcon\Db\Adapter\Pdo\Mysql;
  3. use Phalcon\Db\Enum;
  4. use Phalcon\Mvc\Micro;
  5. use Phalcon\Mvc\View;
  6. use Phalcon\Mvc\View\Engine\Volt;
  7. use Phalcon\Exception as PhalconException;
  8. try {
  9. $app = new Micro();
  10. // Setting up the database connection
  11. $app['db'] = function () {
  12. return new Mysql([
  13. 'host' => 'tfb-database',
  14. 'dbname' => 'hello_world',
  15. 'username' => 'benchmarkdbuser',
  16. 'password' => 'benchmarkdbpass',
  17. 'options' => [
  18. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
  19. PDO::ATTR_PERSISTENT => true,
  20. ],
  21. ]);
  22. };
  23. // Setting up the view component (seems to be required even when not used)
  24. $app['view'] = function () {
  25. $view = new View();
  26. $view->setViewsDir(__DIR__ . '/../app/views/');
  27. $view->registerEngines([
  28. ".volt" => function ($view) {
  29. $volt = new Volt($view);
  30. $volt->setOptions([
  31. "path" => __DIR__ . "/../app/compiled-templates/",
  32. "extension" => ".c",
  33. "separator" => '_',
  34. ]);
  35. return $volt;
  36. }
  37. ]);
  38. return $view;
  39. };
  40. /**
  41. * Routes
  42. */
  43. $app->map('/plaintext', function () {
  44. header("Content-Type: text/plain; charset=UTF-8");
  45. echo "Hello, World!";
  46. });
  47. $app->map('/json', function () {
  48. header("Content-Type: application/json");
  49. echo json_encode(['message' => 'Hello, World!']);
  50. });
  51. $app->map('/db', function () use ($app) {
  52. $db = $app['db'];
  53. $world = $db->fetchOne('SELECT * FROM world WHERE id = ' . mt_rand(1, 10000), Enum::FETCH_ASSOC);
  54. header("Content-Type: application/json");
  55. echo json_encode($world);
  56. });
  57. $app->map('/queries', function () use ($app) {
  58. $db = $app['db'];
  59. $queries = $app->request->getQuery('queries', "int", 1);
  60. $queries = min(max(intval($queries), 1), 500);
  61. $worlds = [];
  62. for ($i = 0; $i < $queries; ++$i) {
  63. $worlds[] = $db->fetchOne('SELECT * FROM world WHERE id = ' . mt_rand(1, 10000), Enum::FETCH_ASSOC);
  64. }
  65. header("Content-Type: application/json");
  66. echo json_encode($worlds);
  67. });
  68. $app->map('/fortunes', function () use ($app) {
  69. $fortunes = $app['db']->query('SELECT * FROM fortune')->fetchAll();
  70. $fortunes[] = [
  71. 'id' => 0,
  72. 'message' => 'Additional fortune added at request time.'
  73. ];
  74. usort($fortunes, function ($left, $right) {
  75. return $left['message'] <=> $right['message'];
  76. });
  77. header("Content-Type: text/html; charset=utf-8");
  78. echo $app['view']->getRender('bench', 'fortunes', [
  79. 'fortunes' => $fortunes,
  80. ]);
  81. });
  82. $url = $_REQUEST['_url'] ?? '/';
  83. $app->handle($url);
  84. } catch (PhalconException $e) {
  85. echo "PhalconException: ", $e->getMessage();
  86. }