1: <?php
2: 3: 4: 5: 6:
7:
8:
9: namespace Rundiz\Profiler;
10:
11: 12: 13: 14: 15: 16: 17: 18: 19:
20: class Profiler extends \Rundiz\Profiler\ProfilerBase
21: {
22:
23:
24: 25: 26: 27: 28:
29: public $Console;
30:
31:
32: 33: 34:
35: public function __construct() {
36: $this->start_time = $this->getMicrotime(true);
37:
38: if (!class_exists('\\Rundiz\\Profiler\\Console')) {
39: require_once __DIR__.DIRECTORY_SEPARATOR.'Console.php';
40: }
41: $this->Console = new \Rundiz\Profiler\Console($this);
42: }
43:
44:
45: 46: 47: 48: 49: 50:
51: public function countTotalLogType($logtype)
52: {
53: $count = 0;
54:
55: if (isset($this->log_sections['Logs']) && is_array($this->log_sections['Logs'])) {
56: foreach ($this->log_sections['Logs'] as $item) {
57: if (isset($item['logtype']) && $item['logtype'] == $logtype) {
58: $count++;
59: }
60: }
61: unset($item);
62: }
63:
64: return $count;
65: }
66:
67:
68: 69: 70: 71: 72:
73: public function display($dbh = '', $display_db_function = '')
74: {
75: $this->gatherAll();
76:
77:
78: ob_start();
79: require __DIR__.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.'display.php';
80: $output = ob_get_contents();
81: ob_end_clean();
82: return $output;
83: }
84:
85:
86: 87: 88: 89: 90:
91: public function dumptest()
92: {
93: return [
94: 'log_sections' => $this->log_sections,
95: 'start_time' => $this->start_time,
96: 'end_time' => $this->end_time,
97: 'max_memory_usage' => $this->max_memory_usage,
98: ];
99: }
100:
101:
102: 103: 104:
105: public function gatherAll()
106: {
107:
108:
109: $this->end_time = $this->getMicrotime();
110: $this->max_memory_usage = memory_get_peak_usage();
111:
112: $this->gatherFiles();
113: $this->gatherInputGet();
114: $this->gatherInputPost();
115: $this->gatherInputSession();
116: }
117:
118:
119: 120: 121:
122: private function gatherFiles()
123: {
124: if (!array_key_exists('Files', $this->log_sections)) {
125: return ;
126: }
127:
128: $files = get_included_files();
129: $section_data_array = [];
130: $total_size = 0;
131: $largest_size = 0;
132:
133: if (is_array($files)) {
134: foreach ($files as $file) {
135: $size = filesize($file);
136: $section_data_array[] = [
137: 'data' => $file,
138: 'size' => $size,
139: ];
140: $total_size = bcadd($total_size, $size);
141:
142: if ($size > $largest_size) {
143: $largest_size = $size;
144: }
145: }
146: unset($file, $size);
147: }
148:
149: $section_data_array['total_size'] = $total_size;
150: $section_data_array['largest_size'] = $largest_size;
151: unset($largest_size, $total_size);
152:
153: $this->log_sections['Files'] = $section_data_array;
154: unset($files, $section_data_array);
155: }
156:
157:
158: 159: 160:
161: private function gatherInputGet()
162: {
163: if (!array_key_exists('Get', $this->log_sections)) {
164: return ;
165: }
166:
167: $section_data_array = [];
168:
169: if (isset($_GET) && is_array($_GET)) {
170: foreach ($_GET as $name => $value) {
171: $section_data_array[] = [
172: 'data' => $name,
173: 'inputvalue' => $value,
174: ];
175: }
176: unset($name, $value);
177: }
178:
179: $this->log_sections['Get'] = $section_data_array;
180: unset($section_data_array);
181: }
182:
183:
184: 185: 186:
187: private function gatherInputPost()
188: {
189: if (!array_key_exists('Post', $this->log_sections)) {
190: return ;
191: }
192:
193: $section_data_array = [];
194:
195: if (isset($_POST) && is_array($_POST)) {
196: foreach ($_POST as $name => $value) {
197: $section_data_array[] = [
198: 'data' => $name,
199: 'inputvalue' => $value,
200: ];
201: }
202: unset($name, $value);
203: }
204:
205: $this->log_sections['Post'] = $section_data_array;
206: unset($section_data_array);
207: }
208:
209:
210: 211: 212:
213: private function gatherInputSession()
214: {
215: if (!array_key_exists('Session', $this->log_sections)) {
216: return ;
217: }
218:
219: $section_data_array = [];
220:
221: if (isset($_SESSION) && is_array($_SESSION)) {
222: foreach ($_SESSION as $name => $value) {
223: $section_data_array[] = [
224: 'data' => $name,
225: 'inputvalue' => $value,
226: ];
227: }
228: unset($name, $value);
229: }
230:
231: $this->log_sections['Session'] = $section_data_array;
232: unset($section_data_array);
233: }
234:
235:
236: 237: 238: 239: 240: 241: 242: 243:
244: public function getReadableFileSize($size, $retstring = null) {
245:
246: $sizes = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
247:
248: if ($retstring === null) {
249: $retstring = '%01.2f %s';
250: }
251:
252: $lastsizestring = end($sizes);
253:
254: foreach ($sizes as $sizestring) {
255: if ($size < 1024) {
256: break;
257: }
258: if ($sizestring != $lastsizestring) {
259: $size /= 1024;
260: }
261: }
262: if ($sizestring == $sizes[0]) {
263: $retstring = '%01d %s';
264: }
265: return sprintf($retstring, $size, $sizestring);
266: }
267:
268:
269: 270: 271: 272: 273: 274:
275: public function getMicrotime($at_start = false)
276: {
277: if ($at_start === true && is_array($_SERVER) && array_key_exists('REQUEST_TIME_FLOAT', $_SERVER)) {
278: return floatval($_SERVER['REQUEST_TIME_FLOAT']);
279: }
280:
281: return floatval(microtime(true));
282: }
283:
284:
285: 286: 287: 288: 289: 290: 291:
292: public function getReadableTime($time) {
293: $ret = $time;
294: $formatter = 0;
295: $formats = ['ms', 's', 'm'];
296: if ($time >= 1000 && $time < 60000) {
297: $formatter = 1;
298: $ret = ($time / 1000);
299: }
300: if ($time >= 60000) {
301: $formatter = 2;
302: $ret = ($time / 1000) / 60;
303: }
304: $ret = number_format($ret, 3, '.', '') . ' ' . $formats[$formatter];
305: unset($formats, $formatter);
306: return $ret;
307: }
308:
309:
310: 311: 312: 313: 314: 315: 316: 317:
318: public function summaryMatchKey($section, $matchKey, $sectionKey)
319: {
320: if (!is_string($section)) {
321: $section = null;
322: }
323:
324: if (!is_string($matchKey)) {
325: $matchKey = null;
326: }
327:
328: if (!is_numeric($sectionKey)) {
329: $sectionKey = 0;
330: }
331:
332: $output = '';
333:
334: if (isset($this->log_sections[$section]) && is_array($this->log_sections[$section]) && $matchKey !== null) {
335: $matchKeyTime = [];
336: $matchKeyMemory = [];
337:
338: foreach ($this->log_sections[$section] as $key => $item) {
339: if (isset($item['matchKey']) && $item['matchKey'] == $matchKey && $key <= $sectionKey) {
340: if (count($matchKeyMemory) >= 2 || count($matchKeyTime) >= 2) {
341: break;
342: }
343:
344: if (isset($item['time'])) {
345: $matchKeyTime[] = $item['time'];
346: }
347:
348: if (isset($item['memory'])) {
349: $matchKeyMemory[] = $item['memory'];
350: }
351: }
352: }
353: unset($item, $key);
354:
355: if (count($matchKeyTime) >= 2) {
356: $output = $this->getReadableTime((max($matchKeyTime)-min($matchKeyTime))*1000);
357: } elseif (count($matchKeyMemory) >= 2) {
358: $output = $this->getReadableFileSize(max($matchKeyMemory)-min($matchKeyMemory));
359: }
360:
361: unset($matchKeyMemory, $matchKeyTime);
362: }
363:
364: return $output;
365: }
366:
367: }