@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * @phutil-external-symbol function xhprof_compute_flat_info
5 */
6final class PhabricatorXHProfProfileSymbolView
7 extends PhabricatorXHProfProfileView {
8
9 private $profileData;
10 private $symbol;
11
12 public function setProfileData(array $data) {
13 $this->profileData = $data;
14 return $this;
15 }
16
17 public function setSymbol($symbol) {
18 $this->symbol = $symbol;
19 return $this;
20 }
21
22 public function render() {
23 DarkConsoleXHProfPluginAPI::includeXHProfLib();
24
25 $data = $this->profileData;
26
27 $GLOBALS['display_calls'] = true;
28 $totals = array();
29 $flat = xhprof_compute_flat_info($data, $totals);
30 unset($GLOBALS['display_calls']);
31
32 $symbol = $this->symbol;
33
34 $children = array();
35 $parents = array();
36 foreach ($this->profileData as $key => $counters) {
37 if (strpos($key, '==>') !== false) {
38 list($parent, $child) = explode('==>', $key, 2);
39 } else {
40 continue;
41 }
42 if ($parent == $symbol) {
43 $children[$key] = $child;
44 } else if ($child == $symbol) {
45 $parents[$key] = $parent;
46 }
47 }
48
49 $rows = array();
50 $rows[] = array(
51 pht('Metrics for this Call'),
52 '',
53 '',
54 '',
55 );
56 $rows[] = $this->formatRow(
57 array(
58 $symbol,
59 $flat[$symbol]['ct'],
60 $flat[$symbol]['wt'],
61 1.0,
62 ));
63
64 $rows[] = array(
65 pht('Parent Calls'),
66 '',
67 '',
68 '',
69 );
70 foreach ($parents as $key => $name) {
71 $rows[] = $this->formatRow(
72 array(
73 $name,
74 $data[$key]['ct'],
75 $data[$key]['wt'],
76 '',
77 ));
78 }
79
80
81 $rows[] = array(
82 pht('Child Calls'),
83 '',
84 '',
85 '',
86 );
87 $child_rows = array();
88 foreach ($children as $key => $name) {
89 $child_rows[] = array(
90 $name,
91 $data[$key]['ct'],
92 $data[$key]['wt'],
93 $data[$key]['wt'] / $flat[$symbol]['wt'],
94 );
95 }
96 $child_rows = isort($child_rows, '2');
97 $child_rows = array_reverse($child_rows);
98 $rows = array_merge(
99 $rows,
100 array_map(array($this, 'formatRow'), $child_rows));
101
102 $table = new AphrontTableView($rows);
103 $table->setHeaders(
104 array(
105 pht('Symbol'),
106 pht('Count'),
107 pht('Wall Time'),
108 '%',
109 ));
110 $table->setColumnClasses(
111 array(
112 'wide pri',
113 'n',
114 'n',
115 'n',
116 ));
117
118 $panel = new PHUIObjectBoxView();
119 $panel->setHeaderText(pht('XHProf Profile'));
120 $panel->setTable($table);
121
122 return $panel->render();
123 }
124
125 private function formatRow(array $row) {
126 return array(
127 $this->renderSymbolLink($row[0]),
128 number_format($row[1]),
129 number_format($row[2]).' us',
130 ($row[3] != '' ? sprintf('%.1f%%', 100 * $row[3]) : ''),
131 );
132 }
133
134}