|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webperf plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Monitor the performance of your webpages through real-world user timing data |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\webperf\log; |
|
12
|
|
|
|
|
13
|
|
|
use nystudio107\webperf\Webperf; |
|
14
|
|
|
|
|
15
|
|
|
use yii\log\Target; |
|
|
|
|
|
|
16
|
|
|
use yii\log\Logger; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
|
|
|
|
|
19
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
20
|
|
|
* @package Webperf |
|
|
|
|
|
|
21
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
|
|
23
|
|
|
class ProfileTarget extends Target |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
const PROFILE_CATEGORIES = [ |
|
27
|
|
|
'database' => [ |
|
28
|
|
|
'prefix' => 'yii\db', |
|
29
|
|
|
], |
|
30
|
|
|
'twig' => [ |
|
31
|
|
|
'prefix' => 'craft\web\twig\Template', |
|
32
|
|
|
], |
|
33
|
|
|
'other' => [ |
|
34
|
|
|
'prefix' => 'webperf-other', |
|
35
|
|
|
], |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
// Public Properties |
|
39
|
|
|
// ========================================================================= |
|
40
|
|
|
|
|
41
|
|
|
public $stats = []; |
|
42
|
|
|
|
|
43
|
|
|
// Public Methods |
|
44
|
|
|
// ========================================================================= |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
|
|
|
|
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
|
|
|
|
|
49
|
|
|
public function init() |
|
50
|
|
|
{ |
|
51
|
|
|
parent::init(); |
|
52
|
|
|
foreach (self::PROFILE_CATEGORIES as $key => $value) { |
|
53
|
|
|
$this->stats[$key] = [ |
|
54
|
|
|
'count' => 0, |
|
55
|
|
|
'duration' => 0.0, |
|
56
|
|
|
'memory' => 0, |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Processes the given log messages. |
|
63
|
|
|
* This method will filter the given messages with [[levels]] and [[categories]]. |
|
64
|
|
|
* And if requested, it will also export the filtering result to specific medium (e.g. email). |
|
65
|
|
|
* @param array $messages log messages to be processed. See [[Logger::messages]] for the structure |
|
|
|
|
|
|
66
|
|
|
* of each message. |
|
|
|
|
|
|
67
|
|
|
* @param bool $final whether this method is called at the end of the current application |
|
|
|
|
|
|
68
|
|
|
*/ |
|
|
|
|
|
|
69
|
|
|
public function collect($messages, $final) |
|
70
|
|
|
{ |
|
71
|
|
|
// Merge in any messages intended for us |
|
72
|
|
|
$this->messages = array_merge( |
|
|
|
|
|
|
73
|
|
|
$this->messages, |
|
74
|
|
|
static::filterMessages($messages, $this->getLevels(), $this->categories, $this->except) |
|
75
|
|
|
); |
|
76
|
|
|
// Calculate the timings from the messages, and then reset them |
|
77
|
|
|
$timings = $this->calculateTimings($this->messages); |
|
78
|
|
|
$this->messages = []; |
|
79
|
|
|
// Loop through and tally up all of the timings |
|
80
|
|
|
foreach ($timings as $timing) { |
|
81
|
|
|
$cat = 'other'; |
|
82
|
|
|
foreach (self::PROFILE_CATEGORIES as $key => $value) { |
|
83
|
|
|
if (strpos($timing['category'], $value['prefix']) === 0) { |
|
84
|
|
|
$cat = $key; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
$this->stats[$cat]['count']++; |
|
88
|
|
|
$this->stats[$cat]['duration'] += (float)$timing['duration'] * 1000; |
|
89
|
|
|
$this->stats[$cat]['memory'] += (int)$timing['memoryDiff']; |
|
90
|
|
|
} |
|
91
|
|
|
if ($final) { |
|
92
|
|
|
$this->export(); |
|
93
|
|
|
Webperf::$plugin->beacons->includeCraftBeacon(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
|
|
|
|
|
98
|
|
|
* @inheritdoc |
|
99
|
|
|
*/ |
|
|
|
|
|
|
100
|
|
|
public function export() |
|
101
|
|
|
{ |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Protected Methods |
|
105
|
|
|
// ========================================================================= |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Calculates the elapsed time for the given log messages. |
|
109
|
|
|
* @param array $messages the log messages obtained from profiling |
|
|
|
|
|
|
110
|
|
|
* @return array timings. Each element is an array consisting of these elements: |
|
|
|
|
|
|
111
|
|
|
* `info`, `category`, `timestamp`, `trace`, `level`, `duration`, `memory`, `memoryDiff`. |
|
112
|
|
|
* The `memory` and `memoryDiff` values are available since version 2.0.11. |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function calculateTimings($messages): array |
|
115
|
|
|
{ |
|
116
|
|
|
$timings = []; |
|
117
|
|
|
$stack = []; |
|
118
|
|
|
|
|
119
|
|
|
foreach ($messages as $i => $log) { |
|
120
|
|
|
list($token, $level, $category, $timestamp, $traces) = $log; |
|
121
|
|
|
$memory = isset($log[5]) ? $log[5] : 0; |
|
122
|
|
|
$log[6] = $i; |
|
123
|
|
|
$hash = md5(json_encode($token)); |
|
124
|
|
|
if ($level == Logger::LEVEL_PROFILE_BEGIN) { |
|
125
|
|
|
$stack[$hash] = $log; |
|
126
|
|
|
} elseif ($level == Logger::LEVEL_PROFILE_END) { |
|
127
|
|
|
if (isset($stack[$hash])) { |
|
128
|
|
|
$timings[$stack[$hash][6]] = [ |
|
129
|
|
|
'category' => $stack[$hash][2], |
|
130
|
|
|
'timestamp' => $stack[$hash][3], |
|
131
|
|
|
'level' => count($stack) - 1, |
|
132
|
|
|
'duration' => $timestamp - $stack[$hash][3], |
|
133
|
|
|
'memory' => $memory, |
|
134
|
|
|
'memoryDiff' => $memory - (isset($stack[$hash][5]) ? $stack[$hash][5] : 0), |
|
135
|
|
|
]; |
|
136
|
|
|
unset($stack[$hash]); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return array_values($timings); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|