1 parent
eb573e143e
commit
3eb2a91940
3 files changed
+29
-1
No files matched your search
+1
-1
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
## Unreleased
|
||||
|
||||
### Added
|
||||
- call backtraces to SQL records
|
||||
- call backtraces to SQL records and HTTP calls
|
||||
- new format of hook events
|
||||
- sidebar tracking
|
||||
|
||||
|
||||
@@ -3,13 +3,21 @@ declare( strict_types=1 );
|
||||
|
||||
namespace Rarst\Laps\Record;
|
||||
|
||||
use Rarst\Laps\Formatter\Backtrace_Formatter;
|
||||
use Symfony\Component\Stopwatch\Stopwatch;
|
||||
use Symfony\Component\Stopwatch\StopwatchEvent;
|
||||
|
||||
/**
|
||||
* Captures time of network requests made with HTTP API.
|
||||
*/
|
||||
class Http_Record_Collector extends Stopwatch_Record_Collector {
|
||||
|
||||
/** @var Backtrace_Formatter */
|
||||
private $formatter;
|
||||
|
||||
/** @var array */
|
||||
private $callers = [];
|
||||
|
||||
/**
|
||||
* @param Stopwatch $stopwatch Stopwatch instance.
|
||||
*/
|
||||
@@ -17,6 +25,8 @@ class Http_Record_Collector extends Stopwatch_Record_Collector {
|
||||
|
||||
parent::__construct( $stopwatch );
|
||||
|
||||
$this->formatter = new Backtrace_Formatter();
|
||||
|
||||
add_action( 'pre_http_request', [ $this, 'pre_http_request' ], 10, 3 );
|
||||
add_action( 'http_api_debug', [ $this, 'http_api_debug' ], 10, 5 );
|
||||
}
|
||||
@@ -33,6 +43,7 @@ class Http_Record_Collector extends Stopwatch_Record_Collector {
|
||||
public function pre_http_request( bool $false, array $args, string $url ): bool {
|
||||
|
||||
$this->start( $url, 'http' );
|
||||
$this->callers[ $url ] = wp_debug_backtrace_summary( __CLASS__, 5, true );
|
||||
|
||||
return $false;
|
||||
}
|
||||
@@ -56,4 +67,17 @@ class Http_Record_Collector extends Stopwatch_Record_Collector {
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name Event name.
|
||||
* @param StopwatchEvent $event Stopwatch event instance.
|
||||
*
|
||||
* @return Stopwatch_Record
|
||||
*/
|
||||
public function transform( string $name, StopwatchEvent $event ): Stopwatch_Record {
|
||||
|
||||
$backtrace = $this->formatter->format( $this->callers[ $name ] );
|
||||
|
||||
return new Stopwatch_Record( $name, $event, implode( ' → ', $backtrace ) );
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Rarst\Laps\Tests\Record;
|
||||
|
||||
use Brain\Monkey\Functions;
|
||||
use Rarst\Laps\Record\Http_Record_Collector;
|
||||
use Rarst\Laps\Tests\LapsTestCase;
|
||||
use Symfony\Component\Stopwatch\Stopwatch;
|
||||
@@ -10,6 +11,9 @@ class HttpTest extends LapsTestCase {
|
||||
|
||||
public function testCollector() {
|
||||
|
||||
Functions\expect( 'wp_normalize_path' )->zeroOrMoreTimes()->andReturnFirstArg();
|
||||
Functions\expect( 'wp_debug_backtrace_summary' )->zeroOrMoreTimes()->andReturn( [] );
|
||||
|
||||
$stopwatch = new Stopwatch();
|
||||
$url = 'https://example.com/';
|
||||
$collector = new Http_Record_Collector( $stopwatch );
|
||||
|
||||
Reference in new issue
Block a user