Completed
Pull Request — master (#177)
by Boris
02:08
created

Xhgui_Profile::_flamegraphData()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 45
rs 5.3846
cc 8
eloc 26
nc 9
nop 5
1
<?php
2
/**
3
 * Domain object for handling profile runs.
4
 *
5
 * Provides method to manipulate the data from a single profile run.
6
 */
7
class Xhgui_Profile
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * @const Key used for methods with no parent
11
     */
12
    const NO_PARENT = '__xhgui_top__';
13
14
    protected $_data;
15
    protected $_collapsed;
16
    protected $_indexed;
17
    protected $_visited;
18
19
    protected $_keys = array('ct', 'wt', 'cpu', 'mu', 'pmu');
20
    protected $_exclusiveKeys = array('ewt', 'ecpu', 'emu', 'epmu');
21
    protected $_functionCount;
22
23
    public function __construct($profile, $convert = true)
24
    {
25
        $this->_data = $profile;
26
        if (!empty($profile['profile']) && $convert) {
27
            $this->_process();
28
        }
29
    }
30
31
    /**
32
     * Convert the raw data into a flatter list that is easier to use.
33
     *
34
     * This removes some of the parentage detail as all calls of a given
35
     * method are aggregated. We are not able to maintain a full tree structure
36
     * in any case, as xhprof only keeps one level of detail.
37
     *
38
     * @return void
39
     */
40
    protected function _process()
41
    {
42
        $result = array();
43
        foreach ($this->_data['profile'] as $name => $values) {
44
            list($parent, $func) = $this->splitName($name);
45
46
            // Generate collapsed data.
47
            if (isset($result[$func])) {
48
                $result[$func] = $this->_sumKeys($result[$func], $values);
49
                $result[$func]['parents'][] = $parent;
50
            } else {
51
                $result[$func] = $values;
52
                $result[$func]['parents'] = array($parent);
53
            }
54
55
            // Build the indexed data.
56
            if ($parent === null) {
57
                $parent = self::NO_PARENT;
58
            }
59
            if (!isset($this->_indexed[$parent])) {
60
                $this->_indexed[$parent] = array();
61
            }
62
            $this->_indexed[$parent][$func] = $values;
63
        }
64
        $this->_collapsed = $result;
65
    }
66
67
    /**
68
     * Sum up the values in $this->_keys;
69
     *
70
     * @param array $a The first set of profile data
71
     * @param array $b The second set of profile data.
72
     * @return array Merged profile data.
73
     */
74
    protected function _sumKeys($a, $b)
75
    {
76
        foreach ($this->_keys as $key) {
77
            $a[$key] += $b[$key];
78
        }
79
        return $a;
80
    }
81
82
    protected function _diffKeys($a, $b, $includeSelf = true)
83
    {
84
        $keys = $this->_keys;
85
        if ($includeSelf) {
86
            $keys = array_merge($keys, $this->_exclusiveKeys);
87
        }
88
        foreach ($keys as $key) {
89
            $a[$key] -= $b[$key];
90
        }
91
        return $a;
92
    }
93
94
    protected function _diffPercentKeys($a, $b, $includeSelf = true)
95
    {
96
        $out = array();
97
        $keys = $this->_keys;
98
        if ($includeSelf) {
99
            $keys = array_merge($keys, $this->_exclusiveKeys);
100
        }
101
        foreach ($keys as $key) {
102
            if ($b[$key] != 0) {
103
                $out[$key] = $a[$key] / $b[$key];
104
            } else {
105
                $out[$key] = -1;
106
            }
107
        }
108
        return $out;
109
    }
110
111
    /**
112
     * Get the profile run data.
113
     *
114
     * TODO remove this and move all the features using it into this/
115
     * other classes.
116
     *
117
     * @return array
118
     */
119
    public function getProfile()
120
    {
121
        return $this->_collapsed;
122
    }
123
124
    public function getId()
125
    {
126
        return $this->_data['_id'];
127
    }
128
129
    public function getDate()
130
    {
131
        $date = $this->getMeta('SERVER.REQUEST_TIME');
132
        if ($date) {
133
            return new DateTime('@' . $date);
134
        }
135
        return new DateTime('now');
136
    }
137
138
    /**
139
     * Get meta data about the profile. Read's a . split path
140
     * out of the meta data in a profile. For example `SERVER.REQUEST_TIME`
141
     *
142
     * @param string $key The dotted key to read.
143
     * @return null|mixed Null on failure, otherwise the stored value.
144
     */
145
    public function getMeta($key = null)
146
    {
147
        $data = $this->_data['meta'];
148
        if ($key === null) {
149
            return $data;
150
        }
151
        $parts = explode('.', $key);
152
        foreach ($parts as $key) {
153
            if (is_array($data) && isset($data[$key])) {
154
                $data =& $data[$key];
155
            } else {
156
                return null;
157
            }
158
        }
159
        return $data;
160
    }
161
162
    /**
163
     * Read data from the profile run.
164
     *
165
     * @param string $key The function key name to read.
166
     * @param string $metric The metric to read.
167
     * @return null|float
168
     */
169
    public function get($key, $metric = null)
170
    {
171
        if (!isset($this->_collapsed[$key])) {
172
            return null;
173
        }
174
        if (empty($metric)) {
175
            return $this->_collapsed[$key];
176
        }
177
        if (!isset($this->_collapsed[$key][$metric])) {
178
            return null;
179
        }
180
        return $this->_collapsed[$key][$metric];
181
    }
182
183
    /**
184
     * Find a function matching a watched function.
185
     *
186
     * @param string $pattern The pattern to look for.
187
     * @return null|array An list of matching functions
188
     *    or null.
189
     */
190
    public function getWatched($pattern)
191
    {
192
        if (isset($this->_collapsed[$pattern])) {
193
            $data = $this->_collapsed[$pattern];
194
            $data['function'] = $pattern;
195
            return array($data);
196
        }
197
        $matches = array();
198
        $keys = array_keys($this->_collapsed);
199
        foreach ($keys as $func) {
200
            if (preg_match('`^' . $pattern . '$`', $func)) {
201
                $data = $this->_collapsed[$func];
202
                $data['function'] = $func;
203
                $matches[] = $data;
204
            }
205
        }
206
        return $matches;
207
    }
208
209
    /**
210
     * Find the parent and children method/functions for a given
211
     * symbol.
212
     *
213
     * The parent/children arrays will contain all the callers + callees
214
     * of the symbol given. The current index will give the total
215
     * inclusive values for all properties.
216
     *
217
     * @param string $symbol The name of the function/method to find
218
     *    relatives for.
219
     * @param string $metric The metric to compare $threshold with.
220
     * @param float $threshold The threshold to exclude child functions at. Any
221
     *   function that represents less than this percentage of the current metric
222
     *   will be filtered out.
223
     * @return array List of (parent, current, children)
224
     */
225
    public function getRelatives($symbol, $metric = null, $threshold = 0)
226
    {
227
        $parents = array();
0 ignored issues
show
Unused Code introduced by
$parents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
228
229
        // If the function doesn't exist, it won't have parents/children
230
        if (empty($this->_collapsed[$symbol])) {
231
            return array(
232
                array(),
233
                array(),
234
                array(),
235
            );
236
        }
237
        $current = $this->_collapsed[$symbol];
238
        $current['function'] = $symbol;
239
240
        $parents = $this->_getParents($symbol);
241
        $children = $this->_getChildren($symbol, $metric, $threshold);
242
        return array($parents, $current, $children);
243
    }
244
245
    /**
246
     * Get the parent methods for a given symbol.
247
     *
248
     * @param string $symbol The name of the function/method to find
249
     *    parents for.
250
     * @return array List of parents
251
     */
252
    protected function _getParents($symbol)
253
    {
254
        $parents = array();
255
        $current = $this->_collapsed[$symbol];
256
        foreach ($current['parents'] as $parent) {
257
            if (isset($this->_collapsed[$parent])) {
258
                $parents[] = array('function' => $parent) + $this->_collapsed[$parent];
259
            }
260
        }
261
        return $parents;
262
    }
263
264
    /**
265
     * Find symbols that are the children of the given name.
266
     *
267
     * @param string $symbol The name of the function to find children of.
268
     * @param string $metric The metric to compare $threshold with.
269
     * @param float $threshold The threshold to exclude functions at. Any
270
     *   function that represents less than
271
     * @return array An array of child methods.
272
     */
273
    protected function _getChildren($symbol, $metric = null, $threshold = 0)
274
    {
275
        $children = array();
276
        if (!isset($this->_indexed[$symbol])) {
277
            return $children;
278
        }
279
280
        $total = 0;
281
        if (isset($metric)) {
282
            $top = $this->_indexed[self::NO_PARENT];
283
            // Not always 'main()'
284
            $mainFunc = current($top);
285
            $total = $mainFunc[$metric];
286
        }
287
288
        foreach ($this->_indexed[$symbol] as $name => $data) {
289
            if (
290
                $metric && $total > 0 && $threshold > 0 &&
0 ignored issues
show
Bug Best Practice introduced by
The expression $metric of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
291
                ($this->_collapsed[$name][$metric] / $total) < $threshold
292
            ) {
293
                continue;
294
            }
295
            $children[] = $data + array('function' => $name);
296
        }
297
        return $children;
298
    }
299
300
    /**
301
     * Extracts a single dimension of data
302
     * from a profile run.
303
     *
304
     * Useful for creating bar/column graphs.
305
     * The profile data will be sorted by the column
306
     * and then the $limit records will be extracted.
307
     *
308
     * @param string $dimension The dimension to extract
309
     * @param int $limit Number of elements to pull
310
     * @return array Array of data with name = function name and
311
     *   value = the dimension.
312
     */
313
    public function extractDimension($dimension, $limit)
314
    {
315
        $profile = $this->sort($dimension, $this->_collapsed);
316
        $slice = array_slice($profile, 0, $limit);
317
        $extract = array();
318
        foreach ($slice as $func => $funcData) {
319
            $extract[] = array(
320
                'name' => $func,
321
                'value' => $funcData[$dimension]
322
            );
323
        }
324
        return $extract;
325
    }
326
327
    /**
328
     * Generate the approximate exclusive values for each metric.
329
     *
330
     * We get a==>b as the name, we need a key for a and b in the array
331
     * to get exclusive values for A we need to subtract the values of B (and any other children);
332
     * call passing in the entire profile only, should return an array of
333
     * functions with their regular timing, and exclusive numbers inside ['exclusive']
334
     *
335
     * Consider:
336
     *              /---c---d---e
337
     *          a -/----b---d---e
338
     *
339
     * We have c==>d and b==>d, and in both instances d invokes e, yet we will
340
     * have but a single d==>e result. This is a known and documented limitation of XHProf
341
     *
342
     * We have one d==>e entry, with some values, including ct=2
343
     * We also have c==>d and b==>d
344
     *
345
     * We should determine how many ==>d options there are, and equally
346
     * split the cost of d==>e across them since d==>e represents the sum total of all calls.
347
     *
348
     * Notes:
349
     *  Function names are not unique, but we're merging them
350
     *
351
     * @return Xhgui_Profile A new instance with exclusive data set.
352
     */
353
    public function calculateSelf()
354
    {
355
        // Init exclusive values
356
        foreach ($this->_collapsed as &$data) {
357
            $data['ewt'] = $data['wt'];
358
            $data['emu'] = $data['mu'];
359
            $data['ecpu'] = $data['cpu'];
360
            $data['ect'] = $data['ct'];
361
            $data['epmu'] = $data['pmu'];
362
        }
363
        unset($data);
364
365
        // Go over each method and remove each childs metrics
366
        // from the parent.
367
        foreach ($this->_collapsed as $name => $data) {
368
            $children = $this->_getChildren($name);
369
            foreach ($children as $child) {
370
                $this->_collapsed[$name]['ewt'] -= $child['wt'];
371
                $this->_collapsed[$name]['emu'] -= $child['mu'];
372
                $this->_collapsed[$name]['ecpu'] -= $child['cpu'];
373
                $this->_collapsed[$name]['ect'] -= $child['ct'];
374
                $this->_collapsed[$name]['epmu'] -= $child['pmu'];
375
            }
376
        }
377
        return $this;
378
    }
379
380
    /**
381
     * Sort data by a dimension.
382
     *
383
     * @param string $dimension The dimension to sort by.
384
     * @param array $data The data to sort.
385
     * @return array The sorted data.
386
     */
387
    public function sort($dimension, $data)
388
    {
389
        $sorter = function ($a, $b) use ($dimension) {
390
            if ($a[$dimension] == $b[$dimension]) {
391
                return 0;
392
            }
393
            return $a[$dimension] > $b[$dimension] ? -1 : 1;
394
        };
395
        uasort($data, $sorter);
396
        return $data;
397
    }
398
399
    /**
400
     * Split a key name into the parent==>child format.
401
     *
402
     * @param string $name The name to split.
403
     * @return array An array of parent, child. parent will be null if there
404
     *    is no parent.
405
     */
406
    public function splitName($name)
407
    {
408
        $a = explode("==>", $name);
409
        if (isset($a[1])) {
410
            return $a;
411
        }
412
        return array(null, $a[0]);
413
    }
414
415
    /**
416
     * Get the total number of tracked function calls in this run.
417
     *
418
     * @return int
419
     */
420
    public function getFunctionCount()
421
    {
422
        if ($this->_functionCount) {
423
            return $this->_functionCount;
424
        }
425
        $total = 0;
426
        foreach ($this->_collapsed as $data) {
427
            $total += $data['ct'];
428
        }
429
        $this->_functionCount = $total;
430
        return $this->_functionCount;
431
    }
432
433
    /**
434
     * Compare this run to another run.
435
     *
436
     * @param Xhgui_Profile $head The other run to compare with
437
     * @return array An array of comparison data.
438
     */
439
    public function compare(Xhgui_Profile $head)
440
    {
441
        $this->calculateSelf();
442
        $head->calculateSelf();
443
444
        $keys = array_merge($this->_keys, $this->_exclusiveKeys);
445
        $emptyData = array_fill_keys($keys, 0);
446
447
        $diffPercent = array();
448
        $diff = array();
449
        foreach ($this->_collapsed as $key => $baseData) {
450
            $headData = $head->get($key);
451
            if (!$headData) {
452
                $diff[$key] = $this->_diffKeys($emptyData, $baseData);
453
                continue;
454
            }
455
            $diff[$key] = $this->_diffKeys($headData, $baseData);
456
457
            if ($key === 'main()') {
458
                $diffPercent[$key] = $this->_diffPercentKeys($headData, $baseData);
459
            }
460
        }
461
462
        $diff['functionCount'] = $head->getFunctionCount() - $this->getFunctionCount();
463
        $diffPercent['functionCount'] = $head->getFunctionCount() / $this->getFunctionCount();
464
465
        return array(
466
            'base' => $this,
467
            'head' => $head,
468
            'diff' => $diff,
469
            'diffPercent' => $diffPercent,
470
        );
471
    }
472
473
    /**
474
     * Get the max value for any give metric.
475
     *
476
     * @param string $metric The metric to get a max value for.
477
     */
478
    protected function _maxValue($metric)
479
    {
480
        return array_reduce(
481
            $this->_collapsed,
482
            function ($result, $item) use ($metric) {
483
                if ($item[$metric] > $result) {
484
                    return $item[$metric];
485
                }
486
                return $result;
487
            },
488
            0
489
        );
490
    }
491
492
    /**
493
     * Return a structured array suitable for generating callgraph visualizations.
494
     *
495
     * Functions whose inclusive time is less than 2% of the total time will
496
     * be excluded from the callgraph data.
497
     *
498
     * @return array
499
     */
500
    public function getCallgraph($metric = 'wt', $threshold = 0.01)
501
    {
502
        $valid = array_merge($this->_keys, $this->_exclusiveKeys);
503
        if (!in_array($metric, $valid)) {
504
            throw new Exception("Unknown metric '$metric'. Cannot generate callgraph.");
505
        }
506
        $this->calculateSelf();
507
508
        // Non exclusive metrics are always main() because it is the root call scope.
509 View Code Duplication
        if (in_array($metric, $this->_exclusiveKeys)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
510
            $main = $this->_maxValue($metric);
511
        } else {
512
            $main = $this->_collapsed['main()'][$metric];
513
        }
514
515
        $this->_visited = $this->_nodes = $this->_links = array();
0 ignored issues
show
Bug introduced by
The property _nodes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property _links does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
516
        $this->_callgraphData(self::NO_PARENT, $main, $metric, $threshold);
517
        $out = array(
518
            'metric' => $metric,
519
            'total' => $main,
520
            'nodes' => $this->_nodes,
521
            'links' => $this->_links
522
        );
523
        unset($this->_visited, $this->_nodes, $this->_links);
524
        return $out;
525
    }
526
527
    protected function _callgraphData($parentName, $main, $metric, $threshold, $parentIndex = null)
528
    {
529
        // Leaves don't have children, and don't have links/nodes to add.
530
        if (!isset($this->_indexed[$parentName])) {
531
            return;
532
        }
533
534
        $children = $this->_indexed[$parentName];
535
        foreach ($children as $childName => $metrics) {
536
            $metrics = $this->_collapsed[$childName];
537
            if ($metrics[$metric] / $main <= $threshold) {
538
                continue;
539
            }
540
            $revisit = false;
541
542
            // Keep track of which nodes we've visited and their position
543
            // in the node list.
544
            if (!isset($this->_visited[$childName])) {
545
                $index = count($this->_nodes);
546
                $this->_visited[$childName] = $index;
547
548
                $this->_nodes[] = array(
549
                    'name' => $childName,
550
                    'callCount' => $metrics['ct'],
551
                    'value' => $metrics[$metric],
552
                );
553
            } else {
554
                $revisit = true;
555
                $index = $this->_visited[$childName];
556
            }
557
558
            if ($parentIndex !== null) {
559
                $this->_links[] = array(
560
                    'source' => $parentName,
561
                    'target' => $childName,
562
                    'callCount' => $metrics['ct'],
563
                );
564
            }
565
566
            // If the current function has more children,
567
            // walk that call subgraph.
568
            if (isset($this->_indexed[$childName]) && !$revisit) {
569
                $this->_callgraphData($childName, $main, $metric, $threshold, $index);
570
            }
571
        }
572
    }
573
574
    /**
575
     * Return a structured array suitable for generating flamegraph visualizations.
576
     *
577
     * Functions whose inclusive time is less than 2% of the total time will
578
     * be excluded from the callgraph data.
579
     *
580
     * @return array
581
     */
582
    public function getFlamegraph($metric = 'wt', $threshold = 0.01)
583
    {
584
        $valid = array_merge($this->_keys, $this->_exclusiveKeys);
585
        if (!in_array($metric, $valid)) {
586
            throw new Exception("Unknown metric '$metric'. Cannot generate flamegraph.");
587
        }
588
        $this->calculateSelf();
589
590
        // Non exclusive metrics are always main() because it is the root call scope.
591 View Code Duplication
        if (in_array($metric, $this->_exclusiveKeys)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
592
            $main = $this->_maxValue($metric);
593
        } else {
594
            $main = $this->_collapsed['main()'][$metric];
595
        }
596
597
        $this->_visited = $this->_nodes = $this->_links = array();
598
        $flamegraph = $this->_flamegraphData(self::NO_PARENT, $main, $metric, $threshold);
599
        return array_shift($flamegraph);
600
    }
601
602
    protected function _flamegraphData($parentName, $main, $metric, $threshold, $parentIndex = null)
0 ignored issues
show
Unused Code introduced by
The parameter $parentIndex is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
603
    {
604
        $result = array();
605
        // Leaves don't have children, and don't have links/nodes to add.
606
        if (!isset($this->_indexed[$parentName])) {
607
            return $result;
608
        }
609
610
        $children = $this->_indexed[$parentName];
611
        foreach ($children as $childName => $metrics) {
612
            $metrics = $this->_collapsed[$childName];
613
            if ($metrics[$metric] / $main <= $threshold) {
614
                continue;
615
            }
616
            $current = array(
617
                'name' => $childName,
618
                'value' => $metrics[$metric],
619
            );
620
            $revisit = false;
621
622
            // Keep track of which nodes we've visited and their position
623
            // in the node list.
624
            if (!isset($this->_visited[$childName])) {
625
                $index = count($this->_nodes);
626
                $this->_visited[$childName] = $index;
627
                $this->_nodes[] = $current;
628
            } else {
629
                $revisit = true;
630
                $index = $this->_visited[$childName];
631
            }
632
633
            // If the current function has more children,
634
            // walk that call subgraph.
635
            if (isset($this->_indexed[$childName]) && !$revisit) {
636
                $grandChildren = $this->_flamegraphData($childName, $main, $metric, $threshold, $index);
637
                if (!empty($grandChildren)) {
638
                    $current['children'] = $grandChildren;
639
                }
640
            }
641
642
            $result[] = $current;
643
        }
644
645
        return $result;
646
    }
647
648
    public function toArray()
649
    {
650
        return $this->_data;
651
    }
652
}
653