Skip to content

Commit

Permalink
Added customization support for performance thresholds.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jul 21, 2024
1 parent d9a3951 commit 6f5a87e
Show file tree
Hide file tree
Showing 27 changed files with 113 additions and 31 deletions.
File renamed without changes.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The following preview is somewhat atypical but shows all supported output cases

![Preview image][]

This printer makes no attempt to modify the test summary; only runtime output is changed.
Pip makes no attempt to modify the test summary; only runtime output is changed.

## Usage

Expand All @@ -51,13 +51,22 @@ This printer makes no attempt to modify the test summary; only runtime output is

4. Enjoy immediate test execution feedback.

### Configuration

Pip's behaviour can be customized by adding `<parameter>` nodes as children of the `<bootstrap>` node in `phpunit.xml`, with `name` and `value` attributes corresponding to the table below.
| Parameter name | Default value | Comments |
|----------------|---------------|--------------------------------------------------|
| perf.slow | 200 (ms) | _Slow_ performance threshold (shown in yellow) |
| perf.vslow | 1000 (ms) | _Very slow_ performance threshold (shown in red) |
## Requirements
| Pip version | PHPUnit versions | Minimum PHP |
|:-----------:|:----------------:|:-----------:|
| 3 | 10 / 11 | 8.1 / 8.2 |
| 2 | *yanked* | - |
| 1 | 5 / 6 | 5.6 / 7.0 |
| Pip version | PHPUnit versions | Minimum PHP version |
|:-----------:|:----------------:|:-------------------:|
| 3 | 10 / 11 | 8.1 / 8.2 |
| 2 | *yanked* | - |
| 1 | 5 / 6 | 5.6 / 7.0 |
## Testing
Expand Down Expand Up @@ -94,8 +103,8 @@ Thanks to the following open source projects that inspired this project. Keep be
[Version image]: https://poser.pugx.org/scriptfusion/pip/version "Latest version"
[Downloads]: https://packagist.org/packages/scriptfusion/pip
[Downloads image]: https://poser.pugx.org/scriptfusion/pip/downloads "Total downloads"
[Build]: https://github.com/ScriptFUSION/PHPUnit-Immediate-Printer/actions/workflows/Test.yaml
[Build image]: https://github.com/ScriptFUSION/PHPUnit-Immediate-Printer/actions/workflows/Test.yaml/badge.svg "Build status"
[Build]: https://github.com/ScriptFUSION/PHPUnit-Immediate-Printer/actions/workflows/Tests.yaml
[Build image]: https://github.com/ScriptFUSION/PHPUnit-Immediate-Printer/actions/workflows/Tests.yaml/badge.svg "Build status"
[Coverage]: https://codecov.io/gh/ScriptFUSION/PHPUnit-Immediate-Printer
[Coverage image]: https://codecov.io/github/ScriptFUSION/PHPUnit-Immediate-Printer/graph/badge.svg "Test coverage"

Expand Down
10 changes: 10 additions & 0 deletions src/PipConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace ScriptFUSION\Pip;

final class PipConfig
{
public int $perfSlow = 200;
public int $perfVslow = 1_000;
}
6 changes: 5 additions & 1 deletion src/PipExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ final class PipExtension implements Extension
{
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
{
$facade->registerTracer(new Printer());
$config = new PipConfig();
$parameters->has('perf.slow') && $config->perfSlow = +$parameters->get('perf.slow');
$parameters->has('perf.vslow') && $config->perfVslow = +$parameters->get('perf.vslow');

$facade->registerTracer(new Printer($config));
$facade->replaceProgressOutput();
}
}
15 changes: 10 additions & 5 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@

