Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor summary data structure #306

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions src/Collector/CollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface CollectorInterface
{
/**
* @return string Collector's name.
* @psalm-return non-empty-string
*/
public function getName(): string;

Expand Down
6 changes: 6 additions & 0 deletions src/Collector/CollectorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Yiisoft\Yii\Debug\Collector;

/**
* @psalm-require-implements CollectorInterface
*/
trait CollectorTrait
{
private bool $isActive = false;
Expand All @@ -15,10 +18,13 @@

public function shutdown(): void
{
$this->reset();

Check warning on line 21 in src/Collector/CollectorTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ } public function shutdown() : void { - $this->reset(); + $this->isActive = false; } /**
$this->isActive = false;

Check warning on line 22 in src/Collector/CollectorTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ public function shutdown() : void { $this->reset(); - $this->isActive = false; + $this->isActive = true; } /** * @psalm-return non-empty-string
}

/**
* @psalm-return non-empty-string
*/
public function getName(): string
{
return self::class;
Expand Down
10 changes: 4 additions & 6 deletions src/Collector/Console/CommandCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
return;
}

$this->timelineCollector->collect($this, spl_object_id($event));

Check warning on line 59 in src/Collector/Console/CommandCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ if (!$this->isActive()) { return; } - $this->timelineCollector->collect($this, spl_object_id($event)); + $command = $event->getCommand(); if ($event instanceof ConsoleErrorEvent) { $this->commands[$event::class] = ['name' => $event->getInput()->getFirstArgument() ?? '', 'command' => $command, 'input' => $this->castInputToString($event->getInput()), 'output' => $this->fetchOutput($event->getOutput()), 'error' => $event->getError()->getMessage(), 'exitCode' => $event->getExitCode()];

$command = $event->getCommand();

Expand All @@ -75,7 +75,7 @@

if ($event instanceof ConsoleTerminateEvent) {
$this->commands[$event::class] = [
'name' => $command?->getName() ?? $event->getInput()->getFirstArgument() ?? '',

Check warning on line 78 in src/Collector/Console/CommandCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Coalesce": --- Original +++ New @@ @@ return; } if ($event instanceof ConsoleTerminateEvent) { - $this->commands[$event::class] = ['name' => $command?->getName() ?? $event->getInput()->getFirstArgument() ?? '', 'command' => $command, 'input' => $this->castInputToString($event->getInput()), 'output' => $this->fetchOutput($event->getOutput()), 'exitCode' => $event->getExitCode()]; + $this->commands[$event::class] = ['name' => $event->getInput()->getFirstArgument() ?? $command?->getName() ?? '', 'command' => $command, 'input' => $this->castInputToString($event->getInput()), 'output' => $this->fetchOutput($event->getOutput()), 'exitCode' => $event->getExitCode()]; return; } $definition = $command?->getDefinition();
'command' => $command,
'input' => $this->castInputToString($event->getInput()),
'output' => $this->fetchOutput($event->getOutput()),
Expand All @@ -86,7 +86,7 @@

$definition = $command?->getDefinition();
$this->commands[$event::class] = [
'name' => $command?->getName() ?? $event->getInput()->getFirstArgument() ?? '',

Check warning on line 89 in src/Collector/Console/CommandCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Coalesce": --- Original +++ New @@ @@ return; } $definition = $command?->getDefinition(); - $this->commands[$event::class] = ['name' => $command?->getName() ?? $event->getInput()->getFirstArgument() ?? '', 'command' => $command, 'input' => $this->castInputToString($event->getInput()), 'output' => $this->fetchOutput($event->getOutput()), 'arguments' => $definition?->getArguments() ?? [], 'options' => $definition?->getOptions() ?? []]; + $this->commands[$event::class] = ['name' => $event->getInput()->getFirstArgument() ?? $command?->getName() ?? '', 'command' => $command, 'input' => $this->castInputToString($event->getInput()), 'output' => $this->fetchOutput($event->getOutput()), 'arguments' => $definition?->getArguments() ?? [], 'options' => $definition?->getOptions() ?? []]; } public function getSummary() : array {
'command' => $command,
'input' => $this->castInputToString($event->getInput()),
'output' => $this->fetchOutput($event->getOutput()),
Expand Down Expand Up @@ -117,13 +117,11 @@
return [];
}

return [

Check warning on line 120 in src/Collector/Console/CommandCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ if ($commandEvent === null) { return []; } - return ['name' => $commandEvent['name'], 'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null, 'input' => $commandEvent['input'], 'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE]; + return ['class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null, 'input' => $commandEvent['input'], 'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE]; } private function reset() : void {
'command' => [
'name' => $commandEvent['name'],
'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null,
'input' => $commandEvent['input'],
'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE,
],
'name' => $commandEvent['name'],
'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null,
'input' => $commandEvent['input'],
'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE,
];
}

Expand Down
20 changes: 9 additions & 11 deletions src/Collector/Console/ConsoleAppInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
return [];
}
return [
'applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted,

Check warning on line 36 in src/Collector/Console/ConsoleAppInfoCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Minus": --- Original +++ New @@ @@ if (!$this->isActive()) { return []; } - return ['applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted, 'preloadTime' => $this->applicationProcessingTimeStarted - $this->requestProcessingTimeStarted, 'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped, 'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted, 'memoryPeakUsage' => memory_get_peak_usage(), 'memoryUsage' => memory_get_usage()]; + return ['applicationProcessingTime' => $this->applicationProcessingTimeStopped + $this->applicationProcessingTimeStarted, 'preloadTime' => $this->applicationProcessingTimeStarted - $this->requestProcessingTimeStarted, 'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped, 'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted, 'memoryPeakUsage' => memory_get_peak_usage(), 'memoryUsage' => memory_get_usage()]; } public function collect(object $event) : void {
'preloadTime' => $this->applicationProcessingTimeStarted - $this->requestProcessingTimeStarted,

Check warning on line 37 in src/Collector/Console/ConsoleAppInfoCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Minus": --- Original +++ New @@ @@ if (!$this->isActive()) { return []; } - return ['applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted, 'preloadTime' => $this->applicationProcessingTimeStarted - $this->requestProcessingTimeStarted, 'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped, 'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted, 'memoryPeakUsage' => memory_get_peak_usage(), 'memoryUsage' => memory_get_usage()]; + return ['applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted, 'preloadTime' => $this->applicationProcessingTimeStarted + $this->requestProcessingTimeStarted, 'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped, 'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted, 'memoryPeakUsage' => memory_get_peak_usage(), 'memoryUsage' => memory_get_usage()]; } public function collect(object $event) : void {
'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped,

Check warning on line 38 in src/Collector/Console/ConsoleAppInfoCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Minus": --- Original +++ New @@ @@ if (!$this->isActive()) { return []; } - return ['applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted, 'preloadTime' => $this->applicationProcessingTimeStarted - $this->requestProcessingTimeStarted, 'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped, 'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted, 'memoryPeakUsage' => memory_get_peak_usage(), 'memoryUsage' => memory_get_usage()]; + return ['applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted, 'preloadTime' => $this->applicationProcessingTimeStarted - $this->requestProcessingTimeStarted, 'applicationEmit' => $this->applicationProcessingTimeStopped + $this->requestProcessingTimeStopped, 'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted, 'memoryPeakUsage' => memory_get_peak_usage(), 'memoryUsage' => memory_get_usage()]; } public function collect(object $event) : void {
'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,

Check warning on line 39 in src/Collector/Console/ConsoleAppInfoCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Minus": --- Original +++ New @@ @@ if (!$this->isActive()) { return []; } - return ['applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted, 'preloadTime' => $this->applicationProcessingTimeStarted - $this->requestProcessingTimeStarted, 'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped, 'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted, 'memoryPeakUsage' => memory_get_peak_usage(), 'memoryUsage' => memory_get_usage()]; + return ['applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted, 'preloadTime' => $this->applicationProcessingTimeStarted - $this->requestProcessingTimeStarted, 'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped, 'requestProcessingTime' => $this->requestProcessingTimeStopped + $this->requestProcessingTimeStarted, 'memoryPeakUsage' => memory_get_peak_usage(), 'memoryUsage' => memory_get_usage()]; } public function collect(object $event) : void {
'memoryPeakUsage' => memory_get_peak_usage(),
'memoryUsage' => memory_get_usage(),
];
Expand Down Expand Up @@ -74,17 +74,15 @@
return [];
}
return [
'console' => [
'php' => [
'version' => PHP_VERSION,
],
'request' => [
'startTime' => $this->requestProcessingTimeStarted,
'processingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
],
'memory' => [
'peakUsage' => memory_get_peak_usage(),
],
'php' => [
'version' => PHP_VERSION,
],
'request' => [
'startTime' => $this->requestProcessingTimeStarted,
'processingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
],
'memory' => [
'peakUsage' => memory_get_peak_usage(),
],
];
}
Expand Down
6 changes: 3 additions & 3 deletions src/Collector/EventCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Yiisoft\Yii\Console\Event\ApplicationStartup as ConsoleApplicationStartup;
use Yiisoft\Yii\Http\Event\ApplicationStartup as HttpApplicationStartup;

use function count;

final class EventCollector implements SummaryCollectorInterface
{
use CollectorTrait;
Expand Down Expand Up @@ -53,9 +55,7 @@ public function getSummary(): array
return [];
}
return [
'event' => [
'total' => count($this->events),
],
'total' => count($this->events),
];
}

Expand Down
15 changes: 8 additions & 7 deletions src/Collector/ExceptionCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ public function getSummary(): array
if (!$this->isActive()) {
return [];
}
if ($this->exception === null) {
return [];
}
return [
'exception' => $this->exception === null ? [] : [
'class' => $this->exception::class,
'message' => $this->exception->getMessage(),
'file' => $this->exception->getFile(),
'line' => $this->exception->getLine(),
'code' => $this->exception->getCode(),
],
'class' => $this->exception::class,
'message' => $this->exception->getMessage(),
'file' => $this->exception->getFile(),
'line' => $this->exception->getLine(),
'code' => $this->exception->getCode(),
];
}

