dborm.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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\Connection::$PDO_OPTIONS[PDO::ATTR_PERSISTENT] = true;
  10. ActiveRecord\Config::initialize(function($cfg)
  11. {
  12. $cfg->set_model_directory('models');
  13. $cfg->set_connections(array('development' =>
  14. 'mysql://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world'));
  15. });
  16. // Read number of queries to run from URL parameter
  17. $query_count = 1;
  18. if (isset($_GET['queries']) && $_GET['queries'] > 0) {
  19. $query_count = $_GET["queries"] > 500 ? 500 : $_GET['queries'];
  20. }
  21. // Create an array with the response string.
  22. $arr = array();
  23. // For each query, store the result set values in the response array
  24. for ($i = 0; $i < $query_count; $i++) {
  25. // Choose a random row
  26. // http://www.php.net/mt_rand
  27. $id = mt_rand(1, 10000);
  28. $world = World::find_by_id($id);
  29. // Store result in array.
  30. $arr[] = $world->to_array();
  31. }
  32. if ($query_count === 1) {
  33. $arr = $arr[0];
  34. }
  35. // Use the PHP standard JSON encoder.
  36. // http://www.php.net/manual/en/function.json-encode.php
  37. echo json_encode($arr);