Moved SQL events to a provider and simplified template.
This commit is contained in:
1 parent
31b80d0413
commit
d8febfeb3a
3 files changed
+155
-112
No files matched your search
@@ -0,0 +1,124 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rarst\Laps\Events;
|
||||||
|
|
||||||
|
use Pimple\Container;
|
||||||
|
use Pimple\ServiceProviderInterface;
|
||||||
|
use Rarst\Laps\Bootable_Provider_Interface;
|
||||||
|
use Rarst\Laps\Laps;
|
||||||
|
|
||||||
|
class Sql_Events_Provider implements ServiceProviderInterface, Bootable_Provider_Interface, Events_Provider_Interface {
|
||||||
|
|
||||||
|
protected $query_starts = [];
|
||||||
|
|
||||||
|
public function register( Container $pimple ) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function boot( Laps $laps ) {
|
||||||
|
|
||||||
|
if ( $this->is_savequeries() ) {
|
||||||
|
add_filter( 'query', [ $this, 'query' ], 20 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Capture SQL queries start times
|
||||||
|
*
|
||||||
|
* @param string $query
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function query( $query ) {
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
if ( empty( $this->query_starts ) && ! empty( $wpdb->queries ) ) {
|
||||||
|
$this->query_starts[ count( $wpdb->queries ) ] = microtime( true ) * 1000;
|
||||||
|
} else {
|
||||||
|
$this->query_starts[] = microtime( true ) * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_events() {
|
||||||
|
|
||||||
|
if ( ! $this->is_savequeries() ) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$query_data = [];
|
||||||
|
$last_query_end = 0;
|
||||||
|
|
||||||
|
foreach ( $wpdb->queries as $key => list( $sql, $duration ) ) {
|
||||||
|
$query_start = isset( $this->query_starts[ $key ] ) ? $this->query_starts[ $key ] : $last_query_end;
|
||||||
|
$sql = trim( $sql );
|
||||||
|
$category = 'query-read';
|
||||||
|
|
||||||
|
if ( 0 === stripos( $sql, 'INSERT' ) || 0 === stripos( $sql, 'UPDATE' ) ) {
|
||||||
|
$category = 'query-write';
|
||||||
|
}
|
||||||
|
|
||||||
|
$duration *= 1000;
|
||||||
|
$last_query_end = $query_start + $duration;
|
||||||
|
|
||||||
|
$name = $sql;
|
||||||
|
$description = $name;
|
||||||
|
$origin = $query_start;
|
||||||
|
|
||||||
|
$query_data[] = compact( 'name', 'description', 'origin', 'duration', 'category' );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function is_savequeries() {
|
||||||
|
|
||||||
|
return defined( 'SAVEQUERIES' ) && SAVEQUERIES;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO revisit query stacking if necessary after timeline logic upgrade.
|
||||||
|
// $query_data = array();
|
||||||
|
// $last_query_end = 0;
|
||||||
|
// $last_offset = 0;
|
||||||
|
// $last_duration = 0;
|
||||||
|
|
||||||
|
// if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
|
||||||
|
//
|
||||||
|
// foreach ( $wpdb->queries as $key => $query ) {
|
||||||
|
// $query_start = isset( $this->query_starts[ $key ] ) ? $this->query_starts[ $key ] : $last_query_end;
|
||||||
|
// list( $sql, $duration, $trace ) = $query;
|
||||||
|
// $sql = trim( $sql );
|
||||||
|
// $category = 'query-read';
|
||||||
|
//
|
||||||
|
// if ( 0 === stripos( $sql, 'INSERT' ) || 0 === stripos( $sql, 'UPDATE' ) ) {
|
||||||
|
// $category = 'query-write';
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// $duration *= 1000;
|
||||||
|
// $last_query_end = $query_start + $duration;
|
||||||
|
// $offset = round( ( $query_start - $start ) / $total * 100, 2 );
|
||||||
|
//
|
||||||
|
// // if query is indistinguishably close to previous then stack it
|
||||||
|
// if ( $offset === $last_offset ) {
|
||||||
|
// $key = count( $query_data ) - 1;
|
||||||
|
// $query_data[ $key ]['sql'] .= '<br />' . $sql;
|
||||||
|
//
|
||||||
|
// $last_duration += $duration;
|
||||||
|
// $width = round( $last_duration / $total * 100, 2 );
|
||||||
|
// $query_data[ $key ]['width'] = $width;
|
||||||
|
//
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// $width = round( $duration / $total * 100, 2 );
|
||||||
|
// $last_offset = $offset;
|
||||||
|
// $last_duration = $duration;
|
||||||
|
//
|
||||||
|
// $query_data[] = compact( 'sql', 'duration', 'offset', 'width', 'category' );
|
||||||
|
// }
|
||||||
|
// }
|
||||||
+27
-90
@@ -7,6 +7,7 @@ use Pimple\ServiceProviderInterface;
|
|||||||
use Rarst\Laps\Events\Events_Provider_Interface;
|
use Rarst\Laps\Events\Events_Provider_Interface;
|
||||||
use Rarst\Laps\Events\Hook_Events_Provider;
|
use Rarst\Laps\Events\Hook_Events_Provider;
|
||||||
use Rarst\Laps\Events\Http_Events_Provider;
|
use Rarst\Laps\Events\Http_Events_Provider;
|
||||||
|
use Rarst\Laps\Events\Sql_Events_Provider;
|
||||||
use Symfony\Component\Stopwatch\Stopwatch;
|
use Symfony\Component\Stopwatch\Stopwatch;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,6 +38,7 @@ class Laps extends Container {
|
|||||||
|
|
||||||
$laps->register( new Hook_Events_Provider() );
|
$laps->register( new Hook_Events_Provider() );
|
||||||
$laps->register( new Http_Events_Provider() );
|
$laps->register( new Http_Events_Provider() );
|
||||||
|
$laps->register( new Sql_Events_Provider() );
|
||||||
|
|
||||||
foreach ( $values as $key => $value ) {
|
foreach ( $values as $key => $value ) {
|
||||||
$this->offsetSet( $key, $value );
|
$this->offsetSet( $key, $value );
|
||||||
@@ -73,10 +75,6 @@ class Laps extends Container {
|
|||||||
add_action( 'pre_update_option_active_plugins', array( $this, 'pre_update_option_active_plugins' ) );
|
add_action( 'pre_update_option_active_plugins', array( $this, 'pre_update_option_active_plugins' ) );
|
||||||
add_action( 'pre_update_site_option_active_sitewide_plugins', array( $this, 'pre_update_option_active_plugins' ) );
|
add_action( 'pre_update_site_option_active_sitewide_plugins', array( $this, 'pre_update_option_active_plugins' ) );
|
||||||
add_action( 'init', array( $this, 'init' ) );
|
add_action( 'init', array( $this, 'init' ) );
|
||||||
|
|
||||||
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
|
|
||||||
add_filter( 'query', array( $this, 'query' ), 20 );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -101,26 +99,6 @@ class Laps extends Container {
|
|||||||
return $plugins;
|
return $plugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Capture SQL queries start times
|
|
||||||
*
|
|
||||||
* @param string $query
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function query( $query ) {
|
|
||||||
|
|
||||||
global $wpdb;
|
|
||||||
|
|
||||||
if ( empty( $this->query_starts ) && ! empty( $wpdb->queries ) ) {
|
|
||||||
$this->query_starts[ count( $wpdb->queries ) ] = microtime( true ) * 1000;
|
|
||||||
} else {
|
|
||||||
$this->query_starts[] = microtime( true ) * 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $query;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function init() {
|
public function init() {
|
||||||
|
|
||||||
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
||||||
@@ -151,7 +129,7 @@ class Laps extends Container {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
global $timestart, $wpdb;
|
global $timestart;
|
||||||
|
|
||||||
/** @var Stopwatch $stopwatch */
|
/** @var Stopwatch $stopwatch */
|
||||||
$stopwatch = $this['stopwatch'];
|
$stopwatch = $this['stopwatch'];
|
||||||
@@ -173,85 +151,44 @@ class Laps extends Container {
|
|||||||
$start = $timestart * 1000;
|
$start = $timestart * 1000;
|
||||||
$end = microtime( true ) * 1000;
|
$end = microtime( true ) * 1000;
|
||||||
$total = $end - $start;
|
$total = $end - $start;
|
||||||
$event_data = array();
|
$event_data = [];
|
||||||
$http_data = array();
|
$http_data = [];
|
||||||
|
$query_data = [];
|
||||||
|
|
||||||
foreach ( $events as $event ) {
|
foreach ( $events as $event ) {
|
||||||
|
|
||||||
$name = $event['name'];
|
$event['offset'] = round( ( $event['origin'] - $start ) / $total * 100, 2 );
|
||||||
$offset = round( ( $event['origin'] - $start ) / $total * 100, 2 );
|
$event['width'] = round( $event['duration'] / $total * 100, 2 );
|
||||||
$duration = $event['duration'];
|
|
||||||
$width = round( $duration / $total * 100, 2 );
|
|
||||||
$category = $event['category'];
|
|
||||||
|
|
||||||
if ( 'http' === $category ) {
|
switch ($event['category']) {
|
||||||
$http_data[] = compact( 'name', 'offset', 'duration', 'width', 'category' );
|
case 'http':
|
||||||
continue;
|
$http_data[] = $event;
|
||||||
|
continue 2;
|
||||||
|
|
||||||
|
case 'query-read':
|
||||||
|
case 'query-write':
|
||||||
|
$query_data[] = $event;
|
||||||
|
continue 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
$memory = $event['memory'];
|
$event_data[] = $event;
|
||||||
|
|
||||||
$event_data[] = compact( 'name', 'offset', 'duration', 'width', 'category', 'memory' );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$query_data = array();
|
$timelines = array_filter( [
|
||||||
$last_query_end = 0;
|
[ 'events' => $event_data ],
|
||||||
$last_offset = 0;
|
[ 'events' => $query_data ],
|
||||||
$last_duration = 0;
|
[ 'events' => $http_data ],
|
||||||
|
|
||||||
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
|
|
||||||
|
|
||||||
foreach ( $wpdb->queries as $key => $query ) {
|
|
||||||
$query_start = isset( $this->query_starts[ $key ] ) ? $this->query_starts[ $key ] : $last_query_end;
|
|
||||||
list( $sql, $duration, $trace ) = $query;
|
|
||||||
$sql = trim( $sql );
|
|
||||||
$category = 'query-read';
|
|
||||||
|
|
||||||
if ( 0 === stripos( $sql, 'INSERT' ) || 0 === stripos( $sql, 'UPDATE' ) ) {
|
|
||||||
$category = 'query-write';
|
|
||||||
}
|
|
||||||
|
|
||||||
$duration *= 1000;
|
|
||||||
$last_query_end = $query_start + $duration;
|
|
||||||
$offset = round( ( $query_start - $start ) / $total * 100, 2 );
|
|
||||||
|
|
||||||
// if query is indistinguishably close to previous then stack it
|
|
||||||
if ( $offset === $last_offset ) {
|
|
||||||
$key = count( $query_data ) - 1;
|
|
||||||
$query_data[ $key ]['sql'] .= '<br />' . $sql;
|
|
||||||
|
|
||||||
$last_duration += $duration;
|
|
||||||
$width = round( $last_duration / $total * 100, 2 );
|
|
||||||
$query_data[ $key ]['width'] = $width;
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$width = round( $duration / $total * 100, 2 );
|
|
||||||
$last_offset = $offset;
|
|
||||||
$last_duration = $duration;
|
|
||||||
|
|
||||||
$query_data[] = compact( 'sql', 'duration', 'offset', 'width', 'category' );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$html = $this['mustache']->render( 'laps', [
|
|
||||||
'events' => $event_data,
|
|
||||||
'queries' => $query_data,
|
|
||||||
'savequeries' => defined( 'SAVEQUERIES' ) && SAVEQUERIES,
|
|
||||||
'http' => $http_data,
|
|
||||||
'savehttp' => ! empty( $http_data ),
|
|
||||||
] );
|
] );
|
||||||
|
|
||||||
$wp_admin_bar->add_node( array(
|
$wp_admin_bar->add_node( [
|
||||||
'id' => 'laps',
|
'id' => 'laps',
|
||||||
'title' => sprintf( 'Lap: %ss', round( $total / 1000, 3 ) ),
|
'title' => sprintf( 'Lap: %ss', round( $total / 1000, 3 ) ),
|
||||||
) );
|
] );
|
||||||
|
|
||||||
$wp_admin_bar->add_node( array(
|
$wp_admin_bar->add_node( [
|
||||||
'id' => 'laps_output',
|
'id' => 'laps_output',
|
||||||
'parent' => 'laps',
|
'parent' => 'laps',
|
||||||
'meta' => array( 'html' => $html ),
|
'meta' => [ 'html' => $this['mustache']->render( 'laps', [ 'timelines' => $timelines ] ) ],
|
||||||
) );
|
] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+4
-22
@@ -1,27 +1,9 @@
|
|||||||
|
{{#timelines}}
|
||||||
<div class="laps-timeline">
|
<div class="laps-timeline">
|
||||||
{{#events}}
|
{{#events}}
|
||||||
<div class="event event-{{category}}" data-toggle="tooltip" title="{{name}} - {{duration}} ms - {{memory}} MB" style="width:{{width}}%; left:{{offset}}%;">
|
<div class="event event-{{category}}" data-toggle="tooltip" title="{{description}}" style="width:{{width}}%; left:{{offset}}%;">
|
||||||
<span class="sr-only">{{name}} - {{duration}} ms - {{memory}} MB</span>
|
<span class="sr-only">{{descriptiondescription}}</span>
|
||||||
</div>
|
</div>
|
||||||
{{/events}}
|
{{/events}}
|
||||||
</div>
|
</div>
|
||||||
|
{{/timelines}}
|
||||||
{{#savequeries}}
|
|
||||||
<div class="laps-timeline">
|
|
||||||
{{#queries}}
|
|
||||||
<div class="event event-{{category}}" title="{{sql}}" style="width:{{width}}%; left:{{offset}}%;">
|
|
||||||
<span class="sr-only">{{sql}}</span>
|
|
||||||
</div>
|
|
||||||
{{/queries}}
|
|
||||||
</div>
|
|
||||||
{{/savequeries}}
|
|
||||||
|
|
||||||
{{#savehttp}}
|
|
||||||
<div class="laps-timeline">
|
|
||||||
{{#http}}
|
|
||||||
<div class="event event-{{category}}" title="{{name}} - {{duration}} ms" style="width:{{width}}%; left:{{offset}}%;">
|
|
||||||
<span class="sr-only">{{name}} - {{duration}} ms</span>
|
|
||||||
</div>
|
|
||||||
{{/http}}
|
|
||||||
</div>
|
|
||||||
{{/savehttp}}
|
|
||||||
Reference in new issue
Block a user