
function benchmark_functions($function_a, $params_a, $function_b, $params_b, $iterations) {
  $results = array();
  $total_time = array(
    'a' => 0,
    'b' => 0,
  );

  benchmark_query_cache(FALSE);

  for ($i = 1; $i <= $iterations; $i++) {
    timer_start($function_a);
    call_user_func_array($function_a, $params_a);
    $result = timer_stop($function_a);
    $results['a'][$i] = $result['time'] - $total_time['a'];
    $total_time['a'] += $results['a'][$i];

    timer_start($function_b);
    call_user_func_array($function_b, $params_b);
    $result = timer_stop($function_b);
    $results['b'][$i] = $result['time'] - $total_time['b'];
    $total_time['b'] += $results['b'][$i];
  }

  benchmark_query_cache(TRUE);

  echo "function $function_a($params_a[1])      <br />\n";
  echo "$total_time[a]       <br />\n";
  echo 'avg: '. $total_time['a'] / $iterations ."       <br />\n";
  echo 'min: '. min($results['a']) ."       <br />\n";
  echo 'max: '. max($results['a']) ."       <br />\n";
  echo " <br />\n";
//  echo 'variance: '. stats_variance($results[$function_a], TRUE) .' <br />';

  echo "function $function_b($params_b[1])      <br />\n";
  echo "$total_time[b]       <br />\n";
  echo 'avg: '. $total_time['b'] / $iterations ."       <br />\n";
  echo 'min: '. min($results['b']) ."       <br />\n";
  echo 'max: '. max($results['b']) ."       <br />\n";
//  echo 'variance: '. stats_variance($results[$function_b], TRUE) .' <br />';
  echo '<br />';

  var_export($results);
}


function benchmark_email_simple_select($contacts, $table) {
  foreach ($contacts as $contact) {
    $wheres = array();
    foreach ($contact as $field => $value) {
      if (!is_int($value)) {
        $value = "'$value'";
      }
      $wheres[] = "$field = $value ";
    }
    db_query("SELECT contact_id, is_primary, email FROM $table WHERE ". implode(' AND ', $wheres));
  }
}

function benchmark_email_complex_select($contacts, $table) {
  foreach ($contacts as $contact) {
    $wheres = array();
    foreach ($contact as $field => $value) {
      if (!is_int($value)) {
        $value = "'$value'";
      }
      $wheres[] = "$field = $value ";
    }
    db_query("SELECT contact_id, first_name, last_name, is_primary, email
      FROM civicrm_contact
      INNER JOIN $table
        ON civicrm_contact.id = $table.contact_id AND $table.is_primary = 1
      WHERE ". implode(' AND ', $wheres));
  }
}

function benchmark_email_insert($contacts, $table) {
  foreach ($contacts as $contact) {
    $sets = array();
    foreach ($contact as $field => $value) {
      if (!is_int($value)) {
        $value = "'$value'";
      }
      $sets[] = " $field = $value ";
    }
    db_query("INSERT INTO $table SET ". implode(', ', $sets));
  }
}

function benchmark_delete($contacts, $table) {
  foreach ($contacts as $contact) {
    $wheres = array();
    foreach ($contact as $field => $value) {
      if (!is_int($value)) {
        $value = "'$value'";
      }
      $wheres[] = "$field = $value ";
    }
    db_query("DELETE FROM $table WHERE ". implode(' AND ', $wheres));
  }
}


function benchmark_query_cache($enable) {
  global $db_type;

  $old_db = db_set_active('civicrm');
  db_query('SET SESSION query_cache_type = %d', $enable);
  db_set_active();
  db_query('SET SESSION query_cache_type = %d', $enable);

  civicrm_initialize();
  require_once('CRM/Core/DAO.php');
  CRM_Core_DAO::executeQuery('SET SESSION query_cache_type = '. (int)$enable);

  if ($old_db && $old_db != 'default') {
    db_set_active($old_db);
  }

}

