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