11 public $percentage_format =
"%01.2f";
12 public $duration_format =
"%01.6f";
13 public $minimum_percentage = 0.2;
29 if (!isset($times[
':end'])) {
30 $times[
':end'] = microtime();
33 $begin = $this->findBeginTime($times);
34 $end = $this->findEndTime($times);
35 $this->total = $this->diffMicrotime($begin, $end);
37 return $this->analyzePeriod(
'', $times);
49 $is_root = empty(
$list);
51 if (isset($tree[
'periods'])) {
52 foreach ($tree[
'periods'] as $period) {
53 $this->flattenTree(
$list, $period,
"{$prefix} {$period['name']}");
55 unset($tree[
'periods']);
57 $tree[
'name'] = trim($prefix);
61 usort(
$list,
function ($a, $b) {
62 if ($a[
'duration'] == $b[
'duration']) {
65 return ($a[
'duration'] > $b[
'duration']) ? -1 : 1;
77 $tree[
'duration'] =
sprintf($this->duration_format, $tree[
'duration']);
78 if (isset($tree[
'percentage'])) {
79 $tree[
'percentage'] =
sprintf($this->percentage_format, $tree[
'percentage']);
81 if (isset($tree[
'periods'])) {
82 $tree[
'periods'] = array_map([$this,
'formatTree'], $tree[
'periods']);
98 $profiler =
new self();
100 if ($min_percentage !== null) {
101 $profiler->minimum_percentage = $min_percentage;
105 $tree = $profiler->formatTree($tree);
106 $data[
'tree'] = $tree;
107 $data[
'total'] = $tree[
'duration'] .
" seconds";
110 $profiler->flattenTree(
$list, $tree);
113 $list = array_map(
function ($period)
use ($root) {
114 $period[
'name'] = str_replace(
"Closure $root",
"Closure ", $period[
'name']);
115 return "{$period['percentage']}% ({$period['duration']}) {$period['name']}";
120 $html .=
"<script>console.log(" . json_encode($data) .
");</script>";
133 private function analyzePeriod(
$name, array $times) {
134 $begin = $this->findBeginTime($times);
135 $end = $this->findEndTime($times);
136 if ($begin ===
false || $end ===
false) {
139 unset($times[
':begin'], $times[
':end']);
141 $total = $this->diffMicrotime($begin, $end);
145 'duration' => $total,
148 foreach ($times as $times_key => $period) {
149 $period = $this->analyzePeriod($times_key, $period);
150 if ($period ===
false) {
153 $period[
'percentage'] = 100 * $period[
'duration'] / $this->total;
154 if ($period[
'percentage'] < $this->minimum_percentage) {
157 $ret[
'periods'][] = $period;
160 if (isset($ret[
'periods'])) {
161 usort($ret[
'periods'],
function ($a, $b) {
162 if ($a[
'duration'] == $b[
'duration']) {
165 return ($a[
'duration'] > $b[
'duration']) ? -1 : 1;
178 private function findBeginTime(array $times) {
179 if (isset($times[
':begin'])) {
180 return $times[
':begin'];
182 unset($times[
':begin'], $times[
':end']);
183 $first = reset($times);
184 if (is_array($first)) {
185 return $this->findBeginTime($first);
196 private function findEndTime(array $times) {
197 if (isset($times[
':end'])) {
198 return $times[
':end'];
200 unset($times[
':begin'], $times[
':end']);
202 if (is_array($last)) {
203 return $this->findEndTime($last);
216 private function diffMicrotime($start, $end) {
217 list($start_usec, $start_sec) = explode(
" ", $start);
218 list($end_usec, $end_sec) = explode(
" ", $end);
219 $diff_sec = (int)$end_sec - (
int)$start_sec;
220 $diff_usec = (float)$end_usec - (
float)$start_usec;
221 return (
float)$diff_sec + $diff_usec;
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
static handlePageOutput($hook, $type, $html, $params)
Append a SCRIPT element to the page output.
getTimes()
Get the tree of recorded start/end times.
if($guid==elgg_get_logged_in_user_guid()) $name
Analyzes duration of functions, queries, and processes.
Capture timing info for profiling.
foreach($emails as $email) $html
buildTree(Timer $timer)
Return a tree of time periods from a Timer.
flattenTree(array &$list=[], array $tree, $prefix= '')
Turn the tree of times into a sorted list.
formatTree(array $tree)
Nicely format the elapsed time values.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
var sprintf
sprintf() for JavaScript 0.7-beta1 http://www.diveintojavascript.com/projects/javascript-sprintf ...
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use