Implemented initial handling of arbitrary nesting events.

See #11
This commit is contained in:
Andrey Savchenko committed 2018-02-09 15:15:23 +02:00
1 parent baf5d293a1
commit bee765071b
2 files changed
+67 -24

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
<?php
namespace Rarst\Laps\Events;
class Recursive_Event_Iterator extends \RecursiveArrayIterator {
protected $children = [];
public function __construct( array $events, $flags = 0 ) {
foreach ( $events as $key => $event ) {
foreach ( $events as $check_key => $check_event ) {
if ( $check_key <= $key ) {
continue;
}
if ( $this->overlaps( $event, $check_event ) ) {
unset( $events[ $check_key ] );
$this->children[] = $check_event;
}
}
}
parent::__construct( $events, $flags );
}
/**
* @param array $event_a
* @param array $event_b
*
* @return bool True if event B starts after A and before it finishes.
*/
protected function overlaps( $event_a, $event_b ) {
if ( $event_a['origin'] > $event_b['origin'] ) {
return false;
}
return $event_b['origin'] < ( $event_a['origin'] + $event_a['duration'] );
}
public function hasChildren() {
return ! empty( $this->children );
}
public function getChildren() {
return new static( $this->children );
}
}
+12 -24
View File
@@ -7,6 +7,7 @@ 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\Recursive_Event_Iterator;
use Rarst\Laps\Events\Sql_Events_Provider;
use Symfony\Component\Stopwatch\Stopwatch;
@@ -143,35 +144,22 @@ class Laps extends Container {
$end = microtime( true ) * 1000;
$total = $end - $start;
$event_data = [];
$http_data = [];
$query_data = [];
foreach ( $events as $event ) {
$event['offset'] = round( ( $event['origin'] - $start ) / $total * 100, 2 );
$event['width'] = round( $event['duration'] / $total * 100, 2 );
switch ($event['category']) {
case 'http':
$http_data[] = $event;
continue 2;
case 'query-read':
case 'query-write':
$query_data[] = $event;
continue 2;
}
$event_data[] = $event;
$event['offset'] = round( ( $event['origin'] - $start ) / $total * 100, 2 );
$event['width'] = round( $event['duration'] / $total * 100, 2 );
$event_data[] = $event;
}
$timelines = array_filter( [
[ 'events' => $event_data ],
[ 'events' => $query_data ],
[ 'events' => $http_data ],
], function ( $data ) {
return ! empty( $data['events'] );
} );
$iterator = new Recursive_Event_Iterator( $event_data );
$timelines = [ [ 'events' => $iterator ] ];
while ( $iterator->hasChildren() ) {
$children = $iterator->getChildren();
$timelines[] = [ 'events' => $children ];
$iterator = $children;
}
$wp_admin_bar->add_node( [
'id' => 'laps',