Expand Down
18 changes: 8 additions & 10 deletions src/Collector/HttpClientCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,15 @@ public function getSummary(): array
return [];
}
return [
'http' => [
'count' => array_sum(array_map(static fn (array $requests) => count($requests), $this->requests)),
'totalTime' => array_sum(
array_merge(
...array_map(
static fn (array $entry) => array_column($entry, 'totalTime'),
array_values($this->requests)
)
'count' => array_sum(array_map(static fn (array $requests) => count($requests), $this->requests)),
'totalTime' => array_sum(
array_merge(
...array_map(
static fn (array $entry) => array_column($entry, 'totalTime'),
array_values($this->requests)
)
),
],
)
),
];
}
}
6 changes: 3 additions & 3 deletions src/Collector/LogCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Yii\Debug\Collector;

use function count;

class LogCollector implements SummaryCollectorInterface
{
use CollectorTrait;
Expand Down Expand Up @@ -50,9 +52,7 @@ public function getSummary(): array
return [];
}
return [
'logger' => [
'total' => count($this->messages),
],
'total' => count($this->messages),
];
}
}
6 changes: 3 additions & 3 deletions src/Collector/ServiceCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Yii\Debug\Collector;

use function count;

final class ServiceCollector implements SummaryCollectorInterface
{
use CollectorTrait;
Expand Down Expand Up @@ -58,9 +60,7 @@ public function getSummary(): array
return [];
}
return [
'service' => [
'total' => count($this->items),
],
'total' => count($this->items),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Collector/Stream/FilesystemStreamCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function getSummary(): array
return [];
}
return [
'fs_stream' => array_merge(
'streams' => array_merge(
...array_map(
fn (string $operation) => [$operation => count($this->operations[$operation])],
array_keys($this->operations)
Expand Down
2 changes: 1 addition & 1 deletion src/Collector/Stream/HttpStreamCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function getSummary(): array
return [];
}
return [
'http_stream' => array_merge(
'streams' => array_merge(
...array_map(
fn (string $operation) => [
$operation => count($this->requests[$operation]),
Expand Down
6 changes: 3 additions & 3 deletions src/Collector/VarDumperCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Yii\Debug\Collector;

use function count;

final class VarDumperCollector implements SummaryCollectorInterface
{
use CollectorTrait;
Expand Down Expand Up @@ -40,9 +42,7 @@ public function getSummary(): array
}

return [
'var-dumper' => [
'total' => count($this->vars),
],
'total' => count($this->vars),
];
}
}
20 changes: 9 additions & 11 deletions src/Collector/Web/WebAppInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,15 @@ public function getSummary(): array
return [];
}
return [
'web' => [
'php' => [
'version' => PHP_VERSION,
],
'request' => [
'startTime' => $this->requestProcessingTimeStarted,
'processingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
],
'memory' => [
'peakUsage' => memory_get_peak_usage(),
],
'php' => [
'version' => PHP_VERSION,
],
'request' => [
'startTime' => $this->requestProcessingTimeStarted,
'processingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
],
'memory' => [
'peakUsage' => memory_get_peak_usage(),
],
];
}
Expand Down
24 changes: 20 additions & 4 deletions src/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@

/**
* Debugger collects data from collectors and stores it in a storage.
*
* @psalm-type TSummary = array{
* id: non-empty-string,
* collectors: list<non-empty-string>,
* summary: array<non-empty-string, array>,
* }
*/
final class Debugger
{
/**
* @var CollectorInterface[] Collectors, indexed by their names.
*
* @psalm-var array<string, CollectorInterface>
* @psalm-var array<non-empty-string, CollectorInterface>
*/
private readonly array $collectors;

Expand All @@ -31,7 +37,7 @@ final class Debugger
private readonly DataNormalizer $dataNormalizer;

/**
* @var string|null ID of the current request. `null` if debugger is not active.
* @var non-empty-string|null ID of the current request. `null` if debugger is not active.
*/
private ?string $id = null;

Expand Down Expand Up @@ -83,6 +89,8 @@ public function isActive(): bool
* Throws `LogicException` if debugger is not started. Use {@see isActive()} to check if debugger is active.
*
* @return string ID of the current request.
*
* @psalm-return non-empty-string
*/
public function getId(): string
{
Expand Down Expand Up @@ -110,6 +118,7 @@ public function start(object $event): void
return;
}

/** @var non-empty-string */
$this->id = str_replace('.', '', uniqid('', true));

foreach ($this->collectors as $collector) {
Expand Down Expand Up @@ -178,18 +187,25 @@ private function deactivate(): void
}

/**
* Collects summary data of current request.
* Collects summary data of current request. Structure of the summary data is:
*
* - `id` - ID of the current request,
* - `collectors` - list of collector names used in the current request,
* - `summary` - summary data collected by collectors indexed by collector names.
*
* @psalm-return TSummary
*/
private function collectSummaryData(): array
{
$summaryData = [
'id' => $this->getId(),
'collectors' => array_keys($this->collectors),
'summary' => [],
];

foreach ($this->collectors as $collector) {
if ($collector instanceof SummaryCollectorInterface) {
$summaryData = [...$summaryData, ...$collector->getSummary()];
$summaryData['summary'][$collector->getName()] = $collector->getSummary();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ interface StorageInterface
/**
* Read all data from storage
*
* @param string $type type of data being read. Available types:
* @param string $type Type of data being read. Available types:
* - {@see TYPE_SUMMARY}
* - {@see TYPE_DATA}
* - {@see TYPE_OBJECTS}
*
* @return array data from storage
* @return array Data from storage
*
* @psalm-param self::TYPE_* $type
*/
Expand Down
9 changes: 4 additions & 5 deletions tests/Unit/Collector/CommandCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ protected function checkCollectedData(array $data): void

protected function checkSummaryData(array $data): void
{
$this->assertArrayHasKey('command', $data);
$this->assertArrayHasKey('input', $data['command']);
$this->assertArrayHasKey('class', $data['command']);
$this->assertEquals('test1', $data['command']['input']);
$this->assertEquals(null, $data['command']['class']);
$this->assertArrayHasKey('input', $data);
$this->assertArrayHasKey('class', $data);
$this->assertEquals('test1', $data['input']);
$this->assertEquals(null, $data['class']);
}
}
17 changes: 8 additions & 9 deletions tests/Unit/Collector/ConsoleAppInfoCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ protected function checkSummaryData(array $data): void
{
parent::checkSummaryData($data);

$this->assertArrayHasKey('console', $data);
$this->assertArrayHasKey('php', $data['console']);
$this->assertArrayHasKey('version', $data['console']['php']);
$this->assertArrayHasKey('request', $data['console']);
$this->assertArrayHasKey('startTime', $data['console']['request']);
$this->assertArrayHasKey('processingTime', $data['console']['request']);
$this->assertArrayHasKey('memory', $data['console']);
$this->assertArrayHasKey('peakUsage', $data['console']['memory']);
$this->assertArrayHasKey('php', $data);
$this->assertArrayHasKey('version', $data['php']);
$this->assertArrayHasKey('request', $data);
$this->assertArrayHasKey('startTime', $data['request']);
$this->assertArrayHasKey('processingTime', $data['request']);
$this->assertArrayHasKey('memory', $data);
$this->assertArrayHasKey('peakUsage', $data['memory']);

$this->assertEquals(PHP_VERSION, $data['console']['php']['version']);
$this->assertEquals(PHP_VERSION, $data['php']['version']);
}
}
Loading