You are here

function drush_xhprof_combine in XHProf 8

Same name and namespace in other branches
  1. 6 xhprof.drush.inc \drush_xhprof_combine()
  2. 7 xhprof.drush.inc \drush_xhprof_combine()

A command callback.

File

./xhprof.drush.inc, line 29

Code

function drush_xhprof_combine() {
  $run_ids = func_get_args();
  $runs = drush_xhprof_get_runs();
  if (empty($run_ids)) {
    $options = array_keys($runs);
    $options[] = 'All';

    // return dt("You must provide a run id argument!");

    //$choice = drush_choice_multiple($options, FALSE, 'Select xhprof runs to combine.');
    $choice = drush_choice($options, 'Select xhprof runs to combine.');
    drush_print_r($options[$choice]);
    if ($choice !== FALSE) {
      if ($options[$choice] == 'All') {
        $run_ids = array_keys($runs);
      }
      else {

        // TODO: This doesn't work. Probably going to have to use drush_choice_multiple.
        $ids = explode(",", $choice);
        foreach ($ids as $id) {
          $run_ids[] = $options[$id];
        }
      }
    }
  }
  $xhprof = new XHProfRuns_Default();
  $runs = drush_xhprof_get_runs();
  $run_data = [];
  $keys = [];
  $desc = "";
  foreach ($run_ids as $run_id) {
    if (isset($runs[$run_id])) {
      $run = $runs[$run_id];
      if ($data = $xhprof
        ->get_run($run['run_id'], $run['source'], $desc)) {
        $run_data[] = $data;
        $keys = $keys + array_keys($data);
      }
    }
  }
  $agg_run = [];
  $run_count = count($run_data);
  foreach ($keys as $key) {
    $agg_key = [];

    // Check which runs have this parent_child function key, collect metrics if so.
    foreach ($run_data as $data) {
      if (isset($data[$key])) {
        foreach ($data[$key] as $metric => $val) {
          $agg_key[$metric][] = $val;
        }
      }
    }

    // Average each metric for the key into the aggregated run.
    $agg_run[$key] = [];
    foreach ($agg_key as $metric => $vals) {
      $agg_run[$key][$metric] = array_sum($agg_key[$metric]) / count($agg_key[$metric]);
    }
  }
  $namespace = \Drupal::config('system.site')
    ->get('name');
  $namespace = 'drush-' . str_replace([
    '.',
    '/',
    '\\',
  ], '-', $namespace);
  \Drupal::moduleHandler()
    ->alter('xhprof_namespace', $namespace);
  if ($agg_run_id = $xhprof
    ->save_run($agg_run, $namespace)) {
    drush_print(dt("Aggregated run id: !id", [
      '!id' => $agg_run_id,
    ]));
  }
  else {
    drush_print(dt("Unable to save aggregated xhprof data!"));
  }
}