Skip to content

Commit d8b4f72

Browse files
committed
Add support for server-timing performance panel extension
Adds support for an additional `start` field in milliseconds. Adds support for special `response-start` and `response-end` fields that should not get a `wp-` prefix. Adds support for an optional `desc`.
1 parent 346d6e1 commit d8b4f72

File tree

3 files changed

+107
-8
lines changed

3 files changed

+107
-8
lines changed

Diff for: plugins/performance-lab/includes/server-timing/class-perflab-server-timing-metric.php

+54-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ class Perflab_Server_Timing_Metric {
3030
private $value;
3131

3232
/**
33-
* The value measured before relevant execution logic in seconds, if used.
33+
* The value measured before relevant execution logic in microseconds, if used.
3434
*
3535
* @since 1.8.0
3636
* @var float|null
3737
*/
3838
private $before_value;
3939

40+
/**
41+
* The metric description, if used.
42+
*
43+
* @since n.e.x.t
44+
* @var string|null
45+
*/
46+
private $description;
47+
4048
/**
4149
* Constructor.
4250
*
@@ -109,6 +117,50 @@ public function get_value() {
109117
return $this->value;
110118
}
111119

120+
/**
121+
* Sets the start time.
122+
*
123+
* @since n.e.x.t
124+
*
125+
* @param float $value Start time in microseconds.
126+
*/
127+
public function set_start_time( float $value ): void {
128+
$this->before_value = $value;
129+
}
130+
131+
/**
132+
* Returns the start time if set.
133+
*
134+
* @since n.e.x.t
135+
*
136+
* @return float|null Start time if set.
137+
*/
138+
public function get_start_time(): ?float {
139+
return $this->before_value;
140+
}
141+
142+
/**
143+
* Sets the metric description.
144+
*
145+
* @since n.e.x.t
146+
*
147+
* @param string $description Description.
148+
*/
149+
public function set_description( string $description ): void {
150+
$this->description = $description;
151+
}
152+
153+
/**
154+
* Returns the metric description.
155+
*
156+
* @since n.e.x.t
157+
*
158+
* @return string|null Description if set.
159+
*/
160+
public function get_description(): ?string {
161+
return $this->description;
162+
}
163+
112164
/**
113165
* Captures the current time, as a reference point to calculate the duration of a task afterward.
114166
*
@@ -118,7 +170,7 @@ public function get_value() {
118170
* @since 1.8.0
119171
*/
120172
public function measure_before(): void {
121-
$this->before_value = microtime( true );
173+
$this->set_start_time( microtime( true ) );
122174
}
123175

124176
/**

Diff for: plugins/performance-lab/includes/server-timing/class-perflab-server-timing.php

+30-6
Original file line numberDiff line numberDiff line change
@@ -288,19 +288,43 @@ function ( string $output, ?int $phase ): string {
288288
*/
289289
private function format_metric_header_value( Perflab_Server_Timing_Metric $metric ): ?string {
290290
$value = $metric->get_value();
291-
292-
// If no value is set, make sure it's just passed through.
293-
if ( null === $value ) {
294-
return null;
295-
}
291+
$start = $metric->get_start_time();
292+
$desc = $metric->get_description();
296293

297294
if ( is_float( $value ) ) {
298295
$value = round( $value, 2 );
299296
}
300297

298+
if ( is_float( $start ) ) {
299+
$start = round( $start, 2 ) * 1000.00;
300+
}
301+
302+
// If no value is set, make sure it's just passed through.
303+
if ( null === $start && null === $value ) {
304+
return null;
305+
}
306+
301307
// See https://github.com/WordPress/performance/issues/955.
302308
$name = preg_replace( '/[^!#$%&\'*+\-.^_`|~0-9a-zA-Z]/', '-', $metric->get_slug() );
303309

304-
return sprintf( 'wp-%1$s;dur=%2$s', $name, $value );
310+
if ( 'response-start' !== $name && 'response-end' !== $name ) {
311+
$name = 'wp-' . $name;
312+
}
313+
314+
$header_value = sprintf( '%s', $name );
315+
316+
if ( null !== $value ) {
317+
$header_value .= sprintf( ';dur=%s', $value );
318+
}
319+
320+
if ( null !== $start ) {
321+
$header_value .= sprintf( ';start=%s', $start );
322+
}
323+
324+
if ( null !== $desc ) {
325+
$header_value .= sprintf( ';desc=%s', $desc );
326+
}
327+
328+
return $header_value;
305329
}
306330
}

Diff for: plugins/performance-lab/includes/server-timing/defaults.php

+23
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function perflab_register_default_server_timing_before_template_metrics(): void
3131
'measure_callback' => static function ( $metric ): void {
3232
// The 'timestart' global is set right at the beginning of WordPress execution.
3333
$metric->set_value( ( microtime( true ) - $GLOBALS['timestart'] ) * 1000.0 );
34+
$metric->set_start_time( $GLOBALS['timestart'] );
3435
},
3536
'access_cap' => 'exist',
3637
)
@@ -135,6 +136,27 @@ static function (): void {
135136
'measure_callback' => static function ( $metric ): void {
136137
// The 'timestart' global is set right at the beginning of WordPress execution.
137138
$metric->set_value( ( microtime( true ) - $GLOBALS['timestart'] ) * 1000.0 );
139+
$metric->set_start_time( $GLOBALS['timestart'] );
140+
},
141+
'access_cap' => 'exist',
142+
)
143+
);
144+
145+
perflab_server_timing_register_metric(
146+
'response-start',
147+
array(
148+
'measure_callback' => static function ( $metric ): void {
149+
$metric->set_start_time( $GLOBALS['timestart'] );
150+
},
151+
'access_cap' => 'exist',
152+
)
153+
);
154+
155+
perflab_server_timing_register_metric(
156+
'response-end',
157+
array(
158+
'measure_callback' => static function ( $metric ): void {
159+
$metric->set_start_time( microtime( true ) );
138160
},
139161
'access_cap' => 'exist',
140162
)
@@ -170,6 +192,7 @@ static function ( $acc, $query ) {
170192
0.0
171193
);
172194
$metric->set_value( ( $total_query_time - $GLOBALS['perflab_query_time_before_template'] ) * 1000.0 );
195+
$metric->set_start_time( $GLOBALS['perflab_query_time_before_template'] );
173196
},
174197
'access_cap' => 'exist',
175198
)

0 commit comments

Comments
 (0)