-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1162 from rubenrubiob/main
Add performance counters to ConsoleSubsriber
- Loading branch information
Showing
10 changed files
with
375 additions
and
19 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Qossmic\Deptrac\Supportive\Time; | ||
|
||
use function hrtime; | ||
|
||
/** | ||
* @psalm-immutable | ||
*/ | ||
final class Period | ||
{ | ||
private function __construct( | ||
public readonly float|int $startedAt, | ||
public readonly float|int $endedAt, | ||
) { | ||
} | ||
|
||
/** | ||
* @psalm-pure | ||
*/ | ||
public static function stop(StartedPeriod $startedPeriod): self | ||
{ | ||
return new self( | ||
$startedPeriod->startedAt, | ||
hrtime(true), | ||
); | ||
} | ||
|
||
public function toSeconds(): float | ||
{ | ||
$duration = $this->endedAt - $this->startedAt; | ||
|
||
return $duration / 1e9; | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Qossmic\Deptrac\Supportive\Time; | ||
|
||
/** | ||
* @psalm-immutable | ||
*/ | ||
final class StartedPeriod | ||
{ | ||
private function __construct( | ||
public readonly float|int $startedAt | ||
) { | ||
} | ||
|
||
public static function start(): self | ||
{ | ||
return new self( | ||
hrtime(true), | ||
); | ||
} | ||
|
||
public function stop(): Period | ||
{ | ||
return Period::stop($this); | ||
} | ||
} |
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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Qossmic\Deptrac\Supportive\Time; | ||
|
||
use function array_key_exists; | ||
|
||
final class Stopwatch | ||
{ | ||
/** @var array<non-empty-string, StartedPeriod> */ | ||
private array $periods = []; | ||
|
||
/** | ||
* @param non-empty-string $event | ||
* | ||
* @throws StopwatchException | ||
*/ | ||
public function start(string $event): void | ||
{ | ||
$this->assertPeriodNotStarted($event); | ||
|
||
$this->periods[$event] = StartedPeriod::start(); | ||
} | ||
|
||
/** | ||
* @param non-empty-string $event | ||
* | ||
* @throws StopwatchException | ||
*/ | ||
public function stop(string $event): Period | ||
{ | ||
$this->assertPeriodStarted($event); | ||
|
||
$period = $this->periods[$event]->stop(); | ||
|
||
unset($this->periods[$event]); | ||
|
||
return $period; | ||
} | ||
|
||
/** | ||
* @param non-empty-string $event | ||
* | ||
* @throws StopwatchException | ||
*/ | ||
private function assertPeriodNotStarted(string $event): void | ||
{ | ||
if (array_key_exists($event, $this->periods)) { | ||
throw StopwatchException::periodAlreadyStarted($event); | ||
} | ||
} | ||
|
||
/** | ||
* @param non-empty-string $event | ||
* | ||
* @throws StopwatchException | ||
*/ | ||
private function assertPeriodStarted(string $event): void | ||
{ | ||
if (!array_key_exists($event, $this->periods)) { | ||
throw StopwatchException::periodNotStarted($event); | ||
} | ||
} | ||
} |
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 Qossmic\Deptrac\Supportive\Time; | ||
|
||
use Qossmic\Deptrac\Contract\ExceptionInterface; | ||
use RuntimeException; | ||
|
||
use function sprintf; | ||
|
||
final class StopwatchException extends RuntimeException implements ExceptionInterface | ||
{ | ||
public static function periodAlreadyStarted(string $period): self | ||
{ | ||
return new self( | ||
sprintf( | ||
'Period "%s" is already started', | ||
$period, | ||
) | ||
); | ||
} | ||
|
||
public static function periodNotStarted(string $period): self | ||
{ | ||
return new self( | ||
sprintf( | ||
'Period "%s" is not started', | ||
$period, | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.