From bd42c7b6a14952bdc38c13bd36836e52742ae823 Mon Sep 17 00:00:00 2001 From: Andrey Savchenko Date: Wed, 27 Mar 2019 21:40:52 +0200 Subject: [PATCH] Added tracking load of individual plugin load (WP 5.1+) Fixes #24 --- CHANGELOG.md | 1 + src/Provider/Record_Provider.php | 10 +-- .../Collector/Plugin_Load_Collector.php | 69 +++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/Record/Collector/Plugin_Load_Collector.php diff --git a/CHANGELOG.md b/CHANGELOG.md index ef91704..e158772 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - plotting of early SQL queries (WP 5.1+) +- tracking load of individual plugin load (WP 5.1+) ### Changed - unit tests for PHPUnit 8 diff --git a/src/Provider/Record_Provider.php b/src/Provider/Record_Provider.php index 259ac20..fbaeb32 100644 --- a/src/Provider/Record_Provider.php +++ b/src/Provider/Record_Provider.php @@ -10,6 +10,7 @@ use Rarst\Laps\Record\Collector\Core_Load_Collector; use Rarst\Laps\Record\Collector\Hook_Collector; use Rarst\Laps\Record\Collector\Http_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 Symfony\Component\Stopwatch\Stopwatch; @@ -29,10 +30,11 @@ class Record_Provider implements ServiceProviderInterface, Bootable_Provider_Int $pimple['collectors'] = function ( Plugin $laps ): array { return [ - 'core' => new Core_Load_Collector(), - 'hook' => new Hook_Collector( $laps['stopwatch'], $laps['hook.events'] ), - 'http' => new Http_Collector( $laps['stopwatch'] ), - 'sql' => new Sql_Collector(), + 'core' => new Core_Load_Collector(), + 'hook' => new Hook_Collector( $laps['stopwatch'], $laps['hook.events'] ), + 'plugins' => new Plugin_Load_Collector(), + 'http' => new Http_Collector( $laps['stopwatch'] ), + 'sql' => new Sql_Collector(), ]; }; diff --git a/src/Record/Collector/Plugin_Load_Collector.php b/src/Record/Collector/Plugin_Load_Collector.php new file mode 100644 index 0000000..d2cd8ff --- /dev/null +++ b/src/Record/Collector/Plugin_Load_Collector.php @@ -0,0 +1,69 @@ +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' + ); + } +}