Skip to content

Commit 3c15eb5

Browse files
authored
Merge pull request #66 from johnkary/master-envVarDisable
Environment variable PHPUNIT_SPEEDTRAP="disabled" can disable profiling
2 parents cd77276 + 7f25cb9 commit 3c15eb5

File tree

3 files changed

+123
-10
lines changed

3 files changed

+123
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ https://github.com/johnkary/phpunit-speedtrap/commit/XXX where XXX is the commit
77
View diff between two versions:
88
https://github.com/johnkary/phpunit-speedtrap/compare/v3.1.0...v3.2.0
99

10+
## 4.0 (xxxx-xx-xx)
11+
12+
* [PR #66](https://github.com/johnkary/phpunit-speedtrap/pull/66) Environment variable PHPUNIT_SPEEDTRAP="disabled" can disable profiling
13+
1014
## 3.2.0 (2020-02-12)
1115

1216
Version 3.2 introduces supports for PHPUnit 9.0+.

README.md

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SpeedTrap helps **identify slow tests** but cannot explain **why** those tests a
1212

1313
## Installation
1414

15-
SpeedTrap is installable via [Composer](http://getcomposer.org) and should be added as a `require-dev` dependency:
15+
SpeedTrap is installed using [Composer](http://getcomposer.org). Add it as a `require-dev` dependency:
1616

1717
composer require --dev johnkary/phpunit-speedtrap
1818

@@ -30,16 +30,16 @@ Enable with all defaults by adding the following code to your project's `phpunit
3030
</phpunit>
3131
```
3232

33-
Now run the test suite as normal. If one or more test executions exceed the slowness threshold (500ms by default), SpeedTrap will report on those tests in the console after all tests have completed.
33+
Now run the test suite. If one or more test executions exceed the slowness threshold (500ms by default), SpeedTrap will report on those tests in the console after all tests have completed.
3434

35-
## Configuration
35+
## Config Parameters
3636

37-
SpeedTrap has two configurable parameters:
37+
SpeedTrap also supports these parameters:
3838

39-
* **slowThreshold** - Number of milliseconds a test takes to execute before being considered "slow" (Default: 500ms)
39+
* **slowThreshold** - Number of milliseconds when a test is considered "slow" (Default: 500ms)
4040
* **reportLength** - Number of slow tests included in the report (Default: 10 tests)
4141

42-
These configuration parameters are set in `phpunit.xml` when adding the listener:
42+
Each parameter is set in `phpunit.xml`:
4343

4444
```xml
4545
<phpunit bootstrap="vendor/autoload.php">
@@ -62,13 +62,11 @@ These configuration parameters are set in `phpunit.xml` when adding the listener
6262
</phpunit>
6363
```
6464

65-
This allows customizing what the project considers a "slow" test and how many are reported on to project maintainers.
66-
6765
## Custom slowness threshold per-test case
6866

6967
Some projects have a few complex tests that take a long time to run. It is possible to set a different slowness threshold for individual test cases.
7068

71-
Use the annotation `@slowThreshold` to set a custom slowness threshold for single test cases. This number may be higher or lower than the default threshold and will be used in place of the default threshold for that specific test.
69+
The annotation `@slowThreshold` can set a custom slowness threshold for each test case. This number may be higher or lower than the default threshold and is used instead of the default threshold for that specific test.
7270

7371
```php
7472
class SomeTestCase extends PHPUnit\Framework\TestCase
@@ -83,9 +81,102 @@ class SomeTestCase extends PHPUnit\Framework\TestCase
8381
}
8482
```
8583

84+
## Disable slowness profiling using an environment variable
85+
86+
SpeedTrapListener profiles for slow tests when enabled in phpunit.xml. But using an environment variable named `PHPUNIT_SPEEDTRAP` can enable or disable the listener.
87+
88+
$ PHPUNIT_SPEEDTRAP="disabled" ./vendor/bin/phpunit
89+
90+
#### Use case: Disable profiling in development, but profile with Travis CI
91+
92+
Travis CI is popular for running tests in the cloud after pushing new code to a repository.
93+
94+
Step 1) Enable SpeedTrapListener in phpunit.xml, but set `PHPUNIT_SPEEDTRAP="disabled"` to disable profiling when running tests.
95+
96+
```xml
97+
<phpunit bootstrap="vendor/autoload.php">
98+
...
99+
<php>
100+
<env name="PHPUNIT_SPEEDTRAP" value="disabled" />
101+
</php>
102+
103+
<listeners>
104+
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
105+
</listeners>
106+
</phpunit>
107+
```
108+
109+
Step 2) Configure `.travis.yml` with `PHPUNIT_SPEEDTRAP="enabled"` to profile for slow tests when running on Travis CI:
110+
111+
```yaml
112+
language: php
113+
114+
php:
115+
- 7.3
116+
117+
env:
118+
- PHPUNIT_SPEEDTRAP="enabled"
119+
```
120+
121+
Step 3) View the Travis CI build output and read the slowness report printed in the console.
122+
123+
[Travis CI Documentation - Environment Variables](https://docs.travis-ci.com/user/environment-variables)
124+
125+
#### Use case: Enable profiling in development, but disable with Travis CI
126+
127+
Step 1) Enable SpeedTrapListener in phpunit.xml. The slowness report will output during all test suite executions.
128+
129+
```xml
130+
<phpunit bootstrap="vendor/autoload.php">
131+
...
132+
<listeners>
133+
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
134+
</listeners>
135+
</phpunit>
136+
```
137+
138+
Step 2) Configure `.travis.yml` with `PHPUNIT_SPEEDTRAP="disabled"` to turn off profiling when running on Travis CI:
139+
140+
```yaml
141+
language: php
142+
143+
php:
144+
- 7.3
145+
146+
env:
147+
- PHPUNIT_SPEEDTRAP="disabled"
148+
```
149+
150+
Step 3) View the Travis CI build output and confirm the slowness report is not printed in the console.
151+
152+
#### Use case: Only enable SpeedTrapListener on demand via command-line
153+
154+
Useful when you only want to profile slow tests once in a while.
155+
156+
Step 1) Setup phpunit.xml to enable SpeedTrapListener, but disable slowness profiling by setting `PHPUNIT_SPEEDTRAP="disabled"` like this:
157+
158+
```xml
159+
<phpunit bootstrap="vendor/autoload.php">
160+
...
161+
<php>
162+
<env name="PHPUNIT_SPEEDTRAP" value="disabled" />
163+
</php>
164+
165+
<listeners>
166+
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
167+
</listeners>
168+
</phpunit>
169+
```
170+
171+
Step 2) When executing `phpunit` from the command-line, enable slowness profiling only for this run by passing the environment variable `PHPUNIT_SPEEDTRAP="enabled"` like this:
172+
173+
```bash
174+
$ PHPUNIT_SPEEDTRAP=enabled ./vendor/bin/phpunit
175+
```
176+
86177
## Inspiration
87178

88-
This project was inspired by [RSpec's](https://github.com/rspec/rspec) `--profile` option that displays feedback about slow tests.
179+
SpeedTrap was inspired by [RSpec's](https://github.com/rspec/rspec) `--profile` option that displays feedback about slow tests.
89180

90181
## License
91182

src/SpeedTrapListener.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ class SpeedTrapListener implements TestListener
1313
{
1414
use TestListenerDefaultImplementation;
1515

16+
/**
17+
* Slowness profiling enabled by default. Set to false to disable profiling
18+
* and reporting.
19+
*
20+
* Use environment variable "PHPUNIT_SPEEDTRAP" set to value "disabled" to
21+
* disable profiling.
22+
*
23+
* @var boolean
24+
*/
25+
protected $enabled = true;
26+
1627
/**
1728
* Internal tracking for test suites.
1829
*
@@ -45,6 +56,8 @@ class SpeedTrapListener implements TestListener
4556

4657
public function __construct(array $options = [])
4758
{
59+
$this->enabled = getenv('PHPUNIT_SPEEDTRAP') === 'disabled' ? false : true;
60+
4861
$this->loadOptions($options);
4962
}
5063

@@ -56,6 +69,7 @@ public function __construct(array $options = [])
5669
*/
5770
public function endTest(Test $test, float $time): void
5871
{
72+
if (!$this->enabled) return;
5973
if (!$test instanceof TestCase) return;
6074

6175
$timeInMilliseconds = $this->toMilliseconds($time);
@@ -73,6 +87,8 @@ public function endTest(Test $test, float $time): void
7387
*/
7488
public function startTestSuite(TestSuite $suite): void
7589
{
90+
if (!$this->enabled) return;
91+
7692
$this->suites++;
7793
}
7894

@@ -83,6 +99,8 @@ public function startTestSuite(TestSuite $suite): void
8399
*/
84100
public function endTestSuite(TestSuite $suite): void
85101
{
102+
if (!$this->enabled) return;
103+
86104
$this->suites--;
87105

88106
if (0 === $this->suites && $this->hasSlowTests()) {

0 commit comments

Comments
 (0)