index.php 2.4 KB

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