25
25
* @since 1.8.0
26
26
*/
27
27
function perflab_register_default_server_timing_before_template_metrics (): void {
28
- $ calculate_before_template_metrics = static function (): void {
28
+ $ current_function = __FUNCTION__ ;
29
+
30
+ $ calculate_before_template_metrics = static function () use ( $ current_function ): void {
29
31
// WordPress execution prior to serving the template.
30
32
perflab_server_timing_register_metric (
31
33
'before-template ' ,
@@ -44,17 +46,52 @@ function perflab_register_default_server_timing_before_template_metrics(): void
44
46
perflab_server_timing_register_metric (
45
47
'before-template-db-queries ' ,
46
48
array (
47
- 'measure_callback ' => static function ( $ metric ): void {
49
+ 'measure_callback ' => static function ( $ metric ) use ( $ current_function ) : void {
48
50
// This should never happen, but some odd database implementations may be doing it wrong.
49
51
if ( ! isset ( $ GLOBALS ['wpdb ' ]->queries ) || ! is_array ( $ GLOBALS ['wpdb ' ]->queries ) ) {
52
+ wp_trigger_error (
53
+ $ current_function ,
54
+ esc_html (
55
+ sprintf (
56
+ /* translators: 1: before-template-db-queries, 2: $wpdb->queries */
57
+ __ ( 'Unable to compute server timing for "%1$s" because %2$s is not an array. ' , 'performance-lab ' ),
58
+ 'before-template-db-queries ' ,
59
+ '$wpdb->queries '
60
+ )
61
+ )
62
+ );
50
63
return ;
51
64
}
52
65
66
+ /**
67
+ * Query times.
68
+ *
69
+ * @var float[] $query_times
70
+ */
71
+ $ query_times = array ();
72
+ foreach ( $ GLOBALS ['wpdb ' ]->queries as $ query ) {
73
+ if ( ! is_array ( $ query ) || ! isset ( $ query [1 ] ) || ! is_float ( $ query [1 ] ) ) {
74
+ wp_trigger_error (
75
+ $ current_function ,
76
+ esc_html (
77
+ sprintf (
78
+ /* translators: 1: before-template-db-queries, 2: $wpdb->queries */
79
+ __ ( 'Unable to compute server timing for "%1$s" because a saved query in %2$s lacks the elapsed time as the second array value. ' , 'performance-lab ' ),
80
+ 'before-template-db-queries ' ,
81
+ '$wpdb->queries '
82
+ )
83
+ )
84
+ );
85
+ return ;
86
+ }
87
+ $ query_times [] = $ query [1 ];
88
+ }
89
+
53
90
// Store this value in a global to later subtract it from total query time after template.
54
91
$ GLOBALS ['perflab_query_time_before_template ' ] = array_reduce (
55
- $ GLOBALS [ ' wpdb ' ]-> queries ,
56
- static function ( $ acc , $ query ) {
57
- return $ acc + $ query [ 1 ] ;
92
+ $ query_times ,
93
+ static function ( float $ acc , float $ query_time ): float {
94
+ return $ acc + $ query_time ;
58
95
},
59
96
0.0
60
97
);
@@ -155,19 +192,35 @@ static function (): void {
155
192
array (
156
193
'measure_callback ' => static function ( $ metric ): void {
157
194
// This global should typically be set when this is called, but check just in case.
158
- if ( ! isset ( $ GLOBALS ['perflab_query_time_before_template ' ] ) ) {
195
+ if ( ! isset ( $ GLOBALS ['perflab_query_time_before_template ' ] ) || ! is_float ( $ GLOBALS [ ' perflab_query_time_before_template ' ] ) ) {
159
196
return ;
160
197
}
161
198
162
199
// This should never happen, but some odd database implementations may be doing it wrong.
163
200
if ( ! isset ( $ GLOBALS ['wpdb ' ]->queries ) || ! is_array ( $ GLOBALS ['wpdb ' ]->queries ) ) {
201
+ // A notice is already emitted above, but if $perflab_query_time_before_template was not
202
+ // set, then this condition wouldn't be checked in the first place.
164
203
return ;
165
204
}
166
205
206
+ /**
207
+ * Query times.
208
+ *
209
+ * @var float[] $query_times
210
+ */
211
+ $ query_times = array ();
212
+ foreach ( $ GLOBALS ['wpdb ' ]->queries as $ query ) {
213
+ if ( ! is_array ( $ query ) || ! isset ( $ query [1 ] ) || ! is_float ( $ query [1 ] ) ) {
214
+ // A notice is already emitted above.
215
+ return ;
216
+ }
217
+ $ query_times [] = $ query [1 ];
218
+ }
219
+
167
220
$ total_query_time = array_reduce (
168
- $ GLOBALS [ ' wpdb ' ]-> queries ,
169
- static function ( $ acc , $ query ) {
170
- return $ acc + $ query [ 1 ] ;
221
+ $ query_times ,
222
+ static function ( float $ acc , float $ query_time ): float {
223
+ return $ acc + $ query_time ;
171
224
},
172
225
0.0
173
226
);
0 commit comments