|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains a data collector for the profiler |
|
4
|
|
|
* |
|
5
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace BZIon\Debug; |
|
9
|
|
|
|
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A data collector that collects database data that will be shown on the |
|
16
|
|
|
* profiler |
|
17
|
|
|
*/ |
|
18
|
|
|
class DatabaseDataCollector implements DataCollectorInterface |
|
19
|
|
|
{ |
|
20
|
|
|
public $data; |
|
21
|
|
|
protected $queries = array(); |
|
22
|
|
|
protected $cacheFetches = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Collects data for the given Request and Response, so that it can be |
|
26
|
|
|
* serialized and stored for later retrieval |
|
27
|
|
|
* |
|
28
|
|
|
* @param Request $request A Request instance |
|
29
|
|
|
* @param Response $response A Response instance |
|
30
|
|
|
* @param \Exception $exception An Exception instance |
|
31
|
|
|
*/ |
|
32
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->data = array( |
|
35
|
|
|
'queries' => $this->queries, |
|
36
|
|
|
'cachedModels' => \Service::getModelCache()->all(), |
|
37
|
|
|
'cacheFetches' => $this->cacheFetches |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Log a database query |
|
43
|
|
|
* |
|
44
|
|
|
* @param DatabaseQuery $query The query |
|
45
|
|
|
*/ |
|
46
|
|
|
public function logQuery($query) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->queries[] = $query; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Log a fetch from the Model Cache |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $type The type of the model |
|
55
|
|
|
* @param int $id The ID of the model |
|
56
|
|
|
*/ |
|
57
|
|
|
public function logCacheFetch($type, $id) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
if (isset($this->cacheFetches[$type])) { |
|
60
|
|
|
++$this->cacheFetches[$type]; |
|
61
|
|
|
} else { |
|
62
|
|
|
$this->cacheFetches[$type] = 1; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the sorted catche fetches by Model type |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getCacheFetches() |
|
72
|
|
|
{ |
|
73
|
|
|
arsort($this->data['cacheFetches']); |
|
74
|
|
|
return $this->data['cacheFetches']; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the total number of catche fetches |
|
79
|
|
|
* |
|
80
|
|
|
* @return int |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getTotalCacheFetches() |
|
83
|
|
|
{ |
|
84
|
|
|
return array_sum($this->data['cacheFetches']); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get an estimate of the time that the model cache saved in milliseconds |
|
89
|
|
|
* |
|
90
|
|
|
* @return number |
|
91
|
|
|
*/ |
|
92
|
|
|
public function estimateTimeSaved() |
|
93
|
|
|
{ |
|
94
|
|
|
$sum = 0; |
|
95
|
|
|
$count = 0; |
|
96
|
|
|
|
|
97
|
|
|
foreach ($this->data['queries'] as $query) { |
|
98
|
|
|
if (0 === strpos(trim($query->getQuery()), 'SELECT *')) { |
|
99
|
|
|
$sum += $query->getDuration(); |
|
100
|
|
|
++$count; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ($count == 0) { |
|
105
|
|
|
return 0; |
|
106
|
|
|
} else { |
|
107
|
|
|
return array_sum($this->data['cacheFetches']) * $sum / $count / 1000; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get the number of times each query was sent to the server |
|
113
|
|
|
* |
|
114
|
|
|
* @return array An array with resolved queries as keys and frequencies as values |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getQueryFrequencies() |
|
117
|
|
|
{ |
|
118
|
|
|
$return = array(); |
|
119
|
|
|
|
|
120
|
|
|
foreach ($this->data['queries'] as $query) { |
|
121
|
|
|
$resolved = $query->getResolvedQuery(); |
|
122
|
|
|
|
|
123
|
|
|
if (!isset($return[$resolved])) { |
|
124
|
|
|
$return[$resolved] = 1; |
|
125
|
|
|
} else { |
|
126
|
|
|
++$return[$resolved]; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $return; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get the number of duplicated queries |
|
135
|
|
|
* |
|
136
|
|
|
* @return int |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getDuplicatedQueryCount() |
|
139
|
|
|
{ |
|
140
|
|
|
$frequencies = $this->getQueryFrequencies(); |
|
141
|
|
|
|
|
142
|
|
|
return array_sum($frequencies) - count($frequencies); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get the queries made to the database |
|
147
|
|
|
* |
|
148
|
|
|
* @return DatabaseQuery[] |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getQueries() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->data['queries']; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get the total duration of the database queries in milliseconds |
|
157
|
|
|
* |
|
158
|
|
|
* @return float |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getDuration() |
|
161
|
|
|
{ |
|
162
|
|
|
$sum = 0; |
|
163
|
|
|
|
|
164
|
|
|
foreach ($this->data['queries'] as $query) { |
|
165
|
|
|
$sum += $query->getDuration(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $sum / 1000; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Returns the name of the collector. |
|
173
|
|
|
* |
|
174
|
|
|
* @return string The collector name |
|
175
|
|
|
*/ |
|
176
|
2 |
|
public function getName() |
|
177
|
|
|
{ |
|
178
|
2 |
|
return 'database'; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.