index.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. dispatch("/updates/:queries", "updates");
  47. function updates() {
  48. header('Content-Type: application/json; charset=utf-8');
  49. $query_count = params("queries");
  50. if ($query_count < 1) {
  51. $query_count = 1;
  52. }
  53. if ($query_count > 500) {
  54. $query_count = 500;
  55. }
  56. $worlds = array();
  57. for ($i = 0; $i < $query_count; $i++) {
  58. $id = mt_rand(1, 10000);
  59. $world = World::find($id);
  60. $world->randomnumber = mt_rand(1, 10000);
  61. $world->save();
  62. $worlds[] = $world->to_array();
  63. }
  64. return json($worlds);
  65. }
  66. function fortune_to_array(&$fortune, $key) {
  67. $fortune->message = htmlspecialchars($fortune->message);
  68. $fortune = $fortune->to_array();
  69. }
  70. function compare_fortunes($f1, $f2) {
  71. return strcmp($f1["message"], $f2["message"]);
  72. }
  73. dispatch("/fortune", "fortune");
  74. function fortune() {
  75. header('Content-Type: text/html; charset=utf-8');
  76. $fortunes = Fortune::all();
  77. array_walk($fortunes, "fortune_to_array");
  78. $fortunes[] = array("id"=>0, "message"=> "Additional fortune added at request time.");
  79. usort($fortunes, "compare_fortunes");
  80. return render("fortune.php", null, array("fortunes" => $fortunes));
  81. }
  82. run();
  83. ?>