Skip to content

Commit 41ac463

Browse files
committed
Util\Timing: add tests
Includes minor fixes to the class documentation.
1 parent 7f39f60 commit 41ac463

File tree

3 files changed

+241
-4
lines changed

3 files changed

+241
-4
lines changed

src/Util/Timing.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Timing
1313
{
1414

1515
/**
16-
* The start time of the run.
16+
* The start time of the run in microseconds.
1717
*
1818
* @var float
1919
*/
@@ -43,7 +43,7 @@ public static function startTiming()
4343
/**
4444
* Get the duration of the run up to "now".
4545
*
46-
* @return float Duration in microseconds.
46+
* @return float Duration in milliseconds.
4747
*/
4848
public static function getDuration()
4949
{
@@ -58,9 +58,9 @@ public static function getDuration()
5858

5959

6060
/**
61-
* Convert a duration in microseconds to a human readable duration string.
61+
* Convert a duration in milliseconds to a human readable duration string.
6262
*
63-
* @param float $duration Duration in microseconds.
63+
* @param float $duration Duration in milliseconds.
6464
*
6565
* @return string
6666
*/
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Util\Timing::getHumanReadableDuration() method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2024 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Util\Timing;
11+
12+
use PHP_CodeSniffer\Util\Timing;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Tests for the \PHP_CodeSniffer\Util\Timing::getHumanReadableDuration() method.
17+
*
18+
* @covers \PHP_CodeSniffer\Util\Timing::getHumanReadableDuration
19+
*/
20+
final class GetHumanReadableDurationTest extends TestCase
21+
{
22+
23+
24+
/**
25+
* Test the method.
26+
*
27+
* @param int|float $duration A duration in milliseconds.
28+
* @param string $expected The expected human readable string.
29+
*
30+
* @dataProvider dataGetHumanReadableDuration
31+
*
32+
* @return void
33+
*/
34+
public function testGetHumanReadableDuration($duration, $expected)
35+
{
36+
$this->assertSame($expected, Timing::getHumanReadableDuration($duration));
37+
38+
}//end testGetHumanReadableDuration()
39+
40+
41+
/**
42+
* Data provider.
43+
*
44+
* @return array<string, array<string, int|float|string>>
45+
*/
46+
public static function dataGetHumanReadableDuration()
47+
{
48+
return [
49+
'Duration: 0' => [
50+
'duration' => 0,
51+
'expected' => '0ms',
52+
],
53+
'Duration: 13 milliseconds' => [
54+
'duration' => 13.232101565,
55+
'expected' => '13ms',
56+
],
57+
'Duration: 14 milliseconds' => [
58+
'duration' => 13.916015625,
59+
'expected' => '14ms',
60+
],
61+
'Duration: 999 milliseconds' => [
62+
'duration' => 999.2236,
63+
'expected' => '999ms',
64+
],
65+
'Duration: 999+ milliseconds' => [
66+
'duration' => 999.89236,
67+
'expected' => '1000ms',
68+
],
69+
'Duration: 1 second' => [
70+
'duration' => 1000,
71+
'expected' => '1000ms',
72+
],
73+
'Duration: slightly more than 1 second' => [
74+
'duration' => 1001.178215,
75+
'expected' => '1 secs',
76+
],
77+
'Duration: just under a 1 minute' => [
78+
'duration' => 59999.3851,
79+
'expected' => '60 secs',
80+
],
81+
'Duration: exactly 1 minute' => [
82+
'duration' => 60000,
83+
'expected' => '60 secs',
84+
],
85+
'Duration: slightly more than 1 minute' => [
86+
'duration' => 60001.7581235,
87+
'expected' => '1 mins, 0 secs',
88+
],
89+
'Duration: 1 minute, just under half a second' => [
90+
'duration' => 60499.83639,
91+
'expected' => '1 mins, 0.5 secs',
92+
],
93+
'Duration: 1 minute, just over half a second' => [
94+
'duration' => 60501.961238,
95+
'expected' => '1 mins, 0.5 secs',
96+
],
97+
'Duration: 1 minute, 1 second' => [
98+
'duration' => 61000,
99+
'expected' => '1 mins, 1 secs',
100+
],
101+
'Duration: exactly 1 hour' => [
102+
'duration' => 3600000,
103+
'expected' => '60 mins, 0 secs',
104+
],
105+
'Duration: 89.4 mins' => [
106+
'duration' => 5364000,
107+
'expected' => '89 mins, 24 secs',
108+
],
109+
];
110+
111+
}//end dataGetHumanReadableDuration()
112+
113+
114+
}//end class

tests/Core/Util/Timing/TimingTest.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Util\Timing class.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2024 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Util\Timing;
11+
12+
use PHP_CodeSniffer\Util\Timing;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Tests for the \PHP_CodeSniffer\Util\Timing class.
17+
*
18+
* {@internal These tests need to run in separate processes as the Timing class uses static properties
19+
* to keep track of the start time and whether or not the runtime has been printed and these
20+
* can't be unset/reset once set.}
21+
*
22+
* @covers \PHP_CodeSniffer\Util\Timing
23+
*
24+
* @runTestsInSeparateProcesses
25+
* @preserveGlobalState disabled
26+
*/
27+
final class TimingTest extends TestCase
28+
{
29+
30+
31+
/**
32+
* Verify that getDuration() returns 0 when the timer wasn't started.
33+
*
34+
* @return void
35+
*/
36+
public function testGetDurationWithoutStartReturnsZero()
37+
{
38+
$this->assertSame(0, Timing::getDuration());
39+
40+
}//end testGetDurationWithoutStartReturnsZero()
41+
42+
43+
/**
44+
* Verify that getDuration() returns 0 when the timer wasn't started.
45+
*
46+
* @return void
47+
*/
48+
public function testGetDurationWithStartReturnsMilliseconds()
49+
{
50+
Timing::startTiming();
51+
usleep(1500);
52+
$duration = Timing::getDuration();
53+
54+
$this->assertTrue(is_float($duration));
55+
$this->assertGreaterThan(1, $duration);
56+
$this->assertLessThan(15, $duration);
57+
58+
}//end testGetDurationWithStartReturnsMilliseconds()
59+
60+
61+
/**
62+
* Verify that printRunTime() doesn't print anything if the timer wasn't started.
63+
*
64+
* @return void
65+
*/
66+
public function testTimeIsNotPrintedIfTimerWasNeverStarted()
67+
{
68+
$this->expectOutputString('');
69+
Timing::printRunTime();
70+
71+
}//end testTimeIsNotPrintedIfTimerWasNeverStarted()
72+
73+
74+
/**
75+
* Verify that printRunTime() doesn't print anything if the timer wasn't started.
76+
*
77+
* @return void
78+
*/
79+
public function testTimeIsNotPrintedIfTimerWasNeverStartedEvenWhenForced()
80+
{
81+
$this->expectOutputString('');
82+
Timing::printRunTime(true);
83+
84+
}//end testTimeIsNotPrintedIfTimerWasNeverStartedEvenWhenForced()
85+
86+
87+
/**
88+
* Verify that printRunTime() when called multiple times only prints the runtime information once.
89+
*
90+
* @return void
91+
*/
92+
public function testTimeIsPrintedOnlyOnce()
93+
{
94+
$this->expectOutputRegex('`^Time: [0-9]+ms; Memory: [0-9\.]+MB'.PHP_EOL.PHP_EOL.'$`');
95+
96+
Timing::startTiming();
97+
usleep(2000);
98+
Timing::printRunTime();
99+
Timing::printRunTime();
100+
Timing::printRunTime();
101+
102+
}//end testTimeIsPrintedOnlyOnce()
103+
104+
105+
/**
106+
* Verify that printRunTime() when called multiple times prints the runtime information multiple times if forced.
107+
*
108+
* @return void
109+
*/
110+
public function testTimeIsPrintedMultipleTimesOnlyIfForced()
111+
{
112+
$this->expectOutputRegex('`^(Time: [0-9]+ms; Memory: [0-9\.]+MB'.PHP_EOL.PHP_EOL.'){3}$`');
113+
114+
Timing::startTiming();
115+
usleep(2000);
116+
Timing::printRunTime(true);
117+
Timing::printRunTime(true);
118+
Timing::printRunTime(true);
119+
120+
}//end testTimeIsPrintedMultipleTimesOnlyIfForced()
121+
122+
123+
}//end class

0 commit comments

Comments
 (0)