Added generic constructor–centric record class.
This commit is contained in:
1 parent
3cae2765cb
commit
e74777c2a9
7 files changed
+135
-116
No files matched your search
@@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rarst\Laps\Record;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic record with all properties set by constructor.
|
||||||
|
*/
|
||||||
|
class Record implements Record_Interface {
|
||||||
|
|
||||||
|
/** @var string $name */
|
||||||
|
protected $name;
|
||||||
|
|
||||||
|
/** @var float $origin */
|
||||||
|
protected $origin;
|
||||||
|
|
||||||
|
/** @var float $duration */
|
||||||
|
protected $duration;
|
||||||
|
|
||||||
|
/** @var string $description */
|
||||||
|
protected $description;
|
||||||
|
|
||||||
|
/** @var string $category */
|
||||||
|
protected $category;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name Name.
|
||||||
|
* @param float $origin Start time.
|
||||||
|
* @param float $duration Duration.
|
||||||
|
* @param string $description Optional description.
|
||||||
|
* @param string $category Optional category.
|
||||||
|
*/
|
||||||
|
public function __construct( $name, $origin, $duration, $description = '', $category = '' ) {
|
||||||
|
$this->name = $name;
|
||||||
|
$this->origin = $origin;
|
||||||
|
$this->duration = $duration;
|
||||||
|
$this->description = $description;
|
||||||
|
$this->category = $category;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function get_name() {
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function get_description() {
|
||||||
|
|
||||||
|
if ( empty( $this->description ) ) {
|
||||||
|
$duration = round( $this->get_duration() * 1000 );
|
||||||
|
|
||||||
|
return "{$this->name} – {$duration} ms";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->description;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function get_origin() {
|
||||||
|
return $this->origin;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function get_duration() {
|
||||||
|
return $this->duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function get_category() {
|
||||||
|
return $this->category;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rarst\Laps\Record;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SQL record of a query event.
|
|
||||||
*/
|
|
||||||
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 SQL query.
|
|
||||||
* @param float $origin Start time.
|
|
||||||
* @param int $duration Query duration.
|
|
||||||
* @param string $category Query type.
|
|
||||||
*/
|
|
||||||
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 * 1000 ); // s to ms.
|
|
||||||
|
|
||||||
return "{$this->sql} – {$duration} ms";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return float Timestamp of record start.
|
|
||||||
*/
|
|
||||||
public function get_origin() {
|
|
||||||
return $this->origin;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return float Record duration in seconds.
|
|
||||||
*/
|
|
||||||
public function get_duration() {
|
|
||||||
return $this->duration;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function get_category() {
|
|
||||||
return $this->category;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -41,7 +41,7 @@ class Sql_Record_Collector implements Record_Collector_Interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Sql_Record[]
|
* @return Record[]
|
||||||
*/
|
*/
|
||||||
public function get_records() {
|
public function get_records() {
|
||||||
|
|
||||||
@@ -54,6 +54,7 @@ class Sql_Record_Collector implements Record_Collector_Interface {
|
|||||||
$query_data = [];
|
$query_data = [];
|
||||||
$last_query_end = 0;
|
$last_query_end = 0;
|
||||||
|
|
||||||
|
// TODO process call trace from the data.
|
||||||
foreach ( $wpdb->queries as $key => list( $sql, $duration ) ) {
|
foreach ( $wpdb->queries as $key => list( $sql, $duration ) ) {
|
||||||
$query_start = isset( $this->query_starts[ $key ] ) ? $this->query_starts[ $key ] : $last_query_end;
|
$query_start = isset( $this->query_starts[ $key ] ) ? $this->query_starts[ $key ] : $last_query_end;
|
||||||
$sql = trim( $sql );
|
$sql = trim( $sql );
|
||||||
@@ -65,7 +66,7 @@ class Sql_Record_Collector implements Record_Collector_Interface {
|
|||||||
|
|
||||||
$last_query_end = $query_start + $duration;
|
$last_query_end = $query_start + $duration;
|
||||||
|
|
||||||
$query_data[] = new Sql_Record( $sql, $query_start, $duration, $category );
|
$query_data[] = new Record( $sql, $query_start, $duration, '', $category );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $query_data;
|
return $query_data;
|
||||||
|
|||||||
@@ -3,15 +3,21 @@
|
|||||||
namespace Rarst\Laps\Tests\Record;
|
namespace Rarst\Laps\Tests\Record;
|
||||||
|
|
||||||
use Rarst\Laps\Record\Recursive_Record_Iterator;
|
use Rarst\Laps\Record\Recursive_Record_Iterator;
|
||||||
|
use Rarst\Laps\Record\Record;
|
||||||
use Rarst\Laps\Record\Timeline_Iterator;
|
use Rarst\Laps\Record\Timeline_Iterator;
|
||||||
use Rarst\Laps\Tests\LapsTestCase;
|
use Rarst\Laps\Tests\LapsTestCase;
|
||||||
|
|
||||||
class IteratorTest extends LapsTestCase {
|
class IteratorTest extends LapsTestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \Rarst\Laps\Record\Recursive_Record_Iterator
|
||||||
|
*
|
||||||
|
* @return Recursive_Record_Iterator
|
||||||
|
*/
|
||||||
public function testRecord() {
|
public function testRecord() {
|
||||||
|
|
||||||
$record_a = new StubRecord( 'Parent', 0, 100 );
|
$record_a = new Record( 'Parent', 0, 100 );
|
||||||
$record_b = new StubRecord( 'Child', 50, 100 );
|
$record_b = new Record( 'Child', 50, 100 );
|
||||||
|
|
||||||
$records = [ $record_b, $record_a ];
|
$records = [ $record_b, $record_a ];
|
||||||
|
|
||||||
@@ -35,6 +41,8 @@ class IteratorTest extends LapsTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @covers \Rarst\Laps\Record\Timeline_Iterator
|
||||||
|
*
|
||||||
* @depends testRecord
|
* @depends testRecord
|
||||||
*/
|
*/
|
||||||
public function testTimeline( Recursive_Record_Iterator $iterator ) {
|
public function testTimeline( Recursive_Record_Iterator $iterator ) {
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rarst\Laps\Tests\Record;
|
||||||
|
|
||||||
|
use Rarst\Laps\Record\Record;
|
||||||
|
use Rarst\Laps\Tests\LapsTestCase;
|
||||||
|
|
||||||
|
class RecordTest extends LapsTestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \Rarst\Laps\Record\Record
|
||||||
|
*/
|
||||||
|
public function testRecord() {
|
||||||
|
|
||||||
|
$name = 'Name';
|
||||||
|
$origin = 0;
|
||||||
|
$duration = 1;
|
||||||
|
|
||||||
|
$description = 'description';
|
||||||
|
$category = 'info';
|
||||||
|
|
||||||
|
$record = new Record( $name, $origin, $duration );
|
||||||
|
|
||||||
|
$this->assertEquals( $name, $record->get_name() );
|
||||||
|
$this->assertEquals( $origin, $record->get_origin() );
|
||||||
|
$this->assertEquals( $duration, $record->get_duration() );
|
||||||
|
$this->assertContains( $name, $record->get_description() );
|
||||||
|
$this->assertContains( (string) ( $duration * 1000 ), $record->get_description() );
|
||||||
|
$this->assertEmpty( $record->get_category() );
|
||||||
|
|
||||||
|
$record = new Record( $name, $origin, $duration, $description, $category );
|
||||||
|
|
||||||
|
$this->assertEquals( $description, $record->get_description() );
|
||||||
|
$this->assertEquals( $category, $record->get_category() );
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,12 +2,15 @@
|
|||||||
|
|
||||||
namespace Rarst\Laps\Tests\Record;
|
namespace Rarst\Laps\Tests\Record;
|
||||||
|
|
||||||
use Rarst\Laps\Record\Sql_Record;
|
use Rarst\Laps\Record\Record;
|
||||||
use Rarst\Laps\Record\Sql_Record_Collector;
|
use Rarst\Laps\Record\Sql_Record_Collector;
|
||||||
use Rarst\Laps\Tests\LapsTestCase;
|
use Rarst\Laps\Tests\LapsTestCase;
|
||||||
|
|
||||||
class SqlTest extends LapsTestCase {
|
class SqlTest extends LapsTestCase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \Rarst\Laps\Record\Sql_Record_Collector
|
||||||
|
*/
|
||||||
public function testCollector() {
|
public function testCollector() {
|
||||||
|
|
||||||
define( 'SAVEQUERIES', true );
|
define( 'SAVEQUERIES', true );
|
||||||
@@ -33,7 +36,7 @@ class SqlTest extends LapsTestCase {
|
|||||||
|
|
||||||
$record = $records[0];
|
$record = $records[0];
|
||||||
|
|
||||||
$this->assertInstanceOf( Sql_Record::class, $record );
|
$this->assertInstanceOf( Record::class, $record );
|
||||||
$this->assertEquals( $query, $record->get_name() );
|
$this->assertEquals( $query, $record->get_name() );
|
||||||
$this->assertContains( $query, $record->get_description() );
|
$this->assertContains( $query, $record->get_description() );
|
||||||
$this->assertInternalType( 'float', $record->get_origin() );
|
$this->assertInternalType( 'float', $record->get_origin() );
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rarst\Laps\Tests\Record;
|
|
||||||
|
|
||||||
use Rarst\Laps\Record\Record_Interface;
|
|
||||||
|
|
||||||
class StubRecord implements Record_Interface {
|
|
||||||
|
|
||||||
protected $name;
|
|
||||||
protected $origin;
|
|
||||||
protected $duration;
|
|
||||||
|
|
||||||
public function __construct( $name, $origin, $duration ) {
|
|
||||||
$this->name = $name;
|
|
||||||
$this->origin = $origin;
|
|
||||||
$this->duration = $duration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_name() {
|
|
||||||
return $this->name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_description() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_origin() {
|
|
||||||
return $this->origin;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_duration() {
|
|
||||||
return $this->duration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get_category() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user