generated from roadrunner-php/package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds diff calculation between two traces
- Loading branch information
1 parent
5a9a044
commit bf40168
Showing
6 changed files
with
5,067 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpiralPackages\Profiler\Converter; | ||
|
||
final class Diff implements \JsonSerializable | ||
{ | ||
private readonly array $keys; | ||
public readonly FlatterList $diff; | ||
public readonly array $diffPercent; | ||
private int $functionsCount; | ||
private float $functionsCountPercent; | ||
|
||
public function __construct( | ||
private readonly FlatterList $current, | ||
private readonly FlatterList $head, | ||
) { | ||
$this->keys = $current->getKeys(); | ||
$emptyData = \array_fill_keys($this->keys, 0); | ||
|
||
$diff = []; | ||
$diffPercent = []; | ||
|
||
foreach ($current->getData() as $key => $baseData) { | ||
$headData = $head->getData()[$key] ?? $emptyData; | ||
$diff[$key] = $this->diffKeys($headData, $baseData); | ||
|
||
if ($key === 'main()') { | ||
$diffPercent[$key] = $this->diffPercentKeys($headData, $baseData); | ||
} | ||
} | ||
|
||
$currentCount = $this->getFunctionCount($current); | ||
$headCount = $this->getFunctionCount($head); | ||
|
||
$this->functionsCount = $headCount - $currentCount; | ||
$this->functionsCountPercent = $headCount / $currentCount; | ||
|
||
$this->diff = new FlatterList($current->getKeys(), $diff); | ||
$this->diffPercent = $diffPercent; | ||
} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'current' => $this->current, | ||
'head' => $this->head, | ||
'diff' => $this->diff, | ||
'functions_count' => $this->functionsCount, | ||
'diff_percent' => $this->diffPercent, | ||
'functions_count_percent' => $this->functionsCountPercent, | ||
]; | ||
} | ||
|
||
|
||
/** | ||
* Get the total number of tracked function calls in this run. | ||
*/ | ||
private function getFunctionCount(FlatterList $list): int | ||
{ | ||
$total = 0; | ||
foreach ($list->getData() as $data) { | ||
$total += $data['ct']; | ||
} | ||
|
||
return $total; | ||
} | ||
|
||
private function diffKeys(array $a, array $b): array | ||
{ | ||
$keys = $this->keys; | ||
foreach ($keys as $key) { | ||
$a[$key] -= $b[$key]; | ||
} | ||
|
||
return $a; | ||
} | ||
|
||
private function diffPercentKeys(array $a, array $b): array | ||
{ | ||
$out = []; | ||
$keys = $this->keys; | ||
|
||
foreach ($keys as $key) { | ||
if ($b[$key] !== 0) { | ||
$out[$key] = $a[$key] / $b[$key]; | ||
} else { | ||
$out[$key] = -1; | ||
} | ||
} | ||
|
||
return $out; | ||
} | ||
} |
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
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
File renamed without changes.
Oops, something went wrong.