1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @maintainer Timur Shagiakhmetov <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Badoo\LiveProfilerUI; |
8
|
|
|
|
9
|
|
|
class FlameGraph |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get svg data for flame graph |
13
|
|
|
* @param string $graph_input |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
4 |
|
public static function getSVG(string $graph_input) : string |
17
|
|
|
{ |
18
|
4 |
|
if (!$graph_input) { |
19
|
4 |
|
return ''; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$tmp_file = tempnam(__DIR__, 'flamefile'); |
23
|
|
|
file_put_contents($tmp_file, $graph_input); |
24
|
|
|
exec('perl ' . __DIR__ . '/../../../scripts/flamegraph.pl ' . $tmp_file, $output); |
25
|
|
|
unlink($tmp_file); |
26
|
|
|
|
27
|
|
|
return implode("\n", $output); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \Badoo\LiveProfilerUI\Entity\MethodTree[] $elements |
32
|
|
|
* @param array $parents_param |
33
|
|
|
* @param array $parent |
34
|
|
|
* @param string $param |
35
|
|
|
* @param float $threshold |
36
|
|
|
* @param int $level |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
6 |
|
public static function buildFlameGraphInput( |
40
|
|
|
array $elements, |
41
|
|
|
array $parents_param, |
42
|
|
|
array $parent, |
43
|
|
|
string $param, |
44
|
|
|
float $threshold, |
45
|
|
|
int $level = 0 |
46
|
|
|
) : string { |
47
|
6 |
|
if ($level > 50) { |
48
|
|
|
// limit nesting level |
49
|
1 |
|
return ''; |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
$texts = ''; |
53
|
5 |
|
foreach ($elements as $Element) { |
54
|
5 |
|
if ($Element->getParentId() === $parent['method_id']) { |
55
|
4 |
|
$element_value = $Element->getValue($param); |
56
|
4 |
|
$value = $parent[$param] - $element_value; |
57
|
|
|
|
58
|
4 |
|
if (($value <= 0) && !empty($parents_param[$Element->getParentId()])) { |
59
|
1 |
|
$p = $parents_param[$Element->getParentId()]; |
60
|
1 |
|
$sum_p = array_sum($p); |
61
|
1 |
|
$element_value = 0; |
62
|
1 |
|
if ($sum_p !== 0) { |
63
|
1 |
|
$element_value = ($parent[$param] / $sum_p) * $Element->getValue($param); |
64
|
|
|
} |
65
|
1 |
|
$value = $parent[$param] - $element_value; |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
if ($element_value < $threshold) { |
69
|
2 |
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$new_parent = [ |
73
|
2 |
|
'method_id' => $Element->getMethodId(), |
74
|
2 |
|
'name' => $parent['name'] . ';' . $Element->getMethodNameAlt(), |
75
|
2 |
|
$param => $element_value |
76
|
|
|
]; |
77
|
2 |
|
$texts .= self::buildFlameGraphInput( |
78
|
2 |
|
$elements, |
79
|
2 |
|
$parents_param, |
80
|
2 |
|
$new_parent, |
81
|
2 |
|
$param, |
82
|
2 |
|
$threshold, |
83
|
2 |
|
$level + 1 |
84
|
|
|
); |
85
|
2 |
|
$parent[$param] = $value; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
5 |
|
$texts .= $parent['name'] . ' ' . $parent[$param] . "\n"; |
90
|
|
|
|
91
|
5 |
|
return $texts; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|