index.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Step 1: Require the Slim Framework
  4. *
  5. * If you are not using Composer, you need to require the
  6. * Slim Framework and register its PSR-0 autoloader.
  7. *
  8. * If you are using Composer, you can skip this step.
  9. */
  10. require 'Slim/Slim.php';
  11. require 'Slim/RedBean/rb.php';
  12. \Slim\Slim::registerAutoloader();
  13. # Turn off 'beautiful' column names (converting tables names from camelCase to snake_case).
  14. RedBean_OODBBean::setFlagBeautifulColumnNames(false);
  15. R::setup('mysql:host=127.0.0.1;dbname=hello_world','benchmarkdbuser','benchmarkdbpass');
  16. R::freeze(true);
  17. /**
  18. * Step 2: Instantiate a Slim application
  19. *
  20. * This example instantiates a Slim application using
  21. * its default settings. However, you will usually configure
  22. * your Slim application now by passing an associative array
  23. * of setting names and values into the application constructor.
  24. */
  25. $app = new \Slim\Slim();
  26. /**
  27. * Step 3: Define the Slim application routes
  28. *
  29. * Here we define several Slim application routes that respond
  30. * to appropriate HTTP request methods. In this example, the second
  31. * argument for `Slim::get`, `Slim::post`, `Slim::put`, and `Slim::delete`
  32. * is an anonymous function.
  33. */
  34. $app->get('/json', function () use($app) {
  35. $app->contentType('application/json');
  36. echo json_encode(array('message' => 'Hello, World!'));
  37. });
  38. $app->get('/db', function () use($app) {
  39. $world = R::load('World', mt_rand(1, 10000))->export();
  40. # Cast fields to int so they don't get wrapped with quotes
  41. $world['id'] = (int) $world['id'];
  42. $world['randomNumber'] = (int) $world['randomNumber'];
  43. $app->contentType('application/json');
  44. echo json_encode($world);
  45. });
  46. $app->get('/dbs', function () use($app) {
  47. $queries = ($app->request()->get('queries') !== null)
  48. ? $app->request()->get('queries')
  49. : 1;
  50. if ($queries < 1) {
  51. $queries = 1;
  52. }
  53. else if ($queries > 500) {
  54. $queries = 500;
  55. }
  56. $worlds = array();
  57. for ($i = 0; $i < $queries; ++$i) {
  58. $world = R::load('World', mt_rand(1, 10000))->export();
  59. # Cast fields to int so they don't get wrapped with quotes
  60. $world['id'] = (int) $world['id'];
  61. $world['randomNumber'] = (int) $world['randomNumber'];
  62. $worlds[] = $world;
  63. }
  64. $app->contentType('application/json');
  65. echo json_encode($worlds);
  66. });
  67. /**
  68. * Step 4: Run the Slim application
  69. *
  70. * This method should be called last. This executes the Slim application
  71. * and returns the HTTP response to the HTTP client.
  72. */
  73. $app->run();