Introduced explicit record interface for event results.

This commit is contained in:
Andrey Savchenko committed 2018-02-12 18:30:00 +02:00
1 parent ce7cf8eade
commit 48b3383e06
10 files changed
+226 -48

No files matched your search

+4 -10
View File
@@ -38,15 +38,9 @@ class Toolbar_Manager implements ServiceProviderInterface, Bootable_Provider_Int
global $timestart; global $timestart;
$records = $this->laps['records']; $start = $timestart * 1000;
$start = $timestart * 1000; $end = microtime( true ) * 1000;
$end = microtime( true ) * 1000; $total = $end - $start;
$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 );
}
$wp_admin_bar->add_node( [ $wp_admin_bar->add_node( [
'id' => 'laps', 'id' => 'laps',
@@ -58,7 +52,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_Record_Iterator( $records ) ), 'timelines' => new Timeline_Iterator( new Recursive_Record_Iterator( $this->laps['records'] ) ),
] ), ] ),
], ],
] ); ] );
+1 -11
View File
@@ -122,16 +122,6 @@ class Hook_Record_Collector implements ServiceProviderInterface, Bootable_Provid
protected function transform( $name, StopwatchEvent $event ) { protected function transform( $name, StopwatchEvent $event ) {
$duration = $event->getDuration(); return new Stopwatch_Record($name, $event );
$memory = $event->getMemory() / 1024 / 1024;
return [
'name' => $name,
'description' => "{$name} - {$duration} ms - {$memory} MB",
'origin' => $event->getOrigin(),
'duration' => $duration,
'memory' => $memory,
'category' => $event->getCategory(),
];
} }
} }
+2 -12
View File
@@ -69,16 +69,6 @@ class Http_Record_Collector implements ServiceProviderInterface, Bootable_Provid
protected function transform( $name, StopwatchEvent $event ) { protected function transform( $name, StopwatchEvent $event ) {
$duration = $event->getDuration(); return new Stopwatch_Record( $name, $event );
$memory = $event->getMemory() / 1024 / 1024;
return [
'name' => $name,
'description' => $name,
'origin' => $event->getOrigin(),
'duration' => $duration,
'memory' => $memory,
'category' => $event->getCategory(),
];
} }
} }
@@ -4,5 +4,8 @@ namespace Rarst\Laps\Record;
interface Record_Collector_Interface { interface Record_Collector_Interface {
/**
* @return Record_Interface[]
*/
public function get_records(); public function get_records();
} }
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace Rarst\Laps\Record;
/**
* Interface for recorded results of individual event.
*/
interface Record_Interface {
/**
* @return string
*/
public function get_name();
/**
* @return string
*/
public function get_description();
/**
* @return float Timestamp of record start.
*/
public function get_origin();
/**
* @return int Record duration in milliseconds.
*/
public function get_duration();
/**
* @return string
*/
public function get_category();
}
+14 -7
View File
@@ -6,32 +6,39 @@ class Recursive_Record_Iterator extends \ArrayIterator implements \RecursiveIter
protected $children = []; protected $children = [];
/**
* @param Record_Interface[] $records
* @param int $flags
*/
public function __construct( array $records, $flags = 0 ) { public function __construct( array $records, $flags = 0 ) {
usort( $records, [ $this, 'sort_origin' ] ); usort( $records, [ $this, 'sort_origin' ] );
$end = 0; $end = 0;
foreach ( $records as $key => $event ) { foreach ( $records as $key => $record ) {
if ( $event['origin'] < $end ) { if ( $record->get_origin() < $end ) {
unset( $records[ $key ] ); unset( $records[ $key ] );
$this->children[] = $event; $this->children[] = $record;
continue; continue;
} }
$end = $event['origin'] + $event['duration']; $end = $record->get_origin() + $record->get_duration();
} }
parent::__construct( $records, $flags ); 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 0;
} }
return ( $record_a['origin'] < $record_b['origin'] ) ? - 1 : 1; return ( $origin_a < $origin_b ) ? - 1 : 1;
} }
public function hasChildren() { public function hasChildren() {
+68
View File
@@ -0,0 +1,68 @@
<?php
namespace Rarst\Laps\Record;
class Sql_Record implements Record_Interface {
/** @var string $sql */
protected $sql;
/** @var float $origin */
protected $origin;
/** @var int $duration */
protected $duration;
/** @var string $category */
protected $category;
/**
* @param string $sql
* @param float $origin
* @param int $duration
* @param string $category
*/
public function __construct( $sql, $origin, $duration, $category = 'query-read' ) {
$this->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;
}
}
+2 -6
View File
@@ -62,14 +62,10 @@ class Sql_Record_Collector implements ServiceProviderInterface, Bootable_Provide
$category = 'query-write'; $category = 'query-write';
} }
$duration *= 1000; $duration *= 1000;
$last_query_end = $query_start + $duration; $last_query_end = $query_start + $duration;
$name = $sql; $query_data[] = new Sql_Record( $sql, $query_start, $duration, $category );
$description = $name;
$origin = $query_start;
$query_data[] = compact( 'name', 'description', 'origin', 'duration', 'category' );
} }
return $query_data; return $query_data;
+62
View File
@@ -0,0 +1,62 @@
<?php
namespace Rarst\Laps\Record;
use Symfony\Component\Stopwatch\StopwatchEvent;
class Stopwatch_Record implements Record_Interface {
/** @var string $name */
protected $name;
/** @var StopwatchEvent */
protected $stopwatch_event;
/**
* @param string $name Event name.
* @param StopwatchEvent $stopwatch_event Stopwatch event instance.
*/
public function __construct( $name, StopwatchEvent $stopwatch_event ) {
$this->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();
}
}
+36 -2
View File
@@ -2,10 +2,17 @@
namespace Rarst\Laps; namespace Rarst\Laps;
use Rarst\Laps\Record\Record_Interface;
use Rarst\Laps\Record\Recursive_Record_Iterator; use Rarst\Laps\Record\Recursive_Record_Iterator;
class Timeline_Iterator implements \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 */ /** @var Recursive_Record_Iterator */
protected $iterator; protected $iterator;
@@ -13,11 +20,25 @@ class Timeline_Iterator implements \Iterator {
protected $current; protected $current;
public function __construct( Recursive_Record_Iterator $iterator ) { public function __construct( Recursive_Record_Iterator $iterator ) {
global $timestart;
$this->origin = $timestart * 1000;
$this->iterator = $iterator; $this->iterator = $iterator;
} }
/**
* @return array
*/
public function current() { public function current() {
return $this->current;
$data = [];
foreach ( $this->current as $record ) {
$data[] = $this->prepare( $record );
}
return $data;
} }
public function next() { public function next() {
@@ -33,6 +54,19 @@ class Timeline_Iterator implements \Iterator {
} }
public function rewind() { public function rewind() {
$this->total = microtime( true ) * 1000 - $this->origin;
$this->current = $this->iterator; $this->current = $this->iterator;
} }
}
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;
}
}