index.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /** @var Base $f3 */
  3. $f3=require('lib/base.php');
  4. $f3->set('DEBUG',0);
  5. $f3->set('UI','ui/');
  6. // lazy initialized DB object
  7. $f3->set('DB',function() {
  8. return new \DB\SQL('mysql:host=localhost;port=3306;dbname=hello_world',
  9. 'benchmarkdbuser', 'benchmarkdbpass');
  10. });
  11. // https://github.com/TechEmpower/FrameworkBenchmarks#json-response
  12. $f3->route('GET /json',function($f3) {
  13. /** @var Base $f3 */
  14. header("Content-type: application/json");
  15. return json_encode(array('message' => 'Hello World!'));
  16. });
  17. // https://github.com/TechEmpower/FrameworkBenchmarks#database-single-query
  18. // https://github.com/TechEmpower/FrameworkBenchmarks#database-multiple-queries
  19. $f3->route(
  20. array(
  21. 'GET /db',
  22. 'GET /db/@queries',
  23. ),
  24. function ($f3,$params) {
  25. /** @var Base $f3 */
  26. $params += array('queries' => 1); //default value
  27. $dbc = $f3->get('DB');
  28. $db = $dbc();
  29. $result = array();
  30. for ($i = 0; $i < $params['queries']; ++$i) {
  31. $id = mt_rand(1, 10000);
  32. $result[] = $db->exec('SELECT randomNumber FROM World WHERE id = ?',$id,0,false);
  33. }
  34. header("Content-type: application/json");
  35. return json_encode($result);
  36. }
  37. );
  38. // https://github.com/TechEmpower/FrameworkBenchmarks#database-single-query
  39. // https://github.com/TechEmpower/FrameworkBenchmarks#database-multiple-queries
  40. $f3->route(
  41. array(
  42. 'GET /db-orm',
  43. 'GET /db-orm/@queries',
  44. ),
  45. function ($f3, $params) {
  46. /** @var Base $f3 */
  47. $params += array('queries' => 1); //default value
  48. $dbc = $f3->get('DB');
  49. $db = $dbc();
  50. $mapper = new \DB\SQL\Mapper($db,'World');
  51. $result = array();
  52. for ($i = 0; $i < $params['queries']; ++$i) {
  53. $id = mt_rand(1, 10000);
  54. $mapper->load(array('where id = ?',$id));
  55. $result[] = $mapper->cast();
  56. }
  57. header("Content-type: application/json");
  58. return json_encode($result);
  59. }
  60. );
  61. $f3->route('GET /plaintext', function ($f3) {
  62. echo "Hello, World!";
  63. });
  64. $f3->route('GET /fortune', function ($f3) {
  65. /** @var Base $f3 */
  66. $dbc = $f3->get('DB');
  67. $db = $dbc();
  68. $result = $db->exec('SELECT id, message FROM Fortune');
  69. $result[] = 'Additional fortune added at request time.';
  70. asort($result);
  71. $f3->set('result',$result);
  72. echo \Template::instance()->render('fortune.html');
  73. });
  74. $f3->route(
  75. array(
  76. 'GET /updateraw',
  77. 'GET /updateraw/@queries',
  78. ),function($f3,$params) {
  79. /** @var Base $f3 */
  80. $params += array('queries' => 1); //default value
  81. $dbc = $f3->get('DB');
  82. $db = $dbc();
  83. $result = array();
  84. for ($i = 0; $i < $params['queries']; ++$i) {
  85. $id = mt_rand(1, 10000);
  86. $row = array(
  87. 'id'=>$id,
  88. 'randomNumber'=>$db->exec('SELECT randomNumber FROM World WHERE id = ?',$id,0,false)
  89. );
  90. $rnu = mt_rand(1, 10000);
  91. $row['randomNumber'] = $rnu;
  92. $db->exec('UPDATE World SET randomNumber = :ranNum WHERE id = :id', array(':ranNum'=>$rnu,':id'=>$id),0,false);
  93. $result[] = $row;
  94. }
  95. header("Content-type: application/json");
  96. return json_encode($result);
  97. });
  98. $f3->run();