From 48b3383e06ed37d07e4668e3049a20c8282b64fe Mon Sep 17 00:00:00 2001 From: Andrey Savchenko Date: Mon, 12 Feb 2018 18:30:00 +0200 Subject: [PATCH] Introduced explicit record interface for event results. --- src/Manager/Toolbar_Manager.php | 14 ++--- src/Record/Hook_Record_Collector.php | 12 +--- src/Record/Http_Record_Collector.php | 14 +---- src/Record/Record_Collector_Interface.php | 3 + src/Record/Record_Interface.php | 34 ++++++++++++ src/Record/Recursive_Record_Iterator.php | 21 ++++--- src/Record/Sql_Record.php | 68 +++++++++++++++++++++++ src/Record/Sql_Record_Collector.php | 8 +-- src/Record/Stopwatch_Record.php | 62 +++++++++++++++++++++ src/Timeline_Iterator.php | 38 ++++++++++++- 10 files changed, 226 insertions(+), 48 deletions(-) create mode 100644 src/Record/Record_Interface.php create mode 100644 src/Record/Sql_Record.php create mode 100644 src/Record/Stopwatch_Record.php diff --git a/src/Manager/Toolbar_Manager.php b/src/Manager/Toolbar_Manager.php index 4c422e8..91fb439 100644 --- a/src/Manager/Toolbar_Manager.php +++ b/src/Manager/Toolbar_Manager.php @@ -38,15 +38,9 @@ class Toolbar_Manager implements ServiceProviderInterface, Bootable_Provider_Int global $timestart; - $records = $this->laps['records']; - $start = $timestart * 1000; - $end = microtime( true ) * 1000; - $total = $end - $start; - - foreach ( $records as $key => $event ) { - $records[ $key ]['offset'] = round( ( $event['origin'] - $start ) / $total * 100, 2 ); - $records[ $key ]['width'] = round( $event['duration'] / $total * 100, 2 ); - } + $start = $timestart * 1000; + $end = microtime( true ) * 1000; + $total = $end - $start; $wp_admin_bar->add_node( [ 'id' => 'laps', @@ -58,7 +52,7 @@ class Toolbar_Manager implements ServiceProviderInterface, Bootable_Provider_Int 'parent' => 'laps', 'meta' => [ 'html' => $this->laps['mustache']->render( 'laps', [ - 'timelines' => new Timeline_Iterator( new Recursive_Record_Iterator( $records ) ), + 'timelines' => new Timeline_Iterator( new Recursive_Record_Iterator( $this->laps['records'] ) ), ] ), ], ] ); diff --git a/src/Record/Hook_Record_Collector.php b/src/Record/Hook_Record_Collector.php index e243349..fbd1a75 100644 --- a/src/Record/Hook_Record_Collector.php +++ b/src/Record/Hook_Record_Collector.php @@ -122,16 +122,6 @@ class Hook_Record_Collector implements ServiceProviderInterface, Bootable_Provid protected function transform( $name, StopwatchEvent $event ) { - $duration = $event->getDuration(); - $memory = $event->getMemory() / 1024 / 1024; - - return [ - 'name' => $name, - 'description' => "{$name} - {$duration} ms - {$memory} MB", - 'origin' => $event->getOrigin(), - 'duration' => $duration, - 'memory' => $memory, - 'category' => $event->getCategory(), - ]; + return new Stopwatch_Record($name, $event ); } } \ No newline at end of file diff --git a/src/Record/Http_Record_Collector.php b/src/Record/Http_Record_Collector.php index 904ef96..588e26e 100644 --- a/src/Record/Http_Record_Collector.php +++ b/src/Record/Http_Record_Collector.php @@ -69,16 +69,6 @@ class Http_Record_Collector implements ServiceProviderInterface, Bootable_Provid protected function transform( $name, StopwatchEvent $event ) { - $duration = $event->getDuration(); - $memory = $event->getMemory() / 1024 / 1024; - - return [ - 'name' => $name, - 'description' => $name, - 'origin' => $event->getOrigin(), - 'duration' => $duration, - 'memory' => $memory, - 'category' => $event->getCategory(), - ]; + return new Stopwatch_Record( $name, $event ); } -} \ No newline at end of file +} diff --git a/src/Record/Record_Collector_Interface.php b/src/Record/Record_Collector_Interface.php index dee4b11..03dfaa7 100644 --- a/src/Record/Record_Collector_Interface.php +++ b/src/Record/Record_Collector_Interface.php @@ -4,5 +4,8 @@ namespace Rarst\Laps\Record; interface Record_Collector_Interface { + /** + * @return Record_Interface[] + */ public function get_records(); } diff --git a/src/Record/Record_Interface.php b/src/Record/Record_Interface.php new file mode 100644 index 0000000..6165554 --- /dev/null +++ b/src/Record/Record_Interface.php @@ -0,0 +1,34 @@ + $event ) { + foreach ( $records as $key => $record ) { - if ( $event['origin'] < $end ) { + if ( $record->get_origin() < $end ) { unset( $records[ $key ] ); - $this->children[] = $event; + $this->children[] = $record; continue; } - $end = $event['origin'] + $event['duration']; + $end = $record->get_origin() + $record->get_duration(); } parent::__construct( $records, $flags ); } - protected function sort_origin( $record_a, $record_b ) { + protected function sort_origin( Record_Interface $record_a, Record_Interface $record_b ) { - if ( $record_a['origin'] === $record_b['origin'] ) { + $origin_a = $record_a->get_origin(); + $origin_b = $record_b->get_origin(); + + if ( $origin_a === $origin_b ) { return 0; } - return ( $record_a['origin'] < $record_b['origin'] ) ? - 1 : 1; + return ( $origin_a < $origin_b ) ? - 1 : 1; } public function hasChildren() { diff --git a/src/Record/Sql_Record.php b/src/Record/Sql_Record.php new file mode 100644 index 0000000..8f207da --- /dev/null +++ b/src/Record/Sql_Record.php @@ -0,0 +1,68 @@ +sql = $sql; + $this->origin = $origin; + $this->duration = $duration; + $this->category = $category; + } + + /** + * @return string + */ + public function get_name() { + return $this->sql; + } + + /** + * @return string + */ + public function get_description() { + $duration = round( $this->duration ); + + return "{$this->sql} – {$duration} ms"; + } + + /** + * @return float Timestamp of record start. + */ + public function get_origin() { + return $this->origin; + } + + /** + * @return int Record duration in milliseconds. + */ + public function get_duration() { + return $this->duration; + } + + /** + * @return string + */ + public function get_category() { + return $this->category; + } +} diff --git a/src/Record/Sql_Record_Collector.php b/src/Record/Sql_Record_Collector.php index ea673c5..018d314 100644 --- a/src/Record/Sql_Record_Collector.php +++ b/src/Record/Sql_Record_Collector.php @@ -62,14 +62,10 @@ class Sql_Record_Collector implements ServiceProviderInterface, Bootable_Provide $category = 'query-write'; } - $duration *= 1000; + $duration *= 1000; $last_query_end = $query_start + $duration; - $name = $sql; - $description = $name; - $origin = $query_start; - - $query_data[] = compact( 'name', 'description', 'origin', 'duration', 'category' ); + $query_data[] = new Sql_Record( $sql, $query_start, $duration, $category ); } return $query_data; diff --git a/src/Record/Stopwatch_Record.php b/src/Record/Stopwatch_Record.php new file mode 100644 index 0000000..0f316fe --- /dev/null +++ b/src/Record/Stopwatch_Record.php @@ -0,0 +1,62 @@ +name = $name; + $this->stopwatch_event = $stopwatch_event; + } + + /** + * @return string + */ + public function get_name() { + return $this->name; + } + + /** + * @return string + */ + public function get_description() { + + $duration = $this->stopwatch_event->getDuration(); + $memory = $this->stopwatch_event->getMemory() / 1024 / 1024; + + return "{$this->name} – {$duration} ms – {$memory} MB"; + } + + /** + * @return float Timestamp of record start. + */ + public function get_origin() { + return $this->stopwatch_event->getOrigin(); + } + + /** + * @return int Record duration in milliseconds. + */ + public function get_duration() { + return $this->stopwatch_event->getDuration(); + } + + /** + * @return string + */ + public function get_category() { + return $this->stopwatch_event->getCategory(); + } +} diff --git a/src/Timeline_Iterator.php b/src/Timeline_Iterator.php index 1b0b1ae..e9bc969 100644 --- a/src/Timeline_Iterator.php +++ b/src/Timeline_Iterator.php @@ -2,10 +2,17 @@ namespace Rarst\Laps; +use Rarst\Laps\Record\Record_Interface; use Rarst\Laps\Record\Recursive_Record_Iterator; class Timeline_Iterator implements \Iterator { + /** @var float $origin Start point for the timeline. */ + protected $origin; + + /** @var float $end Timeline duration. */ + protected $total; + /** @var Recursive_Record_Iterator */ protected $iterator; @@ -13,11 +20,25 @@ class Timeline_Iterator implements \Iterator { protected $current; public function __construct( Recursive_Record_Iterator $iterator ) { + + global $timestart; + + $this->origin = $timestart * 1000; $this->iterator = $iterator; } + /** + * @return array + */ public function current() { - return $this->current; + + $data = []; + + foreach ( $this->current as $record ) { + $data[] = $this->prepare( $record ); + } + + return $data; } public function next() { @@ -33,6 +54,19 @@ class Timeline_Iterator implements \Iterator { } public function rewind() { + $this->total = microtime( true ) * 1000 - $this->origin; $this->current = $this->iterator; } -} \ No newline at end of file + + protected function prepare( Record_Interface $record ) { + + $data = [ + 'description' => $record->get_description(), + 'category' => $record->get_category(), + 'offset' => round( ( $record->get_origin() - $this->origin ) / $this->total * 100, 2 ), + 'width' => round( $record->get_duration() / $this->total * 100, 2 ), + ]; + + return $data; + } +}