Added tracking load of individual plugin load (WP 5.1+)

Fixes #24
This commit is contained in:
Andrey Savchenko committed 2019-03-27 21:40:52 +02:00
1 parent bbc7ff1ada
commit bd42c7b6a1
3 files changed
+76 -4

No files matched your search

+1
View File
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added ### Added
- plotting of early SQL queries (WP 5.1+) - plotting of early SQL queries (WP 5.1+)
- tracking load of individual plugin load (WP 5.1+)
### Changed ### Changed
- unit tests for PHPUnit 8 - unit tests for PHPUnit 8
+6 -4
View File
@@ -10,6 +10,7 @@ use Rarst\Laps\Record\Collector\Core_Load_Collector;
use Rarst\Laps\Record\Collector\Hook_Collector; use Rarst\Laps\Record\Collector\Hook_Collector;
use Rarst\Laps\Record\Collector\Http_Collector; use Rarst\Laps\Record\Collector\Http_Collector;
use Rarst\Laps\Record\Collector\Lazy_Proxy_Collector; use Rarst\Laps\Record\Collector\Lazy_Proxy_Collector;
use Rarst\Laps\Record\Collector\Plugin_Load_Collector;
use Rarst\Laps\Record\Collector\Sql_Collector; use Rarst\Laps\Record\Collector\Sql_Collector;
use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Stopwatch\Stopwatch;
@@ -29,10 +30,11 @@ class Record_Provider implements ServiceProviderInterface, Bootable_Provider_Int
$pimple['collectors'] = function ( Plugin $laps ): array { $pimple['collectors'] = function ( Plugin $laps ): array {
return [ return [
'core' => new Core_Load_Collector(), 'core' => new Core_Load_Collector(),
'hook' => new Hook_Collector( $laps['stopwatch'], $laps['hook.events'] ), 'hook' => new Hook_Collector( $laps['stopwatch'], $laps['hook.events'] ),
'http' => new Http_Collector( $laps['stopwatch'] ), 'plugins' => new Plugin_Load_Collector(),
'sql' => new Sql_Collector(), 'http' => new Http_Collector( $laps['stopwatch'] ),
'sql' => new Sql_Collector(),
]; ];
}; };
@@ -0,0 +1,69 @@
<?php
declare( strict_types=1 );
namespace Rarst\Laps\Record\Collector;
use Rarst\Laps\Record\Record;
use Rarst\Laps\Record\Record_Interface;
/**
* Tracks load time of individual plugins.
*/
class Plugin_Load_Collector implements Record_Collector_Interface {
/** @var float */
private $last;
/** @var array */
private $entries = [];
/**
* Sets up start time and tracking callbacks.
*/
public function __construct() {
$this->last = microtime( true );
add_action( 'network_plugin_loaded', [ $this, 'plugin_loaded' ] );
add_action( 'plugin_loaded', [ $this, 'plugin_loaded' ] );
}
/**
* Records time for a plugin that just finished loading.
*
* @param string $plugin Plugin file path.
*/
public function plugin_loaded( string $plugin ): void {
$time = microtime( true );
$this->entries[] = [
'name' => $plugin,
'origin' => $this->last,
'duration' => $time - $this->last,
];
$this->last = $time;
}
/**
* @return Record_Interface[]
*/
public function get_records(): array {
return array_map( [ $this, 'create_record' ], $this->entries );
}
/**
* Converts recorded information into a Record instance.
*
* @param array $entry Data entry for the plugin.
*
* @return Record_Interface
*/
private function create_record( array $entry ): Record_Interface {
return new Record(
plugin_basename( $entry['name'] ),
$entry['origin'],
$entry['duration'],
'',
'plugin'
);
}
}