Skip to content

Commit

Permalink
Merge pull request #9 from robiningelbrecht/output-execution-time
Browse files Browse the repository at this point in the history
Output execution time
  • Loading branch information
robiningelbrecht authored Sep 20, 2023
2 parents f0c6672 + 1c35b19 commit a1aec59
Show file tree
Hide file tree
Showing 18 changed files with 296 additions and 105 deletions.
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,5 @@
<include>
<directory>src</directory>
</include>
<exclude>
<file>src/Exitter.php</file>
</exclude>
</source>
</phpunit>
17 changes: 15 additions & 2 deletions src/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\MinCoverageResult;
use RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\ResultStatus;
use RobinIngelbrecht\PHPUnitCoverageTools\Timer\ResourceUsageFormatter;
use RobinIngelbrecht\PHPUnitCoverageTools\Timer\SystemResourceUsageFormatter;
use SebastianBergmann\Timer\Duration;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
Expand All @@ -12,10 +15,11 @@
use Symfony\Component\Console\Helper\TableStyle;
use Symfony\Component\Console\Output\OutputInterface;

class ConsoleOutput
final class ConsoleOutput
{
public function __construct(
private readonly OutputInterface $output,
private readonly ResourceUsageFormatter $resourceUsageFormatter,
) {
$this->output->setDecorated(true);
$this->output->getFormatter()->setStyle(
Expand All @@ -36,10 +40,18 @@ public function __construct(
);
}

public static function create(): self
{
return new self(
output: new \Symfony\Component\Console\Output\ConsoleOutput(),
resourceUsageFormatter: SystemResourceUsageFormatter::create()
);
}

/**
* @param \RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\MinCoverageResult[] $results
*/
public function print(array $results): void
public function print(array $results, Duration $duration): void
{
$statusWeights = array_map(fn (MinCoverageResult $result) => $result->getStatus()->getWeight(), $results);
$finalStatus = ResultStatus::fromWeight(max($statusWeights));
Expand Down Expand Up @@ -99,5 +111,6 @@ public function print(array $results): void
],
]);
$table->render();
$this->output->writeln($this->resourceUsageFormatter->resourceUsage($duration));
}
}
3 changes: 3 additions & 0 deletions src/Exitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace RobinIngelbrecht\PHPUnitCoverageTools;

/**
* @codeCoverageIgnore
*/
class Exitter
{
public function exit(): void
Expand Down
23 changes: 19 additions & 4 deletions src/Subscriber/Application/ApplicationFinishedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\MinCoverageRule;
use RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\MinCoverageRules;
use RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\ResultStatus;
use RobinIngelbrecht\PHPUnitCoverageTools\Timer\SystemTimer;
use RobinIngelbrecht\PHPUnitCoverageTools\Timer\Timer;
use Symfony\Component\Console\Helper\FormatterHelper;

final class ApplicationFinishedSubscriber extends FormatterHelper implements FinishedSubscriber
Expand All @@ -24,11 +26,13 @@ public function __construct(
private readonly bool $cleanUpCloverXml,
private readonly Exitter $exitter,
private readonly ConsoleOutput $consoleOutput,
private readonly Timer $timer,
) {
}

public function notify(Finished $event): void
{
$this->timer->start();
/** @var string $reflectionFileName */
$reflectionFileName = (new \ReflectionClass(ClassLoader::class))->getFileName();
$absolutePathToCloverXml = dirname($reflectionFileName, 3).'/'.$this->relativePathToCloverXml;
Expand All @@ -48,15 +52,21 @@ public function notify(Finished $event): void
if ($this->minCoverageRules->hasTotalRule() && \XMLReader::ELEMENT == $reader->nodeType && 'metrics' == $reader->name && 2 === $reader->depth) {
/** @var \SimpleXMLElement $node */
$node = simplexml_load_string($reader->readOuterXml());
$metricTotal = CoverageMetric::fromCloverXmlNode($node, MinCoverageRule::TOTAL);
$metricTotal = CoverageMetric::fromCloverXmlNode(
node: $node,
forClass: MinCoverageRule::TOTAL
);
continue;
}
if ($this->minCoverageRules->hasOtherRulesThanTotalRule() && \XMLReader::ELEMENT == $reader->nodeType && 'class' == $reader->name && 3 === $reader->depth) {
/** @var \SimpleXMLElement $node */
$node = simplexml_load_string($reader->readInnerXml());
/** @var string $className */
$className = $reader->getAttribute('name');
$metrics[] = CoverageMetric::fromCloverXmlNode($node, $className);
$metrics[] = CoverageMetric::fromCloverXmlNode(
node: $node,
forClass: $className
);
}
}
$reader->close();
Expand All @@ -74,7 +84,11 @@ public function notify(Finished $event): void
metrics: $metrics,
metricTotal: $metricTotal,
);
$this->consoleOutput->print($results);

