index.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. require_once "vendor/sofadesign/limonade/lib/limonade.php";
  3. require_once "vendor/php-activerecord/php-activerecord/ActiveRecord.php";
  4. function configure() {
  5. ActiveRecord\Connection::$PDO_OPTIONS[PDO::ATTR_PERSISTENT] = true;
  6. $cfg = ActiveRecord\Config::instance();
  7. $cfg->set_model_directory("models");
  8. $cfg->set_connections(array(
  9. "development" => "mysql://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world?charset=utf8"));
  10. option("bas_url", "/");
  11. }
  12. dispatch("/plaintext", "plaintext");
  13. function plaintext() {
  14. header('Content-Type: text/plain; charset=utf-8');
  15. return txt("Hello, World!");
  16. }
  17. dispatch("/json", "jsonHandler");
  18. function jsonHandler() {
  19. header('Content-Type: application/json; charset=utf-8');
  20. $arr = array("message" => "Hello, World!");
  21. return json($arr);
  22. }
  23. dispatch("/db", "db");
  24. function db() {
  25. $id = mt_rand(1, 10000);
  26. $test = World::find($id);
  27. return json($test->to_array());
  28. }
  29. dispatch("/queries/:queries", "queries");
  30. function queries() {
  31. header('Content-Type: application/json; charset=utf-8');
  32. $query_count = params("queries");
  33. if ($query_count < 1) {
  34. $query_count = 1;
  35. }
  36. if ($query_count > 500) {
  37. $query_count = 500;
  38. }
  39. $worlds = array();
  40. for ($i = 0; $i < $query_count; $i++) {
  41. $id = mt_rand(1, 10000);
  42. $world = World::find($id);
  43. $worlds[] = $world->to_array();
  44. }
  45. return json($worlds);
  46. }
  47. dispatch("/updates/:queries", "updates");
  48. function updates() {
  49. header('Content-Type: application/json; charset=utf-8');
  50. $query_count = params("queries");
  51. if ($query_count < 1) {
  52. $query_count = 1;
  53. }
  54. if ($query_count > 500) {
  55. $query_count = 500;
  56. }
  57. $worlds = array();
  58. for ($i = 0; $i < $query_count; $i++) {
  59. $id = mt_rand(1, 10000);
  60. $world = World::find($id);
  61. $world->randomnumber = mt_rand(1, 10000);
  62. $world->save();
  63. $worlds[] = $world->to_array();
  64. }
  65. return json($worlds);
  66. }
  67. function fortune_to_array(&$fortune, $key) {
  68. $fortune->message = htmlspecialchars($fortune->message);
  69. $fortune = $fortune->to_array();
  70. }
  71. function compare_fortunes($f1, $f2) {
  72. return strcmp($f1["message"], $f2["message"]);
  73. }
  74. dispatch("/fortune", "fortune");
  75. function fortune() {
  76. header('Content-Type: text/html; charset=utf-8');
  77. $fortunes = Fortune::all();
  78. array_walk($fortunes, "fortune_to_array");
  79. $fortunes[] = array("id"=>0, "message"=> "Additional fortune added at request time.");
  80. usort($fortunes, "compare_fortunes");
  81. return render("fortune.php", null, array("fortunes" => $fortunes));
  82. }
  83. run();
  84. ?>