index.php 3.4 KB

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