|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
namespace CloudFramework\Tool; |
|
4
|
|
|
|
|
5
|
|
|
use CloudFramework\CloudFrameworkApp; |
|
6
|
|
|
use CloudFramework\Patterns\Singleton; |
|
7
|
|
|
|
|
8
|
|
|
class Performance extends Singleton |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
var $data; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
1 |
|
function __construct() |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
// Performance Vars |
|
16
|
1 |
|
$this->data['initMicrotime'] = microtime(TRUE); |
|
17
|
1 |
|
$this->data['lastMicrotime'] = $this->data['initMicrotime']; |
|
18
|
1 |
|
$this->data['initMemory'] = memory_get_usage() / (1024 * 1024); |
|
19
|
1 |
|
$this->data['lastMemory'] = $this->data['initMemory']; |
|
20
|
1 |
|
$this->data['lastIndex'] = 1; |
|
21
|
1 |
|
$this->data['info'][] = 'File: ' . str_replace($_SERVER['DOCUMENT_ROOT'], '', __FILE__); |
|
22
|
1 |
|
$this->data['info'][] = 'Init Memory Usage: ' . number_format(round($this->data['initMemory'], 4), 4) . 'Mb'; |
|
23
|
1 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
function add($title, $file = '', $type = 'all') |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
// Hidding full path (security) |
|
28
|
|
|
$file = str_replace($_SERVER['DOCUMENT_ROOT'], '', $file); |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
if ($type == 'note') $line = "[$type"; |
|
32
|
|
|
else $line = $this->data['lastIndex'] . ' ['; |
|
33
|
|
|
|
|
34
|
|
|
if (strlen($file)) $file = " ($file)"; |
|
35
|
|
|
|
|
36
|
|
|
$_mem = memory_get_usage() / (1024 * 1024) - $this->data['lastMemory']; |
|
37
|
|
View Code Duplication |
if ($type == 'all' || $type == 'endnote' || $type == 'memory' || $_GET['data'] == $this->data['lastIndex']) { |
|
|
|
|
|
|
38
|
|
|
$line .= number_format(round($_mem, 3), 3) . ' Mb'; |
|
39
|
|
|
$this->data['lastMemory'] = memory_get_usage() / (1024 * 1024); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$_time = microtime(TRUE) - $this->data['lastMicrotime']; |
|
43
|
|
View Code Duplication |
if ($type == 'all' || $type == 'endnote' || $type == 'time' || $_GET['data'] == $this->data['lastIndex']) { |
|
|
|
|
|
|
44
|
|
|
$line .= (($line == '[') ? '' : ', ') . (round($_time, 3)) . ' secs'; |
|
45
|
|
|
$this->data['lastMicrotime'] = microtime(TRUE); |
|
46
|
|
|
} |
|
47
|
|
|
$line .= '] ' . $title; |
|
48
|
|
|
$line = (($type != 'note') ? '[' . number_format(round(memory_get_usage() / (1024 * 1024), 3), 3) . ' Mb, ' |
|
49
|
|
|
. (round(microtime(TRUE) - $this->data['initMicrotime'], 3)) |
|
50
|
|
|
. ' secs] / ' : '') . $line . $file; |
|
51
|
|
|
if ($type == 'endnote') $line = "[$type] " . $line; |
|
52
|
|
|
$this->data['info'][] = $line; |
|
53
|
|
|
|
|
54
|
|
|
if ($title) { |
|
55
|
|
|
$this->data['titles'][$title]['mem'] = $_mem; |
|
56
|
|
|
$this->data['titles'][$title]['time'] += $_time; |
|
57
|
|
|
$this->data['titles'][$title]['lastIndex'] = $this->data['lastIndex']; |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (isset($_GET['__p']) && $_GET['__p'] == $this->data['lastIndex']) { |
|
62
|
|
|
__sp(); |
|
63
|
|
|
exit; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->data['lastIndex']++; |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
function init($spacename, $key) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$this->data['init'][$spacename][$key]['mem'] = memory_get_usage(); |
|
73
|
|
|
$this->data['init'][$spacename][$key]['time'] = microtime(TRUE); |
|
74
|
|
|
$this->data['init'][$spacename][$key]['ok'] = TRUE; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
function end($spacename, $key, $ok = TRUE, $msg = FALSE) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
$this->data['init'][$spacename][$key]['mem'] = round((memory_get_usage() - $this->data['init'][$spacename][$key]['mem']) / (1024 * 1024), 3) . ' Mb'; |
|
80
|
|
|
$this->data['init'][$spacename][$key]['time'] = round(microtime(TRUE) - $this->data['init'][$spacename][$key]['time'], 3) . ' secs'; |
|
81
|
|
|
$this->data['init'][$spacename][$key]['ok'] = $ok; |
|
82
|
|
|
if ($msg !== FALSE) $this->data['init'][$spacename][$key]['notes'] = $msg; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
function getLog() |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
$ret = ''; |
|
88
|
|
|
$spaces = ""; |
|
89
|
|
|
if (is_array($this->data['info'])) |
|
90
|
|
|
foreach ($this->data['info'] as $key => $value) { |
|
91
|
|
View Code Duplication |
if (is_string($value) && strpos($value, '[endnote]') !== FALSE) $spaces = substr($spaces, 0, -2); |
|
|
|
|
|
|
92
|
|
|
$ret .= $spaces; |
|
93
|
|
|
$ret .= ((is_string($value)) ? $value : print_r($value, TRUE)) . "\n"; |
|
94
|
|
|
if (is_string($value) && strpos($value, '[note]') !== FALSE) $spaces .= " "; |
|
95
|
|
|
if (is_string($value) && strpos($value, '[endnote]') !== FALSE) $ret .= "\n"; |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
$ret .= "\n\nTOTALS:\n"; |
|
99
|
|
|
if (is_array($this->data['titles'])) |
|
100
|
|
View Code Duplication |
foreach ($this->data['titles'] as $key => $value) { |
|
|
|
|
|
|
101
|
|
|
$ret .= "[$key] : " . round($value['mem'], 3) . ' Mb / ' . round($value['time'], 3) . " secs.\n"; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $ret; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Performance Functions |
|
109
|
1 |
|
$__p = Performance::getInstance(); |
|
110
|
|
|
|
|
111
|
|
|
function __sp($title = '', $top = "<!--\n", $bottom = "\n-->") |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
|
|
114
|
|
|
$__p = Performance::getInstance(); |
|
115
|
|
|
$addhtml = ''; |
|
116
|
|
|
|
|
117
|
|
|
if (isset($_GET['debug'])) { |
|
118
|
|
|
if (is_object($adnbp)) { |
|
119
|
|
|
$__p->data['info'][] = 'Object ADNBP'; |
|
|
|
|
|
|
120
|
|
|
$__p->data['info'][] = $adnbp; |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
$__p->data['info'][] = '$_SERVER'; |
|
|
|
|
|
|
123
|
|
|
$__p->data['info'][] = $_SERVER; |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
echo $top; |
|
126
|
|
|
echo $title; |
|
127
|
|
|
$spaces = ""; |
|
128
|
|
|
foreach ($__p->data['info'] as $key => $value) { |
|
|
|
|
|
|
129
|
|
View Code Duplication |
if (is_string($value) && strpos($value, '[endnote]') !== FALSE) $spaces = substr($spaces, 0, -2); |
|
|
|
|
|
|
130
|
|
|
echo $spaces; |
|
131
|
|
|
echo ((is_string($value)) ? $value : print_r($value, TRUE)) . "\n"; |
|
132
|
|
|
if (is_string($value) && strpos($value, '[note]') !== FALSE) $spaces .= " "; |
|
133
|
|
|
if (is_string($value) && strpos($value, '[endnote]') !== FALSE) echo "\n"; |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
echo "\n\nTOTALS:\n"; |
|
137
|
|
View Code Duplication |
foreach ($__p->data['titles'] as $key => $value) { |
|
|
|
|
|
|
138
|
|
|
echo "[$key] : " . round($value['mem'], 3) . ' Mb / ' . round($value['time'], 3) . " secs.\n"; |
|
139
|
|
|
} |
|
140
|
|
|
echo $addhtml; |
|
141
|
|
|
echo $bottom; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
function __p($title = NULL, $file = NULL, $type = 'all') |
|
145
|
|
|
{ |
|
146
|
|
|
$__p = Performance::getInstance(); |
|
147
|
|
|
if ($title === NULL && $file == NULL) return $__p->data; |
|
|
|
|
|
|
148
|
|
|
else $__p->add($title, $file, $type); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
function __print($args) |
|
152
|
|
|
{ |
|
153
|
|
|
echo "<pre>"; |
|
154
|
|
|
for ($i = 0, $tr = count($args); $i < $tr; $i++) { |
|
155
|
|
|
if ($args[$i] === "exit") |
|
156
|
|
|
exit; |
|
|
|
|
|
|
157
|
|
|
echo "\n<li>[$i]: "; |
|
158
|
|
|
if (is_array($args[$i])) |
|
159
|
|
|
echo print_r($args[$i], TRUE); |
|
160
|
|
|
else if (is_object($args[$i])) |
|
161
|
|
|
echo var_dump($args[$i]); |
|
|
|
|
|
|
162
|
|
|
else if (is_bool($args[$i])) |
|
163
|
|
|
echo ($args[$i]) ? 'true' : 'false'; |
|
164
|
|
|
else if (is_null($args[$i])) |
|
165
|
|
|
echo 'NULL'; |
|
166
|
|
|
else |
|
167
|
|
|
echo $args[$i]; |
|
168
|
|
|
} |
|
169
|
|
|
echo "</pre>"; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
function _print() |
|
173
|
|
|
{ |
|
174
|
|
|
__print(func_get_args()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
function _printe() |
|
178
|
|
|
{ |
|
179
|
|
|
__print(array_merge(func_get_args(), array('exit'))); |
|
180
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.