Profiler.php
2.06 KB
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
<?php
/**
* @package OSMap
* @copyright 2007-2014 XMap - Joomla! Vargas - Guillermo Vargas. All rights reserved.
* @copyright 2016 Open Source Training, LLC. All rights reserved.
* @contact www.joomlashack.com, help@joomlashack.com
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
*/
namespace Alledia\Framework;
defined('_JEXEC') or die();
class Profiler
{
protected $startTime = 0;
protected $initialMemory = 0;
protected $maxLength = 80;
protected $lastMemory = 0;
public function start()
{
$this->initialMemory = memory_get_usage();
}
public function step($label = null)
{
$this->startStep($label);
$this->endStep();
}
public function echoData()
{
echo "\n";
$total = memory_get_usage() - $this->initialMemory;
$data = "==== Mem: " . number_format($total, 0, '.', ',') . ' bytes';
$diff = $total - $this->lastMemory;
$peak = memory_get_peak_usage();
$operator = '';
echo $data;
if ($diff != 0) {
$operator = $diff > 0 ? '+' : '-';
}
echo ' diff: ' . $operator . number_format(abs($diff), 0, '.', ',') . ' bytes peak: ' . number_format($peak, '0', '.', ',') . ' bytes';
$this->lastMemory = $total;
echo "\n";
}
public function startStep($label = null)
{
echo "\n";
$this->printHeader($label);
$this->echoData();
}
public function endStep()
{
$this->echoData();
$this->printSeparator();
echo "\n";
}
protected function printHeader($label = null, $leftPadding = 4)
{
if (!is_null($label)) {
$length = $leftPadding;
echo str_repeat('=', $length);
echo " $label ";
$length += strlen($label) + 2;
echo str_repeat('=', $this->maxLength - $length);
} else {
$this->printSeparator();
}
}
protected function printSeparator()
{
echo str_repeat("=", $this->maxLength);
}
}