final class Printer implements Tracer
{
private array $performanceThresholds = [
'red' => 1000,
'yellow' => 200,
'green' => 0,
];
private readonly array $performanceThresholds;

private int $totalTests;

Expand All @@ -41,6 +37,15 @@ final class Printer implements Tracer

private bool $flawless = true;

public function __construct(private readonly PipConfig $config)
{
$this->performanceThresholds = [
'red' => $config->perfVslow,
'yellow' => $config->perfSlow,
'green' => 0,
];
}

public function trace(Event $event): void
{
if ($event instanceof ExecutionStarted) {
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions test/config/PerformanceThresholds.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<phpunit>
<extensions>
<bootstrap class="ScriptFUSION\Pip\PipExtension">
<parameter name="perf.slow" value="300"/>
<parameter name="perf.vslow" value="600"/>
</bootstrap>
</extensions>
</phpunit>
23 changes: 23 additions & 0 deletions test/config/PerformanceThresholdsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace ScriptFUSIONTest\Pip\config;

use PHPUnit\Framework\TestCase;

final class PerformanceThresholdsTest extends TestCase
{
public function testSlow(): void
{
usleep(300_000);

self::assertTrue(true);
}

public function testVslow(): void
{
usleep(600_000);

self::assertTrue(true);
}
}
22 changes: 22 additions & 0 deletions test/config/performance thresholds.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Tests that when custom performance thresholds are specified, they overrides the defaults.

--ARGS--
-c test/config/PerformanceThresholds.xml --colors=always test/config/PerformanceThresholdsTest.php

--FILE_EXTERNAL--
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s

Runtime: %s
Configuration: %s

50% . ScriptFUSIONTest\Pip\config\PerformanceThresholdsTest::testSlow (%d ms)
100% . ScriptFUSIONTest\Pip\config\PerformanceThresholdsTest::testVslow (%d ms)


Time: %s

OK (2 tests, 2 assertions)
2 changes: 1 addition & 1 deletion test/functional/data provider.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A successful test is fed two cases by a data provider.
-c test --colors=always test/CapabilitiesTest.php --filter '::testDataProvider\h'

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/deprecation.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A test is marked "deprecated".
-c test --colors=always test/CapabilitiesTest.php --filter ::testDeprecation$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/diff failure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ An assertion fails and produces a diff.
-c test --colors=always test/CapabilitiesTest.php --filter ::testDiffFailure$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/exception.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ An exception is thrown in the test case.
-c test --colors=always test/CapabilitiesTest.php --filter ::testException$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/failure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ An assertion fails.
-c test --colors=always test/CapabilitiesTest.php --filter ::testFailure$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/incomplete.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A test is marked "incomplete".
-c test --colors=always test/CapabilitiesTest.php --filter ::testIncomplete$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/multiple files.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tests that multiple files tested together are counted correctly.
-c test --colors=always test/multi

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/nested exception.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ An exception containing another exception is thrown by another class.
-c test --colors=always test/CapabilitiesTest.php --filter ::testNestedException$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/notice.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A test is marked "notice".
-c test --colors=always test/CapabilitiesTest.php --filter ::testNotice$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/repeat.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ An successful test is repeated 80 times.
-c test --colors=always test/CapabilitiesTest.php --filter ::testSuccess$ --repeat 80

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/risky.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A test is marked "risky".
-c test --colors=always test/CapabilitiesTest.php --filter ::testRisky$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/skipped.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A test is skipped.
-c test --colors=always test/CapabilitiesTest.php --filter ::testSkipped$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/slow.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tests that when a test takes more than 200ms to complete, its timer output is sh
-c test --colors=always test/CapabilitiesTest.php --filter ::testSlow$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/success after failure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Successful tests following a failure have coloured progress marker.
-c test --colors=always test/CapabilitiesTest.php --filter '::testSuccessAfterFailure\h'

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/success.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ An assertion succeeds.
-c test --colors=always test/CapabilitiesTest.php --filter ::testSuccess$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/vslow.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tests that when a test takes more than 1s to complete, its timer output is shown
-c test --colors=always test/CapabilitiesTest.php --filter ::testGigaSlow$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
2 changes: 1 addition & 1 deletion test/functional/warning.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A test is marked "warning".
-c test --colors=always test/CapabilitiesTest.php --filter ::testWarning$

--FILE_EXTERNAL--
PHPUnit runner.php
../PHPUnit runner.php

--EXPECTF--
PHPUnit %s
Expand Down
1 change: 1 addition & 0 deletions test/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<testsuites>
<testsuite name="Default">
<directory suffix=".phpt">functional</directory>
<directory suffix=".phpt">config</directory>
</testsuite>
</testsuites>

Expand Down

0 comments on commit 6f5a87e

Please sign in to comment.