This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 209
Convert XHProf to Cachegrind for PHPStorm usage #92
Open
beberlei
wants to merge
3
commits into
master
Choose a base branch
from
Cachegrind
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/phpunit | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"autoload": { | ||
"psr-4": { | ||
"Tideways\\Xhprof\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" ?> | ||
<phpunit bootstrap="vendor/autoload.php"> | ||
<testsuites> | ||
<testsuite name="Tideways XHProf Support Code"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace Tideways\Xhprof; | ||
|
||
class CachegrindConverter | ||
{ | ||
private $aggregations = []; | ||
private $children = []; | ||
private $data = []; | ||
private $output = ''; | ||
private $renderedCalls = []; | ||
private $num = 0; | ||
|
||
public function convertToCachegrind(array $data) | ||
{ | ||
$this->aggregations = []; | ||
$this->children = []; | ||
$this->renderedCalls = []; | ||
$this->data = $data; | ||
$this->num = 0; | ||
|
||
$this->aggregations['main()'] = ['ct' => 1, 'wt' => $data['main()']['wt'], 'pmu' => $data['main()']['pmu'] ?? 0]; | ||
|
||
foreach ($data as $callPair => $callInfo) { | ||
if ($callPair === "main()") { | ||
continue; | ||
} | ||
|
||
[$parent, $child] = explode('==>', $callPair); | ||
|
||
if (!isset($this->children[$parent])) { | ||
$this->children[$parent] = []; | ||
} | ||
if (!isset($this->children[$child])) { | ||
$this->children[$child] = []; | ||
} | ||
$this->children[$parent][] = $child; | ||
|
||
if (isset($this->aggregations[$child])) { | ||
$this->aggregations[$child]['wt'] += $callInfo['wt'] ?? 0; | ||
$this->aggregations[$child]['pmu'] += $callInfo['pmu'] ?? 0; | ||
} else { | ||
$this->aggregations[$child]['wt'] = $callInfo['wt'] ?? 0; | ||
$this->aggregations[$child]['pmu'] = $callInfo['pmu'] ?? 0; | ||
} | ||
|
||
if (isset($this->aggregations[$parent])) { | ||
$this->aggregations[$parent]['wt'] -= $callInfo['wt'] ?? 0; | ||
$this->aggregations[$parent]['pmu'] -= $callInfo['pmu'] ?? 0; | ||
} else { | ||
$this->aggregations[$parent]['wt'] = -1 * $callInfo['wt'] ?? 0; | ||
$this->aggregations[$parent]['pmu'] = -1 * $callInfo['pmu'] ?? 0; | ||
} | ||
} | ||
|
||
$this->output = <<<CGD | ||
version: 1 | ||
creator: tideways_xhprof | ||
cmd: php | ||
part: 1 | ||
positions: line | ||
|
||
|
||
CGD; | ||
|
||
$this->output .= "events: Time Memory\n\n"; | ||
|
||
$this->renderCall('main()'); | ||
|
||
$this->data["main()"]["wt"] = $this->data["main()"]["wt"] ?? 0; | ||
$this->data["main()"]["pmu"] = $this->data["main()"]["pmu"] ?? 0; | ||
|
||
$this->output .= "summary: {$this->data["main()"]["wt"]} {$this->data["main()"]["pmu"]}\n"; | ||
|
||
return $this->output; | ||
} | ||
|
||
private function renderCall($call) | ||
{ | ||
if (isset($this->renderedCalls[$call])) { | ||
return; | ||
} | ||
|
||
$callInfo = $this->aggregations[$call]; | ||
$this->renderedCalls[$call] = true; | ||
|
||
foreach ($this->children[$call] as $child) { | ||
$this->renderCall($child); | ||
} | ||
|
||
$this->num++; | ||
$this->aggregations[$call]['num'] = $this->num; | ||
|
||
$this->output .= "fl=(1) php:internal\n"; | ||
$this->output .= "fn=({$this->num}) {$call}\n"; | ||
$this->output .= "1 {$callInfo['wt']} {$callInfo['pmu']}\n"; | ||
|
||
foreach ($this->children[$call] as $child) { | ||
$callPair = $call . "==>" . $child; | ||
$this->output .= "cfl=(1)\n"; | ||
$this->output .= "cfn=({$this->aggregations[$child]['num']}) $child\n"; | ||
$this->output .= "calls=1 0 0\n"; | ||
$this->output .= "1 {$this->data[$callPair]['wt']} {$this->data[$callPair]['pmu']}\n"; | ||
} | ||
|
||
$this->output .= "\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Tideways\Xhprof\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Tideways\Xhprof\CachegrindConverter; | ||
|
||
class CachegrindConverterTest extends TestCase | ||
{ | ||
public function testConvert() | ||
{ | ||
$data = [ | ||
'main()' => ['wt' => 100, 'ct' => 1, 'pmu' => 50], | ||
'main()==>foo' => ['wt' => 30, 'ct' => 1, 'pmu' => 50], | ||
'main()==>bar' => ['wt' => 30, 'ct' => 1, 'pmu' => 40], | ||
'foo==>baz' => ['wt' => 25, 'ct' => 10, 'pmu' => 10], | ||
]; | ||
|
||
$converter = new CachegrindConverter(); | ||
$output = $converter->convertToCachegrind($data); | ||
|
||
$this->assertEquals(<<<CGT | ||
version: 1 | ||
creator: tideways_xhprof | ||
cmd: php | ||
part: 1 | ||
positions: line | ||
|
||
events: Time Memory | ||
|
||
fl=(1) php:internal | ||
fn=(1) baz | ||
1 25 10 | ||
|
||
fl=(1) php:internal | ||
fn=(2) foo | ||
1 5 40 | ||
cfl=(1) | ||
cfn=(1) baz | ||
calls=1 0 0 | ||
1 25 10 | ||
|
||
fl=(1) php:internal | ||
fn=(3) bar | ||
1 30 40 | ||
|
||
fl=(1) php:internal | ||
fn=(4) main() | ||
1 40 -40 | ||
cfl=(1) | ||
cfn=(2) foo | ||
calls=1 0 0 | ||
1 30 50 | ||
cfl=(1) | ||
cfn=(3) bar | ||
calls=1 0 0 | ||
1 30 40 | ||
|
||
summary: 100 50 | ||
|
||
CGT, $output); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this makes the code php 7.3 minimum version