Reorganized code to designate events as input and records as results.
This commit is contained in:
1 parent
67d686f601
commit
ce7cf8eade
11 files changed
+159
-199
No files matched your search
@@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rarst\Laps\Events;
|
|
||||||
|
|
||||||
interface Events_Provider_Interface {
|
|
||||||
|
|
||||||
public function get_events();
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rarst\Laps\Events;
|
|
||||||
|
|
||||||
class Recursive_Event_Iterator extends \ArrayIterator implements \RecursiveIterator {
|
|
||||||
|
|
||||||
protected $children = [];
|
|
||||||
|
|
||||||
public function __construct( array $events, $flags = 0 ) {
|
|
||||||
|
|
||||||
usort( $events, [ $this, 'sort_origin' ] );
|
|
||||||
$end = 0;
|
|
||||||
|
|
||||||
foreach ( $events as $key => $event ) {
|
|
||||||
|
|
||||||
if ( $event['origin'] < $end ) {
|
|
||||||
unset( $events[ $key ] );
|
|
||||||
$this->children[] = $event;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$end = $event['origin'] + $event['duration'];
|
|
||||||
}
|
|
||||||
|
|
||||||
parent::__construct( $events, $flags );
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function sort_origin( $event_a, $event_b ) {
|
|
||||||
|
|
||||||
if ( $event_a['origin'] === $event_b['origin'] ) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ( $event_a['origin'] < $event_b['origin'] ) ? - 1 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function hasChildren() {
|
|
||||||
|
|
||||||
return ! empty( $this->children );
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getChildren() {
|
|
||||||
|
|
||||||
return new static( $this->children );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,124 +0,0 @@
|
|||||||
<?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' );
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
+9
-9
@@ -4,10 +4,10 @@ namespace Rarst\Laps;
|
|||||||
|
|
||||||
use Pimple\Container;
|
use Pimple\Container;
|
||||||
use Pimple\ServiceProviderInterface;
|
use Pimple\ServiceProviderInterface;
|
||||||
use Rarst\Laps\Events\Events_Provider_Interface;
|
use Rarst\Laps\Record\Record_Collector_Interface;
|
||||||
use Rarst\Laps\Events\Hook_Events_Provider;
|
use Rarst\Laps\Record\Hook_Record_Collector;
|
||||||
use Rarst\Laps\Events\Http_Events_Provider;
|
use Rarst\Laps\Record\Http_Record_Collector;
|
||||||
use Rarst\Laps\Events\Sql_Events_Provider;
|
use Rarst\Laps\Record\Sql_Record_Collector;
|
||||||
use Rarst\Laps\Manager\Asset_Manager;
|
use Rarst\Laps\Manager\Asset_Manager;
|
||||||
use Rarst\Laps\Manager\Load_Order_Manager;
|
use Rarst\Laps\Manager\Load_Order_Manager;
|
||||||
use Rarst\Laps\Manager\Toolbar_Manager;
|
use Rarst\Laps\Manager\Toolbar_Manager;
|
||||||
@@ -42,8 +42,8 @@ class Laps extends Container {
|
|||||||
$records = [];
|
$records = [];
|
||||||
|
|
||||||
foreach ( $this->providers as $provider ) {
|
foreach ( $this->providers as $provider ) {
|
||||||
if ( $provider instanceof Events_Provider_Interface ) {
|
if ( $provider instanceof Record_Collector_Interface ) {
|
||||||
$records[] = $provider->get_events();
|
$records[] = $provider->get_records();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,9 +54,9 @@ class Laps extends Container {
|
|||||||
$laps->register( new Asset_Manager() );
|
$laps->register( new Asset_Manager() );
|
||||||
$laps->register( new Toolbar_Manager() );
|
$laps->register( new Toolbar_Manager() );
|
||||||
|
|
||||||
$laps->register( new Hook_Events_Provider() );
|
$laps->register( new Hook_Record_Collector() );
|
||||||
$laps->register( new Http_Events_Provider() );
|
$laps->register( new Http_Record_Collector() );
|
||||||
$laps->register( new Sql_Events_Provider() );
|
$laps->register( new Sql_Record_Collector() );
|
||||||
|
|
||||||
foreach ( $values as $key => $value ) {
|
foreach ( $values as $key => $value ) {
|
||||||
$this->offsetSet( $key, $value );
|
$this->offsetSet( $key, $value );
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ namespace Rarst\Laps\Manager;
|
|||||||
use Pimple\Container;
|
use Pimple\Container;
|
||||||
use Pimple\ServiceProviderInterface;
|
use Pimple\ServiceProviderInterface;
|
||||||
use Rarst\Laps\Bootable_Provider_Interface;
|
use Rarst\Laps\Bootable_Provider_Interface;
|
||||||
use Rarst\Laps\Events\Recursive_Event_Iterator;
|
use Rarst\Laps\Record\Recursive_Record_Iterator;
|
||||||
use Rarst\Laps\Laps;
|
use Rarst\Laps\Laps;
|
||||||
use Rarst\Laps\Timeline_Iterator;
|
use Rarst\Laps\Timeline_Iterator;
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ class Toolbar_Manager implements ServiceProviderInterface, Bootable_Provider_Int
|
|||||||
'parent' => 'laps',
|
'parent' => 'laps',
|
||||||
'meta' => [
|
'meta' => [
|
||||||
'html' => $this->laps['mustache']->render( 'laps', [
|
'html' => $this->laps['mustache']->render( 'laps', [
|
||||||
'timelines' => new Timeline_Iterator( new Recursive_Event_Iterator( $records ) ),
|
'timelines' => new Timeline_Iterator( new Recursive_Record_Iterator( $records ) ),
|
||||||
] ),
|
] ),
|
||||||
],
|
],
|
||||||
] );
|
] );
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Rarst\Laps\Events;
|
namespace Rarst\Laps\Record;
|
||||||
|
|
||||||
use Pimple\Container;
|
use Pimple\Container;
|
||||||
use Pimple\ServiceProviderInterface;
|
use Pimple\ServiceProviderInterface;
|
||||||
use Rarst\Laps\Bootable_Provider_Interface;
|
use Rarst\Laps\Bootable_Provider_Interface;
|
||||||
|
use Rarst\Laps\Events\Core_Events;
|
||||||
|
use Rarst\Laps\Events\Laps_Events;
|
||||||
use Rarst\Laps\Laps;
|
use Rarst\Laps\Laps;
|
||||||
use Symfony\Component\Stopwatch\Stopwatch;
|
use Symfony\Component\Stopwatch\Stopwatch;
|
||||||
use Symfony\Component\Stopwatch\StopwatchEvent;
|
use Symfony\Component\Stopwatch\StopwatchEvent;
|
||||||
|
|
||||||
class Hook_Events_Provider implements ServiceProviderInterface, Bootable_Provider_Interface, Events_Provider_Interface {
|
class Hook_Record_Collector implements ServiceProviderInterface, Bootable_Provider_Interface, Record_Collector_Interface {
|
||||||
|
|
||||||
/** @var Stopwatch $stopwatch */
|
/** @var Stopwatch $stopwatch */
|
||||||
protected $stopwatch;
|
protected $stopwatch;
|
||||||
@@ -101,7 +103,7 @@ class Hook_Events_Provider implements ServiceProviderInterface, Bootable_Provide
|
|||||||
return $input;
|
return $input;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_events() {
|
public function get_records() {
|
||||||
|
|
||||||
if ( $this->stopwatch->isStarted( 'Toolbar' ) ) {
|
if ( $this->stopwatch->isStarted( 'Toolbar' ) ) {
|
||||||
$this->stopwatch->stop( 'Toolbar' );
|
$this->stopwatch->stop( 'Toolbar' );
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Rarst\Laps\Events;
|
namespace Rarst\Laps\Record;
|
||||||
|
|
||||||
use Pimple\Container;
|
use Pimple\Container;
|
||||||
use Pimple\ServiceProviderInterface;
|
use Pimple\ServiceProviderInterface;
|
||||||
@@ -9,7 +9,7 @@ use Rarst\Laps\Laps;
|
|||||||
use Symfony\Component\Stopwatch\Stopwatch;
|
use Symfony\Component\Stopwatch\Stopwatch;
|
||||||
use Symfony\Component\Stopwatch\StopwatchEvent;
|
use Symfony\Component\Stopwatch\StopwatchEvent;
|
||||||
|
|
||||||
class Http_Events_Provider implements ServiceProviderInterface, Bootable_Provider_Interface, Events_Provider_Interface {
|
class Http_Record_Collector implements ServiceProviderInterface, Bootable_Provider_Interface, Record_Collector_Interface {
|
||||||
|
|
||||||
/** @var Stopwatch $stopwatch */
|
/** @var Stopwatch $stopwatch */
|
||||||
protected $stopwatch;
|
protected $stopwatch;
|
||||||
@@ -60,7 +60,7 @@ class Http_Events_Provider implements ServiceProviderInterface, Bootable_Provide
|
|||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_events() {
|
public function get_records() {
|
||||||
|
|
||||||
$events = $this->stopwatch->getSectionEvents( '__root__' );
|
$events = $this->stopwatch->getSectionEvents( '__root__' );
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rarst\Laps\Record;
|
||||||
|
|
||||||
|
interface Record_Collector_Interface {
|
||||||
|
|
||||||
|
public function get_records();
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rarst\Laps\Record;
|
||||||
|
|
||||||
|
class Recursive_Record_Iterator extends \ArrayIterator implements \RecursiveIterator {
|
||||||
|
|
||||||
|
protected $children = [];
|
||||||
|
|
||||||
|
public function __construct( array $records, $flags = 0 ) {
|
||||||
|
|
||||||
|
usort( $records, [ $this, 'sort_origin' ] );
|
||||||
|
$end = 0;
|
||||||
|
|
||||||
|
foreach ( $records as $key => $event ) {
|
||||||
|
|
||||||
|
if ( $event['origin'] < $end ) {
|
||||||
|
unset( $records[ $key ] );
|
||||||
|
$this->children[] = $event;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$end = $event['origin'] + $event['duration'];
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::__construct( $records, $flags );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function sort_origin( $record_a, $record_b ) {
|
||||||
|
|
||||||
|
if ( $record_a['origin'] === $record_b['origin'] ) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ( $record_a['origin'] < $record_b['origin'] ) ? - 1 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasChildren() {
|
||||||
|
|
||||||
|
return ! empty( $this->children );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getChildren() {
|
||||||
|
|
||||||
|
return new static( $this->children );
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rarst\Laps\Record;
|
||||||
|
|
||||||
|
use Pimple\Container;
|
||||||
|
use Pimple\ServiceProviderInterface;
|
||||||
|
use Rarst\Laps\Bootable_Provider_Interface;
|
||||||
|
use Rarst\Laps\Laps;
|
||||||
|
|
||||||
|
class Sql_Record_Collector implements ServiceProviderInterface, Bootable_Provider_Interface, Record_Collector_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_records() {
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,17 +2,17 @@
|
|||||||
|
|
||||||
namespace Rarst\Laps;
|
namespace Rarst\Laps;
|
||||||
|
|
||||||
use Rarst\Laps\Events\Recursive_Event_Iterator;
|
use Rarst\Laps\Record\Recursive_Record_Iterator;
|
||||||
|
|
||||||
class Timeline_Iterator implements \Iterator {
|
class Timeline_Iterator implements \Iterator {
|
||||||
|
|
||||||
/** @var Recursive_Event_Iterator */
|
/** @var Recursive_Record_Iterator */
|
||||||
protected $iterator;
|
protected $iterator;
|
||||||
|
|
||||||
/** @var Recursive_Event_Iterator */
|
/** @var Recursive_Record_Iterator */
|
||||||
protected $current;
|
protected $current;
|
||||||
|
|
||||||
public function __construct( Recursive_Event_Iterator $iterator ) {
|
public function __construct( Recursive_Record_Iterator $iterator ) {
|
||||||
$this->iterator = $iterator;
|
$this->iterator = $iterator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user