-
Notifications
You must be signed in to change notification settings - Fork 1
/
xhprof.drush.inc
159 lines (138 loc) · 3.74 KB
/
xhprof.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* @file
*/
/**
* Implements hook_drush_command().
*/
function xhprof_drush_command() {
$items['xhprof-list'] = array(
'description' => dt(''),
'arguments' => array(
),
);
$items['xhprof-combine'] = array(
'description' => dt(''),
'arguments' => array(
),
);
$items['xhprof-clear'] = array(
'description' => dt(''),
'arguments' => array(
),
);
return $items;
}
/**
* A command callback.
*/
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 = array();
$keys = array();
$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 = array();
$run_count = count($run_data);
foreach ($keys as $key) {
$agg_key = array();
// 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] = array();
foreach ($agg_key as $metric => $vals) {
$agg_run[$key][$metric] = (array_sum($agg_key[$metric]) / count($agg_key[$metric]));
}
}
$namespace = 'drush-' . variable_get('site_name', '');
drupal_alter('xhprof_namespace', $namespace);
if ($agg_run_id = $xhprof->save_run($agg_run, $namespace)) {
drush_print(dt("Aggregated run id: !id", array('!id' => $agg_run_id)));
}
else {
drush_print(dt("Unable to save aggregated xhprof data!"));
}
}
/**
* A command callback.
*/
function drush_xhprof_list() {
$runs = drush_xhprof_get_runs();
$rows = array();
$headers = array();
foreach ($runs as $run) {
$row = array();
foreach ($run as $key => $value) {
if (!in_array($key, $headers)) $headers[] = $key;
$row[] = $value;
}
$rows[] = array_reverse($row);
}
$headers = array_reverse($headers);
array_unshift($rows, $headers);
drush_print_table($rows, TRUE);
}
/**
* A command callback.
*/
function drush_xhprof_clear() {
drush_xhprof_get_dir();
foreach (drush_xhprof_list_run_files() as $file) {
drush_register_file_for_deletion($file);
}
}
function drush_xhprof_get_dir() {
$dir = ini_get("xhprof.output_dir");
return (!empty($dir) && is_dir($dir)) ? $dir = "/tmp" : FALSE;
}
function drush_xhprof_list_run_files() {
$dir = drush_xhprof_get_dir();
$files = array();
foreach (glob("{$dir}/*.xhprof") as $file) $files[] = $file;
return $files;
}
function drush_xhprof_get_runs() {
$dir = drush_xhprof_get_dir();
$run_ids = array();
foreach (glob("{$dir}/*.xhprof") as $file) {
$run = array();
list($run['run_id'], $run['source']) = explode('.', basename($file));
$runs[$run['run_id']] = $run;
}
return $runs;
}