dborm.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. // Set content type
  3. header("Content-type: application/json");
  4. // Database connection
  5. // http://www.php.net/manual/en/ref.pdo-mysql.php
  6. // $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass');
  7. # inclue the ActiveRecord library
  8. require_once 'vendor/php-activerecord/php-activerecord/ActiveRecord.php';
  9. ActiveRecord\Config::initialize(function($cfg)
  10. {
  11. $cfg->set_model_directory('models');
  12. $cfg->set_connections(array('development' =>
  13. 'mysql://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world'));
  14. });
  15. // Read number of queries to run from URL parameter
  16. $query_count = 1;
  17. if (isset($_GET['queries']) && $_GET['queries'] > 0) {
  18. $query_count = $_GET["queries"] > 500 ? 500 : $_GET['queries'];
  19. }
  20. // Create an array with the response string.
  21. $arr = array();
  22. // For each query, store the result set values in the response array
  23. for ($i = 0; $i < $query_count; $i++) {
  24. // Choose a random row
  25. // http://www.php.net/mt_rand
  26. $id = mt_rand(1, 10000);
  27. $world = World::find_by_id($id);
  28. // Store result in array.
  29. $arr[] = $world->to_array();
  30. }
  31. if ($query_count === 1) {
  32. $arr = $arr[0];
  33. }
  34. // Use the PHP standard JSON encoder.
  35. // http://www.php.net/manual/en/function.json-encode.php
  36. echo json_encode($arr);