|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/***************************************** |
|
4
|
|
|
* Title : PHP Quick Profiler Class |
|
5
|
|
|
* Author : Created by Ryan Campbell |
|
6
|
|
|
* URL : http://particletree.com/features/php-quick-profiler/ |
|
7
|
|
|
* Description : This class processes the logs and organizes the data |
|
8
|
|
|
* for output to the browser. Initialize this class with a start time |
|
9
|
|
|
* at the beginning of your code, and then call the display method when |
|
10
|
|
|
* your code is terminating. |
|
11
|
|
|
*****************************************/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Particletree\Pqp; |
|
14
|
|
|
|
|
15
|
|
|
class PhpQuickProfiler |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** @var Particletree\Pqp\Console */ |
|
19
|
|
|
protected $console; |
|
20
|
|
|
|
|
21
|
|
|
/** @var integer */ |
|
22
|
|
|
protected $startTime; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Particletree\Pqp\Console $console |
|
26
|
|
|
* @param integer $startTime |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Console $console, $startTime = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->console = $console; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
if (is_null($startTime)) { |
|
33
|
|
|
$startTime = microtime(true); |
|
34
|
|
|
} |
|
35
|
|
|
$this->startTime = $startTime; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/*------------------------------------------- |
|
39
|
|
|
AGGREGATE DATA ON THE FILES INCLUDED |
|
40
|
|
|
-------------------------------------------*/ |
|
41
|
|
|
|
|
42
|
|
|
public function gatherFileData() { |
|
43
|
|
|
$files = get_included_files(); |
|
44
|
|
|
$fileList = array(); |
|
45
|
|
|
$fileTotals = array( |
|
46
|
|
|
"count" => count($files), |
|
47
|
|
|
"size" => 0, |
|
48
|
|
|
"largest" => 0, |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
foreach($files as $file) { |
|
52
|
|
|
$size = filesize($file); |
|
53
|
|
|
$fileList[] = array( |
|
54
|
|
|
'name' => $file, |
|
55
|
|
|
'size' => $this->getReadableFileSize($size) |
|
56
|
|
|
); |
|
57
|
|
|
$fileTotals['size'] += $size; |
|
58
|
|
|
if($size > $fileTotals['largest']) $fileTotals['largest'] = $size; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$fileTotals['size'] = $this->getReadableFileSize($fileTotals['size']); |
|
62
|
|
|
$fileTotals['largest'] = $this->getReadableFileSize($fileTotals['largest']); |
|
63
|
|
|
|
|
64
|
|
|
return array( |
|
65
|
|
|
'files' => $fileList, |
|
66
|
|
|
'fileTotals' => $fileTotals |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get data about memory usage of the application |
|
72
|
|
|
* |
|
73
|
|
|
* @returns array |
|
74
|
|
|
*/ |
|
75
|
|
|
public function gatherMemoryData() |
|
76
|
|
|
{ |
|
77
|
|
|
$usedMemory = memory_get_peak_usage(); |
|
78
|
|
|
$allowedMemory = ini_get('memory_limit'); |
|
79
|
|
|
return array( |
|
80
|
|
|
'used' => $usedMemory, |
|
81
|
|
|
'allowed' => $allowedMemory |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/*-------------------------------------------------------- |
|
86
|
|
|
QUERY DATA -- DATABASE OBJECT WITH LOGGING REQUIRED |
|
87
|
|
|
----------------------------------------------------------*/ |
|
88
|
|
|
|
|
89
|
|
|
public function gatherQueryData() { |
|
90
|
|
|
$queryTotals = array(); |
|
91
|
|
|
$queryTotals['count'] = 0; |
|
92
|
|
|
$queryTotals['time'] = 0; |
|
93
|
|
|
$queries = array(); |
|
94
|
|
|
|
|
95
|
|
|
if($this->db != '') { |
|
96
|
|
|
$queryTotals['count'] += $this->db->queryCount; |
|
|
|
|
|
|
97
|
|
|
foreach($this->db->queries as $query) { |
|
98
|
|
|
$query = $this->attemptToExplainQuery($query); |
|
99
|
|
|
$queryTotals['time'] += $query['time']; |
|
100
|
|
|
$query['time'] = $this->getReadableTime($query['time']); |
|
101
|
|
|
$queries[] = $query; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$queryTotals['time'] = $this->getReadableTime($queryTotals['time']); |
|
106
|
|
|
$this->output['queries'] = $queries; |
|
|
|
|
|
|
107
|
|
|
$this->output['queryTotals'] = $queryTotals; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/*-------------------------------------------------------- |
|
111
|
|
|
CALL SQL EXPLAIN ON THE QUERY TO FIND MORE INFO |
|
112
|
|
|
----------------------------------------------------------*/ |
|
113
|
|
|
|
|
114
|
|
|
function attemptToExplainQuery($query) { |
|
|
|
|
|
|
115
|
|
|
try { |
|
116
|
|
|
$sql = 'EXPLAIN '.$query['sql']; |
|
117
|
|
|
$rs = $this->db->query($sql); |
|
118
|
|
|
} |
|
119
|
|
|
catch(Exception $e) {} |
|
|
|
|
|
|
120
|
|
|
if($rs) { |
|
121
|
|
|
$row = mysql_fetch_array($rs, MYSQL_ASSOC); |
|
|
|
|
|
|
122
|
|
|
$query['explain'] = $row; |
|
123
|
|
|
} |
|
124
|
|
|
return $query; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get data about speed of the application |
|
129
|
|
|
* |
|
130
|
|
|
* @returns array |
|
131
|
|
|
*/ |
|
132
|
|
|
public function gatherSpeedData() |
|
133
|
|
|
{ |
|
134
|
|
|
$elapsedTime = microtime(true) - $this->startTime; |
|
135
|
|
|
$allowedTime = ini_get('max_execution_time'); |
|
136
|
|
|
return array( |
|
137
|
|
|
'elapsed' => $elapsedTime, |
|
138
|
|
|
'allowed' => $allowedTime |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/*------------------------------------------- |
|
143
|
|
|
HELPER FUNCTIONS TO FORMAT DATA |
|
144
|
|
|
-------------------------------------------*/ |
|
145
|
|
|
|
|
146
|
|
|
public function getReadableFileSize($size, $retstring = null) { |
|
147
|
|
|
// adapted from code at http://aidanlister.com/repos/v/function.size_readable.php |
|
148
|
|
|
$sizes = array('bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); |
|
149
|
|
|
|
|
150
|
|
|
if ($retstring === null) { $retstring = '%01.2f %s'; } |
|
151
|
|
|
|
|
152
|
|
|
$lastsizestring = end($sizes); |
|
153
|
|
|
|
|
154
|
|
|
foreach ($sizes as $sizestring) { |
|
155
|
|
|
if ($size < 1024) { break; } |
|
156
|
|
|
if ($sizestring != $lastsizestring) { $size /= 1024; } |
|
157
|
|
|
} |
|
158
|
|
|
if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional |
|
|
|
|
|
|
159
|
|
|
return sprintf($retstring, $size, $sizestring); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Static formatter for human-readable time |
|
164
|
|
|
* Only handles time up to 60 minutes gracefully |
|
165
|
|
|
* |
|
166
|
|
|
* @param integer $time time in seconds |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
View Code Duplication |
public static function getReadableTime($time) |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
$unit = 's'; |
|
172
|
|
|
|
|
173
|
|
|
if ($time < 1) { |
|
174
|
|
|
$time *= 1000; |
|
175
|
|
|
$unit = 'ms'; |
|
176
|
|
|
} else if ($time > 60) { |
|
177
|
|
|
$time /= 60; |
|
178
|
|
|
$unit = 'm'; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$time = number_format($time, 3); |
|
182
|
|
|
return "{$time} {$unit}"; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Triggers end display of the profiling data |
|
188
|
|
|
* |
|
189
|
|
|
* @param Display $display |
|
190
|
|
|
*/ |
|
191
|
|
|
public function display(Display $display) |
|
192
|
|
|
{ |
|
193
|
|
|
$display->setConsole($this->console); |
|
194
|
|
|
$display->setFileData($this->gatherFileData()); |
|
195
|
|
|
$display->setMemoryData($this->gatherMemoryData()); |
|
196
|
|
|
$display->setQueryData($this->gatherQueryData()); |
|
|
|
|
|
|
197
|
|
|
$display->setSpeedData($this->gatherSpeedData()); |
|
198
|
|
|
|
|
199
|
|
|
$display(); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..