diff --git a/src/Events/Events_Provider_Interface.php b/src/Events/Events_Provider_Interface.php deleted file mode 100644 index 5929062..0000000 --- a/src/Events/Events_Provider_Interface.php +++ /dev/null @@ -1,8 +0,0 @@ - $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 ); - } -} \ No newline at end of file diff --git a/src/Events/Sql_Events_Provider.php b/src/Events/Sql_Events_Provider.php deleted file mode 100644 index 58e5cd4..0000000 --- a/src/Events/Sql_Events_Provider.php +++ /dev/null @@ -1,124 +0,0 @@ -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'] .= '
' . $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' ); -// } -// } \ No newline at end of file diff --git a/src/Laps.php b/src/Laps.php index 92a637a..20ec164 100644 --- a/src/Laps.php +++ b/src/Laps.php @@ -4,10 +4,10 @@ namespace Rarst\Laps; use Pimple\Container; use Pimple\ServiceProviderInterface; -use Rarst\Laps\Events\Events_Provider_Interface; -use Rarst\Laps\Events\Hook_Events_Provider; -use Rarst\Laps\Events\Http_Events_Provider; -use Rarst\Laps\Events\Sql_Events_Provider; +use Rarst\Laps\Record\Record_Collector_Interface; +use Rarst\Laps\Record\Hook_Record_Collector; +use Rarst\Laps\Record\Http_Record_Collector; +use Rarst\Laps\Record\Sql_Record_Collector; use Rarst\Laps\Manager\Asset_Manager; use Rarst\Laps\Manager\Load_Order_Manager; use Rarst\Laps\Manager\Toolbar_Manager; @@ -42,8 +42,8 @@ class Laps extends Container { $records = []; foreach ( $this->providers as $provider ) { - if ( $provider instanceof Events_Provider_Interface ) { - $records[] = $provider->get_events(); + if ( $provider instanceof Record_Collector_Interface ) { + $records[] = $provider->get_records(); } } @@ -54,9 +54,9 @@ class Laps extends Container { $laps->register( new Asset_Manager() ); $laps->register( new Toolbar_Manager() ); - $laps->register( new Hook_Events_Provider() ); - $laps->register( new Http_Events_Provider() ); - $laps->register( new Sql_Events_Provider() ); + $laps->register( new Hook_Record_Collector() ); + $laps->register( new Http_Record_Collector() ); + $laps->register( new Sql_Record_Collector() ); foreach ( $values as $key => $value ) { $this->offsetSet( $key, $value ); diff --git a/src/Manager/Toolbar_Manager.php b/src/Manager/Toolbar_Manager.php index 21f0d94..4c422e8 100644 --- a/src/Manager/Toolbar_Manager.php +++ b/src/Manager/Toolbar_Manager.php @@ -5,7 +5,7 @@ namespace Rarst\Laps\Manager; use Pimple\Container; use Pimple\ServiceProviderInterface; 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\Timeline_Iterator; @@ -58,7 +58,7 @@ class Toolbar_Manager implements ServiceProviderInterface, Bootable_Provider_Int 'parent' => 'laps', 'meta' => [ 'html' => $this->laps['mustache']->render( 'laps', [ - 'timelines' => new Timeline_Iterator( new Recursive_Event_Iterator( $records ) ), + 'timelines' => new Timeline_Iterator( new Recursive_Record_Iterator( $records ) ), ] ), ], ] ); diff --git a/src/Events/Hook_Events_Provider.php b/src/Record/Hook_Record_Collector.php similarity index 92% rename from src/Events/Hook_Events_Provider.php rename to src/Record/Hook_Record_Collector.php index e3f777f..e243349 100644 --- a/src/Events/Hook_Events_Provider.php +++ b/src/Record/Hook_Record_Collector.php @@ -1,15 +1,17 @@ stopwatch->isStarted( 'Toolbar' ) ) { $this->stopwatch->stop( 'Toolbar' ); diff --git a/src/Events/Http_Events_Provider.php b/src/Record/Http_Record_Collector.php similarity index 90% rename from src/Events/Http_Events_Provider.php rename to src/Record/Http_Record_Collector.php index c727509..904ef96 100644 --- a/src/Events/Http_Events_Provider.php +++ b/src/Record/Http_Record_Collector.php @@ -1,6 +1,6 @@ stopwatch->getSectionEvents( '__root__' ); diff --git a/src/Record/Record_Collector_Interface.php b/src/Record/Record_Collector_Interface.php new file mode 100644 index 0000000..dee4b11 --- /dev/null +++ b/src/Record/Record_Collector_Interface.php @@ -0,0 +1,8 @@ + $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 ); + } +} \ No newline at end of file diff --git a/src/Record/Sql_Record_Collector.php b/src/Record/Sql_Record_Collector.php new file mode 100644 index 0000000..ea673c5 --- /dev/null +++ b/src/Record/Sql_Record_Collector.php @@ -0,0 +1,82 @@ +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; + } +} diff --git a/src/Timeline_Iterator.php b/src/Timeline_Iterator.php index a2adae5..1b0b1ae 100644 --- a/src/Timeline_Iterator.php +++ b/src/Timeline_Iterator.php @@ -2,17 +2,17 @@ namespace Rarst\Laps; -use Rarst\Laps\Events\Recursive_Event_Iterator; +use Rarst\Laps\Record\Recursive_Record_Iterator; class Timeline_Iterator implements \Iterator { - /** @var Recursive_Event_Iterator */ + /** @var Recursive_Record_Iterator */ protected $iterator; - /** @var Recursive_Event_Iterator */ + /** @var Recursive_Record_Iterator */ protected $current; - public function __construct( Recursive_Event_Iterator $iterator ) { + public function __construct( Recursive_Record_Iterator $iterator ) { $this->iterator = $iterator; }