Matt Borja Tue Nov 13 12:54:28 -0500 2012

Subject: SQL Benchmarks

UPDATE: See http://stackoverflow.com/questions/13366155/sql-benchmarks-php-activerecord-orm-vs-mysql-vs-codeigniter-active-record-vs for some profiling of this library which reveals my findings.

The test query is SELECT * FROM school_courses;

Can anyone provide "outside-the-box-thinking" feedback as to:

a) Why PHP ActiveRecord ORM takes 4 seconds to perform the same query per the below results?
b) Whether this is a practical benchmark or more of a hypothetical one for comparing methods of querying?
c) Are there other methods (test cases) I should try (or revise these ones) to get a clearer picture?

================

Benchmark Results:

Iterations: 100

PHP (config file)
Base Time: 5.793571472168E-5
Gross Time: 0.055607080459595
Net Time: 0.055549144744873

PHP ActiveRecord ORM
Base Time: 5.2213668823242E-5
Gross Time: 4.1013090610504
Net Time: 4.1012568473816

MySQL (standard)
Base Time: 5.1975250244141E-5
Gross Time: 0.32771301269531
Net Time: 0.32766103744507

CodeIgniter (ActiveRecord)
Base Time: 5.1975250244141E-5
Gross Time: 0.28282189369202
Net Time: 0.28276991844177

================

// Benchmark test
set_time_limit(0);
$runs = 100;

echo 'Iterations: '.$runs."\r\n";

echo 'PHP (config file)';
$start = microtime(TRUE); for ($i = 0; $i < $runs; $i++) {} $end = microtime(TRUE);
$base_time = $end - $start;
echo 'Base Time: ' . $base_time . "\r\n";

$start = microtime(TRUE);
for ($i = 0; $i < $runs; $i++) {
$this->view_data['courses'] = course_info();
}
$end = microtime(TRUE);

$gross_time = $end - $start;
$net_time = $gross_time - $base_time;
echo 'Gross Time: ' . $gross_time . "\r\n";
echo 'Net Time: ' . $net_time . "\r\n";
// End of Benchmark test

// Benchmark test
echo 'MySQL (phpactiverecord)';
$start = microtime(TRUE); for ($i = 0; $i < $runs; $i++) {} $end = microtime(TRUE);
$base_time = $end - $start;
echo 'Base Time: ' . $base_time . "\r\n";

$start = microtime(TRUE);
for ($i = 0; $i < $runs; $i++) {
$this->view_data['courses'] = Course::all();
}
$end = microtime(TRUE);

$gross_time = $end - $start;
$net_time = $gross_time - $base_time;
echo 'Gross Time: ' . $gross_time . "\r\n";
echo 'Net Time: ' . $net_time . "\r\n";
// End of Benchmark test

// Benchmark test
echo 'MySQL (standard)';
$start = microtime(TRUE); for ($i = 0; $i < $runs; $i++) {} $end = microtime(TRUE);
$base_time = $end - $start;
echo 'Base Time: ' . $base_time . "\r\n";

$start = microtime(TRUE);
$this->load->database('default');
mysql_connect($this->db->hostname, $this->db->username, $this->db->password) or die(mysql_error());
mysql_select_db('school');
for ($i = 0; $i < $runs; $i++) {
$sql = mysql_query('SELECT * FROM school_courses') or die(mysql_error());
while ($row = mysql_fetch_object($sql)) {
array_push($this->view_data['courses'], $row);
}
}
$end = microtime(TRUE);

$gross_time = $end - $start;
$net_time = $gross_time - $base_time;
echo 'Gross Time: ' . $gross_time . "\r\n";
echo 'Net Time: ' . $net_time . "\r\n";
// End of Benchmark test

// Benchmark test
echo 'CodeIgniter (ActiveRecord)';
$start = microtime(TRUE); for ($i = 0; $i < $runs; $i++) {} $end = microtime(TRUE);
$base_time = $end - $start;
echo 'Base Time: ' . $base_time . "\r\n";

$start = microtime(TRUE);
for ($i = 0; $i < $runs; $i++) {
$this->view_data['courses'] = $this->db->get('school_courses');
}
$end = microtime(TRUE);

$gross_time = $end - $start;
$net_time = $gross_time - $base_time;
echo 'Gross Time: ' . $gross_time . "\r\n";
echo 'Net Time: ' . $net_time . "\r\n";
// End of Benchmark test

exit();