Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add support for server-timing performance panel extension #1487

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ class Perflab_Server_Timing_Metric {
private $value;

/**
* The value measured before relevant execution logic in seconds, if used.
* The value measured before relevant execution logic in microseconds, if used.
*
* @since 1.8.0
* @var float|null
*/
private $before_value;

/**
* The metric description, if used.
*
* @since n.e.x.t
* @var string|null
*/
private $description;

/**
* Constructor.
*
Expand Down Expand Up @@ -109,6 +117,50 @@ public function get_value() {
return $this->value;
}

/**
* Sets the start time.
*
* @since n.e.x.t
*
* @param float $value Start time in microseconds.
*/
public function set_start_time( float $value ): void {
$this->before_value = $value;
}

/**
* Returns the start time if set.
*
* @since n.e.x.t
*
* @return float|null Start time if set.
*/
public function get_start_time(): ?float {
return $this->before_value;
}

/**
* Sets the metric description.
*
* @since n.e.x.t
*
* @param string $description Description.
*/
public function set_description( string $description ): void {
$this->description = $description;
}

/**
* Returns the metric description.
*
* @since n.e.x.t
*
* @return string|null Description if set.
*/
public function get_description(): ?string {
return $this->description;
}

/**
* Captures the current time, as a reference point to calculate the duration of a task afterward.
*
Expand All @@ -118,7 +170,7 @@ public function get_value() {
* @since 1.8.0
*/
public function measure_before(): void {
$this->before_value = microtime( true );
$this->set_start_time( microtime( true ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,43 @@ function ( string $output, ?int $phase ): string {
*/
private function format_metric_header_value( Perflab_Server_Timing_Metric $metric ): ?string {
$value = $metric->get_value();

// If no value is set, make sure it's just passed through.
if ( null === $value ) {
return null;
}
$start = $metric->get_start_time();
$desc = $metric->get_description();

if ( is_float( $value ) ) {
$value = round( $value, 2 );
}

if ( is_float( $start ) ) {
$start = round( $start, 2 ) * 1000.00;
}

// If no value is set, make sure it's just passed through.
if ( null === $start && null === $value ) {
return null;
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
}

// See https://github.com/WordPress/performance/issues/955.
$name = preg_replace( '/[^!#$%&\'*+\-.^_`|~0-9a-zA-Z]/', '-', $metric->get_slug() );

return sprintf( 'wp-%1$s;dur=%2$s', $name, $value );
if ( 'response-start' !== $name && 'response-end' !== $name ) {
$name = 'wp-' . $name;
}

$header_value = sprintf( '%s', $name );

if ( null !== $value ) {
$header_value .= sprintf( ';dur=%s', $value );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
}

if ( null !== $start ) {
$header_value .= sprintf( ';start=%s', $start );
}

if ( null !== $desc ) {
$header_value .= sprintf( ';desc=%s', $desc );
}

return $header_value;
}
}
23 changes: 23 additions & 0 deletions plugins/performance-lab/includes/server-timing/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function perflab_register_default_server_timing_before_template_metrics(): void
'measure_callback' => static function ( $metric ): void {
// The 'timestart' global is set right at the beginning of WordPress execution.
$metric->set_value( ( microtime( true ) - $GLOBALS['timestart'] ) * 1000.0 );
$metric->set_start_time( $GLOBALS['timestart'] );
},
'access_cap' => 'exist',
)
Expand Down Expand Up @@ -135,6 +136,27 @@ static function (): void {
'measure_callback' => static function ( $metric ): void {
// The 'timestart' global is set right at the beginning of WordPress execution.
$metric->set_value( ( microtime( true ) - $GLOBALS['timestart'] ) * 1000.0 );
$metric->set_start_time( $GLOBALS['timestart'] );
},
'access_cap' => 'exist',
)
);

perflab_server_timing_register_metric(
'response-start',
array(
'measure_callback' => static function ( $metric ): void {
$metric->set_start_time( $GLOBALS['timestart'] );
},
'access_cap' => 'exist',
)
);

perflab_server_timing_register_metric(
'response-end',
array(
'measure_callback' => static function ( $metric ): void {
$metric->set_start_time( microtime( true ) );
},
'access_cap' => 'exist',
)
Expand Down Expand Up @@ -170,6 +192,7 @@ static function ( $acc, $query ) {
0.0
);
$metric->set_value( ( $total_query_time - $GLOBALS['perflab_query_time_before_template'] ) * 1000.0 );
$metric->set_start_time( $GLOBALS['perflab_query_time_before_template'] );
},
'access_cap' => 'exist',
)
Expand Down