dborm.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. $query_param = isset($_GET['queries']);
  19. if ($query_param && $_GET['queries'] > 0) {
  20. $query_count = $_GET['queries'] > 500 ? 500 : $_GET['queries'];
  21. }
  22. // Create an array with the response string.
  23. $arr = array();
  24. // For each query, store the result set values in the response array
  25. $query_counter = $query_count;
  26. while (0 < $query_counter--) {
  27. // Choose a random row
  28. // http://www.php.net/mt_rand
  29. $id = mt_rand(1, 10000);
  30. $world = World::find_by_id($id);
  31. // Store result in array.
  32. $arr[] = $world->to_array();
  33. }
  34. if ($query_count === 1 && !$query_param) {
  35. $arr = $arr[0];
  36. }
  37. // Use the PHP standard JSON encoder.
  38. // http://www.php.net/manual/en/function.json-encode.php
  39. echo json_encode($arr);