$this->consoleOutput->print(
results: $results,
duration: $this->timer->stop()
);

$needsExit = !empty(array_filter(
$results,
Expand Down Expand Up @@ -134,7 +148,8 @@ public static function fromConfigurationAndParameters(
minCoverageRules: $rules,
cleanUpCloverXml: $cleanUpCloverXml,
exitter: new Exitter(),
consoleOutput: new ConsoleOutput(new \Symfony\Component\Console\Output\ConsoleOutput()),
consoleOutput: ConsoleOutput::create(),
timer: SystemTimer::create(),
);
}
}
12 changes: 12 additions & 0 deletions src/Timer/ResourceUsageFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace RobinIngelbrecht\PHPUnitCoverageTools\Timer;

use SebastianBergmann\Timer\Duration;

interface ResourceUsageFormatter
{
public function resourceUsage(Duration $duration): string;

public function resourceUsageSinceStartOfRequest(): string;
}
35 changes: 35 additions & 0 deletions src/Timer/SystemResourceUsageFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace RobinIngelbrecht\PHPUnitCoverageTools\Timer;

use SebastianBergmann\Timer\Duration;
use SebastianBergmann\Timer\ResourceUsageFormatter as PhpUnitResourceUsageFormatter;

final class SystemResourceUsageFormatter implements ResourceUsageFormatter
{
private function __construct(
private readonly PhpUnitResourceUsageFormatter $resourceUsageFormatter
) {
}

public static function create(): self
{
return new self(new PhpUnitResourceUsageFormatter());
}

/**
* @codeCoverageIgnore
*/
public function resourceUsage(Duration $duration): string
{
return $this->resourceUsageFormatter->resourceUsage($duration);
}

/**
* @codeCoverageIgnore
*/
public function resourceUsageSinceStartOfRequest(): string
{
return $this->resourceUsageFormatter->resourceUsageSinceStartOfRequest();
}
}
35 changes: 35 additions & 0 deletions src/Timer/SystemTimer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace RobinIngelbrecht\PHPUnitCoverageTools\Timer;

use SebastianBergmann\Timer\Duration;
use SebastianBergmann\Timer\Timer as PhpUnitTimer;

final class SystemTimer implements Timer
{
private function __construct(
private readonly PhpUnitTimer $timer,
) {
}

public static function create(): self
{
return new self(new PhpUnitTimer());
}

/**
* @codeCoverageIgnore
*/
public function start(): void
{
$this->timer->start();
}

/**
* @codeCoverageIgnore
*/
public function stop(): Duration
{
return $this->timer->stop();
}
}
12 changes: 12 additions & 0 deletions src/Timer/Timer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace RobinIngelbrecht\PHPUnitCoverageTools\Timer;

use SebastianBergmann\Timer\Duration;

interface Timer
{
public function start(): void;

public function stop(): Duration;
}
36 changes: 36 additions & 0 deletions tests/FixedResourceUsageFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Tests;

use RobinIngelbrecht\PHPUnitCoverageTools\Timer\ResourceUsageFormatter;
use SebastianBergmann\Timer\Duration;

final class FixedResourceUsageFormatter implements ResourceUsageFormatter
{
private function __construct(
private readonly float $usageInMb
) {
}

public static function withUsageInMb(float $usageInMb): self
{
return new self($usageInMb);
}

public function resourceUsage(Duration $duration): string
{
return sprintf(
'Time: %s, Memory: %s MB',
$duration->asString(),
number_format($this->usageInMb, 2, '.', ''),
);
}

public function resourceUsageSinceStartOfRequest(): string
{
return sprintf(
'Time: 00:00.350, Memory: %s MB',
number_format($this->usageInMb, 2, '.', ''),
);
}
}
28 changes: 28 additions & 0 deletions tests/PausedTimer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests;

use RobinIngelbrecht\PHPUnitCoverageTools\Timer\Timer;
use SebastianBergmann\Timer\Duration;

final class PausedTimer implements Timer
{
private function __construct(
private readonly Duration $duration
) {
}

public static function withDuration(Duration $duration): self
{
return new self($duration);
}

public function start(): void
{
}

public function stop(): Duration
{
return $this->duration;
}
}
Loading

0 comments on commit a1aec59

Please sign in to comment.