TimerPanel.php
4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit\Panel;
use Cake\I18n\Number;
use DebugKit\DebugMemory;
use DebugKit\DebugPanel;
use DebugKit\DebugTimer;
/**
* Provides debug information on all timers used in a request.
*/
class TimerPanel extends DebugPanel
{
/**
* Return an array of events to listen to.
*
* @return array
*/
public function implementedEvents()
{
$before = function ($name) {
return function () use ($name) {
DebugTimer::start($name, __d('debug_kit', $name));
};
};
$after = function ($name) {
return function () use ($name) {
DebugTimer::stop($name);
};
};
$both = function ($name) use ($before, $after) {
return [
['priority' => 0, 'callable' => $before('Event: ' . $name)],
['priority' => 999, 'callable' => $after('Event: ' . $name)]
];
};
return [
'Controller.initialize' => [
['priority' => 0, 'callable' => function () {
DebugMemory::record(__d('debug_kit', 'Controller initialization'));
}],
['priority' => 0, 'callable' => $before('Event: Controller.initialize')],
['priority' => 999, 'callable' => $after('Event: Controller.initialize')]
],
'Controller.startup' => [
['priority' => 0, 'callable' => $before('Event: Controller.startup')],
['priority' => 999, 'callable' => $after('Event: Controller.startup')],
['priority' => 999, 'callable' => function () {
DebugMemory::record(__d('debug_kit', 'Controller action start'));
DebugTimer::start(__d('debug_kit', 'Controller action'));
}],
],
'Controller.beforeRender' => [
['priority' => 0, 'callable' => function () {
DebugTimer::stop(__d('debug_kit', 'Controller action'));
}],
['priority' => 0, 'callable' => $before('Event: Controller.beforeRender')],
['priority' => 999, 'callable' => $after('Event: Controller.beforeRender')],
['priority' => 999, 'callable' => function () {
DebugMemory::record(__d('debug_kit', 'View Render start'));
DebugTimer::start(__d('debug_kit', 'View Render start'));
}],
],
'View.beforeRender' => $both('View.beforeRender'),
'View.afterRender' => $both('View.afterRender'),
'View.beforeLayout' => $both('View.beforeLayout'),
'View.afterLayout' => $both('View.afterLayout'),
'View.beforeRenderFile' => [
['priority' => 0, 'callable' => function ($event, $filename) {
DebugTimer::start(__d('debug_kit', 'Render {0}', $filename));
}],
],
'View.afterRenderFile' => [
['priority' => 0, 'callable' => function ($event, $filename) {
DebugTimer::stop(__d('debug_kit', 'Render {0}', $filename));
}],
],
'Controller.shutdown' => [
['priority' => 0, 'callable' => $before('Event: Controller.shutdown')],
['priority' => 0, 'callable' => function () {
DebugTimer::stop(__d('debug_kit', 'View Render start'));
DebugMemory::record(__d('debug_kit', 'Controller shutdown'));
}],
['priority' => 999, 'callable' => $after('Event: Controller.shutdown')],
],
];
}
/**
* Get the data for the panel.
*
* @return array
*/
public function data()
{
return [
'requestTime' => DebugTimer::requestTime(),
'timers' => DebugTimer::getAll(),
'memory' => DebugMemory::getAll(),
'peakMemory' => DebugMemory::getPeak(),
];
}
/**
* Get the summary for the panel.
*
* @return string
*/
public function summary()
{
$time = Number::precision(DebugTimer::requestTime(), 2) . ' s';
$memory = Number::toReadableSize(DebugMemory::getPeak());
return "$time / $memory";
}
}