|
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
|
|
|
* Get data about files loaded for the application to current point |
|
40
|
|
|
* |
|
41
|
|
|
* @returns array |
|
42
|
|
|
*/ |
|
43
|
|
|
public function gatherFileData() |
|
44
|
|
|
{ |
|
45
|
|
|
$files = get_included_files(); |
|
46
|
|
|
$data = array(); |
|
47
|
|
|
foreach ($files as $file) { |
|
48
|
|
|
array_push($data, array( |
|
49
|
|
|
'name' => $file, |
|
50
|
|
|
'size' => filesize($file) |
|
51
|
|
|
)); |
|
52
|
|
|
} |
|
53
|
|
|
return $data; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get data about memory usage of the application |
|
58
|
|
|
* |
|
59
|
|
|
* @returns array |
|
60
|
|
|
*/ |
|
61
|
|
|
public function gatherMemoryData() |
|
62
|
|
|
{ |
|
63
|
|
|
$usedMemory = memory_get_peak_usage(); |
|
64
|
|
|
$allowedMemory = ini_get('memory_limit'); |
|
65
|
|
|
return array( |
|
66
|
|
|
'used' => $usedMemory, |
|
67
|
|
|
'allowed' => $allowedMemory |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/*-------------------------------------------------------- |
|
72
|
|
|
QUERY DATA -- DATABASE OBJECT WITH LOGGING REQUIRED |
|
73
|
|
|
----------------------------------------------------------*/ |
|
74
|
|
|
|
|
75
|
|
|
public function gatherQueryData() { |
|
76
|
|
|
$queryTotals = array(); |
|
77
|
|
|
$queryTotals['count'] = 0; |
|
78
|
|
|
$queryTotals['time'] = 0; |
|
79
|
|
|
$queries = array(); |
|
80
|
|
|
|
|
81
|
|
|
if($this->db != '') { |
|
82
|
|
|
$queryTotals['count'] += $this->db->queryCount; |
|
|
|
|
|
|
83
|
|
|
foreach($this->db->queries as $query) { |
|
84
|
|
|
$query = $this->attemptToExplainQuery($query); |
|
85
|
|
|
$queryTotals['time'] += $query['time']; |
|
86
|
|
|
$query['time'] = Display::getReadableTime($query['time']); |
|
87
|
|
|
$queries[] = $query; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$queryTotals['time'] = Display::getReadableTime($queryTotals['time']); |
|
92
|
|
|
$this->output['queries'] = $queries; |
|
|
|
|
|
|
93
|
|
|
$this->output['queryTotals'] = $queryTotals; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/*-------------------------------------------------------- |
|
97
|
|
|
CALL SQL EXPLAIN ON THE QUERY TO FIND MORE INFO |
|
98
|
|
|
----------------------------------------------------------*/ |
|
99
|
|
|
|
|
100
|
|
|
function attemptToExplainQuery($query) { |
|
|
|
|
|
|
101
|
|
|
try { |
|
102
|
|
|
$sql = 'EXPLAIN '.$query['sql']; |
|
103
|
|
|
$rs = $this->db->query($sql); |
|
104
|
|
|
} |
|
105
|
|
|
catch(Exception $e) {} |
|
|
|
|
|
|
106
|
|
|
if($rs) { |
|
107
|
|
|
$row = mysql_fetch_array($rs, MYSQL_ASSOC); |
|
|
|
|
|
|
108
|
|
|
$query['explain'] = $row; |
|
109
|
|
|
} |
|
110
|
|
|
return $query; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get data about speed of the application |
|
115
|
|
|
* |
|
116
|
|
|
* @returns array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function gatherSpeedData() |
|
119
|
|
|
{ |
|
120
|
|
|
$elapsedTime = microtime(true) - $this->startTime; |
|
121
|
|
|
$allowedTime = ini_get('max_execution_time'); |
|
122
|
|
|
return array( |
|
123
|
|
|
'elapsed' => $elapsedTime, |
|
124
|
|
|
'allowed' => $allowedTime |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Triggers end display of the profiling data |
|
130
|
|
|
* |
|
131
|
|
|
* @param Display $display |
|
132
|
|
|
*/ |
|
133
|
|
|
public function display(Display $display) |
|
134
|
|
|
{ |
|
135
|
|
|
$display->setConsole($this->console); |
|
136
|
|
|
$display->setFileData($this->gatherFileData()); |
|
137
|
|
|
$display->setMemoryData($this->gatherMemoryData()); |
|
138
|
|
|
$display->setQueryData($this->gatherQueryData()); |
|
|
|
|
|
|
139
|
|
|
$display->setSpeedData($this->gatherSpeedData()); |
|
140
|
|
|
|
|
141
|
|
|
$display(); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
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..