Introduced explicit record interface for event results.
This commit is contained in:
1 parent
ce7cf8eade
commit
48b3383e06
10 files changed
+220
-42
No files matched your search
@@ -38,16 +38,10 @@ 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 );
|
||||
}
|
||||
|
||||
$wp_admin_bar->add_node( [
|
||||
'id' => 'laps',
|
||||
'title' => sprintf( 'Lap: %ss', round( $total / 1000, 3 ) ),
|
||||
@@ -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'] ) ),
|
||||
] ),
|
||||
],
|
||||
] );
|
||||
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,8 @@ namespace Rarst\Laps\Record;
|
||||
|
||||
interface Record_Collector_Interface {
|
||||
|
||||
/**
|
||||
* @return Record_Interface[]
|
||||
*/
|
||||
public function get_records();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -6,32 +6,39 @@ class Recursive_Record_Iterator extends \ArrayIterator implements \RecursiveIter
|
||||
|
||||
protected $children = [];
|
||||
|
||||
/**
|
||||
* @param Record_Interface[] $records
|
||||
* @param int $flags
|
||||
*/
|
||||
public function __construct( array $records, $flags = 0 ) {
|
||||
|
||||
usort( $records, [ $this, 'sort_origin' ] );
|
||||
$end = 0;
|
||||
|
||||
foreach ( $records as $key => $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() {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -65,11 +65,7 @@ class Sql_Record_Collector implements ServiceProviderInterface, Bootable_Provide
|
||||
$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;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user