index-micro.php 2.9 KB

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