dborm.php 1.3 KB

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