From 9a3bc92da1e30957f38716cd40b706f7c909db64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 20 Apr 2020 10:58:29 +0300 Subject: [PATCH 1/2] Add SPX profiler as proof of concept --- README.md | 15 ++++++++++++++ src/Profilers/SpxProfiler.php | 33 ++++++++++++++++++++++++++++++ tests/Profiler/SpxProfilerTest.php | 23 +++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/Profilers/SpxProfiler.php create mode 100644 tests/Profiler/SpxProfilerTest.php diff --git a/README.md b/README.md index 887f1aa..428f7aa 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ This project replaces `header.php` approach from xhgui-collector with object bas Supported profilers: - [Tideways XHProf v5.x](#tideways-xhprof-5): PHP >= 7.0 - [XHProf](#xhprof): PHP >= 5.3, PHP >= 7.0 + - [SPX](#spx) - PHP >= 5.6, PHP >= 7.0 - [Tideways v4.x](#tideways-4x): PHP >= 7.0 - [UProfiler](#uprofiler): PHP >= 5.3, < PHP 7.0 @@ -398,3 +399,17 @@ To install `uprofiler` extension, see their [installation documentation][uprofil [UProfiler]: https://github.com/FriendsOfPHP/uprofiler [uprofiler-install]: https://github.com/FriendsOfPHP/uprofiler#installing-the-uprofiler-extension + +### SPX + +To install [SPX profiler], see their [installation documentation][spx-install]. + +Alternatively on `brew` (macOS) you can use package from [glensc/tap] tap: + +``` +brew install glensc/tap/php@7.1-spx +``` + +[glensc/tap]: https://github.com/glensc/homebrew-tap +[spx-install]: https://github.com/NoiseByNorthwest/php-spx#installation +[SPX profiler]: https://github.com/NoiseByNorthwest/php-spx diff --git a/src/Profilers/SpxProfiler.php b/src/Profilers/SpxProfiler.php new file mode 100644 index 0000000..ac3dd47 --- /dev/null +++ b/src/Profilers/SpxProfiler.php @@ -0,0 +1,33 @@ +profiler = new SpxProfiler(); + } + + public function testDefaults() + { + $data = $this->runProfiler(); + $this->assertCount(13, $data); + } +} From d8463375d921b607c6bd1013feb23df0a485ed75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 20 Apr 2020 11:50:28 +0300 Subject: [PATCH 2/2] Attempt to read profiling to return something. still POC Apparenty putenv() have no effect to spx, as these are done too late (need to be done before extension initialization on cli usage) --- src/Profilers/SpxProfiler.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Profilers/SpxProfiler.php b/src/Profilers/SpxProfiler.php index ac3dd47..b69a04a 100644 --- a/src/Profilers/SpxProfiler.php +++ b/src/Profilers/SpxProfiler.php @@ -20,6 +20,11 @@ public function isSupported() public function enable($flags = array(), $options = array()) { + // https://github.com/NoiseByNorthwest/php-spx#available-parameters + putenv('SPX_ENABLED=1'); + putenv('SPX_REPORT=full'); + putenv('SPX_AUTO_START=0'); + putenv('SPX_TRACE_FILE=/tmp/spx-SPX_TRACE_FILE'); spx_profiler_start(); } @@ -28,6 +33,20 @@ public function enable($flags = array(), $options = array()) */ public function disable() { - return spx_profiler_stop(); + spx_profiler_stop(); + + $key = 'spx-full-20200420_114745-rocinante-7902-16807'; + + return $this->readProfile($key); + } + + private function readProfile($key) + { + $profileDir = ini_get('spx.data_dir'); + $json = json_decode(file_get_contents("{$profileDir}/{$key}.json"), true); + + return array_fill(0, $json['call_count'], array()); + + return $json; } }