-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sort profiler edges using Tree abstraction; add envs TRAP_XHPRO…
…F_SORT and TRAP_XHPROF_PATH
- Loading branch information
Showing
6 changed files
with
402 additions
and
99 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
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Service\FilesObserver\Converter; | ||
|
||
/** | ||
* @template-covariant T of object | ||
* | ||
* @internal | ||
*/ | ||
final class Branch | ||
{ | ||
/** | ||
* @param T $item | ||
* @param non-empty-string $id | ||
* @param non-empty-string|null $parentId | ||
* @param list<Branch<T>> $children | ||
* @param Branch<T>|null $parent | ||
*/ | ||
public function __construct( | ||
public object $item, | ||
public readonly string $id, | ||
public readonly ?string $parentId, | ||
public array $children = [], | ||
public ?Branch $parent = null, | ||
) {} | ||
|
||
public function __destruct() | ||
{ | ||
unset($this->item, $this->children, $this->parent); | ||
} | ||
} |
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,129 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Service\FilesObserver\Converter; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class Cost implements \JsonSerializable | ||
{ | ||
public float $p_cpu = 0; | ||
public float $p_ct = 0; | ||
public float $p_mu = 0; | ||
public float $p_pmu = 0; | ||
public float $p_wt = 0; | ||
|
||
/** @var int<0, max> */ | ||
public int $d_cpu = 0; | ||
|
||
/** @var int<0, max> */ | ||
public int $d_ct = 0; | ||
|
||
/** @var int<0, max> */ | ||
public int $d_mu = 0; | ||
|
||
/** @var int<0, max> */ | ||
public int $d_pmu = 0; | ||
|
||
/** @var int<0, max> */ | ||
public int $d_wt = 0; | ||
|
||
/** | ||
* @param int<0, max> $ct | ||
* @param int<0, max> $wt | ||
* @param int<0, max> $cpu | ||
* @param int<0, max> $mu | ||
* @param int<0, max> $pmu | ||
*/ | ||
public function __construct( | ||
public readonly int $ct, | ||
public readonly int $wt, | ||
public readonly int $cpu, | ||
public readonly int $mu, | ||
public readonly int $pmu, | ||
) {} | ||
|
||
/** | ||
* @param array{ | ||
* ct: int<0, max>, | ||
* wt: int<0, max>, | ||
* cpu: int<0, max>, | ||
* mu: int<0, max>, | ||
* pmu: int<0, max>, | ||
* p_ct?: float<0, 100>, | ||
* p_wt?: float<0, 100>, | ||
* p_cpu?: float<0, 100>, | ||
* p_mu?: float<0, 100>, | ||
* p_pmu?: float<0, 100>, | ||
* d_ct?: int<0, max>, | ||
* d_wt?: int<0, max>, | ||
* d_cpu?: int<0, max>, | ||
* d_mu?: int<0, max>, | ||
* d_pmu?: int<0, max> | ||
* } $data | ||
*/ | ||
public static function fromArray(array $data): self | ||
{ | ||
$self = new self( | ||
$data['ct'], | ||
$data['wt'], | ||
$data['cpu'], | ||
$data['mu'], | ||
$data['pmu'], | ||
); | ||
\array_key_exists('p_ct', $data) and $self->p_ct = $data['p_ct']; | ||
\array_key_exists('p_wt', $data) and $self->p_wt = $data['p_wt']; | ||
\array_key_exists('p_cpu', $data) and $self->p_cpu = $data['p_cpu']; | ||
\array_key_exists('p_mu', $data) and $self->p_mu = $data['p_mu']; | ||
\array_key_exists('p_pmu', $data) and $self->p_pmu = $data['p_pmu']; | ||
\array_key_exists('d_ct', $data) and $self->d_ct = $data['d_ct']; | ||
\array_key_exists('d_wt', $data) and $self->d_wt = $data['d_wt']; | ||
\array_key_exists('d_cpu', $data) and $self->d_cpu = $data['d_cpu']; | ||
\array_key_exists('d_mu', $data) and $self->d_mu = $data['d_mu']; | ||
\array_key_exists('d_pmu', $data) and $self->d_pmu = $data['d_pmu']; | ||
|
||
return $self; | ||
} | ||
|
||
/** | ||
* @return array{ | ||
* ct: int<0, max>, | ||
* wt: int<0, max>, | ||
* cpu: int<0, max>, | ||
* mu: int<0, max>, | ||
* pmu: int<0, max>, | ||
* p_ct: float<0, 100>, | ||
* p_wt: float<0, 100>, | ||
* p_cpu: float<0, 100>, | ||
* p_mu: float<0, 100>, | ||
* p_pmu: float<0, 100>, | ||
* d_ct: int<0, max>, | ||
* d_wt: int<0, max>, | ||
* d_cpu: int<0, max>, | ||
* d_mu: int<0, max>, | ||
* d_pmu: int<0, max> | ||
* } | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'ct' => $this->ct, | ||
'wt' => $this->wt, | ||
'cpu' => $this->cpu, | ||
'mu' => $this->mu, | ||
'pmu' => $this->pmu, | ||
'p_ct' => $this->p_ct, | ||
'p_wt' => $this->p_wt, | ||
'p_cpu' => $this->p_cpu, | ||
'p_mu' => $this->p_mu, | ||
'p_pmu' => $this->p_pmu, | ||
'd_ct' => $this->d_ct, | ||
'd_wt' => $this->d_wt, | ||
'd_cpu' => $this->d_cpu, | ||
'd_mu' => $this->d_mu, | ||
'd_pmu' => $this->d_pmu, | ||
]; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Service\FilesObserver\Converter; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class Edge implements \JsonSerializable | ||
{ | ||
public function __construct( | ||
public readonly ?string $caller, | ||
public readonly string $callee, | ||
public readonly Cost $cost, | ||
) {} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'caller' => $this->caller, | ||
'callee' => $this->callee, | ||
'cost' => $this->cost, | ||
]; | ||
} | ||
} |
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,144 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Service\FilesObserver\Converter; | ||
|
||
/** | ||
* @template-covariant TItem of object | ||
* | ||
* @implements \IteratorAggregate<Branch<TItem>> | ||
* | ||
* @internal | ||
*/ | ||
final class Tree implements \IteratorAggregate | ||
{ | ||
/** @var array<non-empty-string, Branch<TItem>> */ | ||
private array $root = []; | ||
|
||
/** @var array<non-empty-string, Branch<TItem>> */ | ||
private array $all = []; | ||
|
||
/** @var array<non-empty-string, Branch<TItem>> */ | ||
private array $lostChildren = []; | ||
|
||
/** | ||
* @template-covariant T of object | ||
* | ||
* @param list<T> $edges | ||
* @param callable(T): non-empty-string $getCurrent Get current node id | ||
* @param callable(T): (non-empty-string|null) $getParent Get parent node id | ||
* | ||
* @return self<T> | ||
*/ | ||
public static function fromEdgesList(array $edges, callable $getCurrent, callable $getParent): self | ||
{ | ||
$tree = new self(); | ||
|
||
foreach ($edges as $edge) { | ||
$current = $getCurrent($edge); | ||
$parent = $getParent($edge); | ||
|
||
$tree->addItem($edge, $current, $parent); | ||
} | ||
|
||
return $tree; | ||
} | ||
|
||
/** | ||
* @param TItem $item | ||
* @param non-empty-string $id | ||
* @param non-empty-string|null $parentId | ||
*/ | ||
public function addItem(object $item, string $id, ?string $parentId): void | ||
{ | ||
$branch = new Branch($item, $id, $parentId); | ||
$this->all[$id] = $branch; | ||
|
||
if ($parentId === null) { | ||
$this->root[$id] = $branch; | ||
} else { | ||
$branch->parent = $this->all[$parentId] ?? null; | ||
|
||
$branch->parent === null | ||
? $this->lostChildren[$id] = $branch | ||
: $branch->parent->children[] = $branch; | ||
} | ||
|
||
foreach ($this->lostChildren as $lostChild) { | ||
if ($lostChild->parentId === $id) { | ||
$branch->children[] = $lostChild; | ||
unset($this->lostChildren[$lostChild->id]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Iterate all the branches without sorting and hierarchy. | ||
* | ||
* @return \Traversable<non-empty-string, Branch<TItem>> | ||
*/ | ||
public function getIterator(): \Traversable | ||
{ | ||
yield from $this->all; | ||
} | ||
|
||
/** | ||
* Yield items by the level in the hierarchy with custom sorting in level scope | ||
* | ||
* @param callable(Branch<TItem>, Branch<TItem>): int $sorter | ||
* | ||
* @return \Traversable<TItem> | ||
*/ | ||
public function getItemsSortedV1(?callable $sorter): \Traversable | ||
{ | ||
$level = 0; | ||
$queue = [$level => $this->root]; | ||
processLevel: | ||
while ($queue[$level] !== []) { | ||
$branch = \array_shift($queue[$level]); | ||
yield $branch->item; | ||
|
||
// Fill the next level | ||
$queue[$level + 1] ??= []; | ||
\array_unshift($queue[$level + 1], ...$branch->children); | ||
} | ||
|
||
if (\array_key_exists(++$level, $queue)) { | ||
$sorter === null or \usort($queue[$level], $sorter); | ||
|
||
goto processLevel; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Yield items deep-first. | ||
* | ||
* @param callable(Branch<TItem>, Branch<TItem>): int $sorter | ||
* | ||
* @return \Traversable<TItem> | ||
*/ | ||
public function getItemsSortedV0(?callable $sorter): \Traversable | ||
{ | ||
$queue = $this->root; | ||
while (\count($queue) > 0) { | ||
$branch = \array_shift($queue); | ||
yield $branch->item; | ||
|
||
$children = $branch->children; | ||
$sorter === null or \usort($children, $sorter); | ||
|
||
\array_unshift($queue, ...$children); | ||
} | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
foreach ($this->all as $branch) { | ||
$branch->__destruct(); | ||
} | ||
|
||
unset($this->all, $this->root, $this->lostChildren); | ||
} | ||
} |
Oops, something went wrong.