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

+1 -11
View File
@@ -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 );
}
}
+2 -12
View File
@@ -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();
}
+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 = [];
/**
* @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() {
+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';
}
$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;
+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();
}
}