Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
52 changes: 52 additions & 0 deletions src/Profilers/SpxProfiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Xhgui\Profiler\Profilers;

use Xhgui\Profiler\ProfilingFlags;

/**
* A simple & straight-to-the-point PHP profiling extension
*
* @see https://github.com/NoiseByNorthwest/php-spx
*/
class SpxProfiler implements ProfilerInterface
{
const EXTENSION_NAME = 'spx';

public function isSupported()
{
return extension_loaded(self::EXTENSION_NAME);
}

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();
}

/**
* {@inheritdoc}
*/
public function disable()
{
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;
}
}
23 changes: 23 additions & 0 deletions tests/Profiler/SpxProfilerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Xhgui\Profiler\Test\Profiler;

use Xhgui\Profiler\Profilers\SpxProfiler;
use Xhgui\Profiler\Test\TestCase;

/**
* @requires extension spx
*/
class SpxProfilerTest extends TestCase
{
public function setUp()
{
$this->profiler = new SpxProfiler();
}

public function testDefaults()
{
$data = $this->runProfiler();
$this->assertCount(13, $data);
}
}