Skip to content

Commit 55156d2

Browse files
committed
Timing: add new getDurationSince() method
This method doesn't use the "system" run start time, but uses an arbitrary starttime as provided via a parameter. This allows for removing some duplicate code in various places. Includes making the class `final` and explicitly marking this class as an internal class to allow for further changes in the future.
1 parent 492b884 commit 55156d2

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Util/Timing.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22
/**
33
* Timing functions for the run.
44
*
5+
* ---------------------------------------------------------------------------------------------
6+
* This class is intended for internal use only and is not part of the public API.
7+
* This also means that it has no promise of backward compatibility. Use at your own risk.
8+
* ---------------------------------------------------------------------------------------------
9+
*
10+
* @internal
11+
*
512
* @author Greg Sherwood <[email protected]>
13+
* @author Juliette Reinders Folmer <[email protected]>
614
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
15+
* @copyright 2025 PHPCSStandards and contributors
716
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
817
*/
918

1019
namespace PHP_CodeSniffer\Util;
1120

1221
use PHP_CodeSniffer\Util\Writers\StatusWriter;
1322

14-
class Timing
23+
final class Timing
1524
{
1625

1726
/**
@@ -29,7 +38,7 @@ class Timing
2938
private const SECOND_IN_MS = 1000;
3039

3140
/**
32-
* The start time of the run in microseconds.
41+
* The start time of the run in seconds.
3342
*
3443
* @var float
3544
*/
@@ -73,6 +82,20 @@ public static function getDuration()
7382
}//end getDuration()
7483

7584

85+
/**
86+
* Get the duration since a given start time up to "now".
87+
*
88+
* @param float $startTime Start time in microseconds.
89+
*
90+
* @return float Duration in milliseconds.
91+
*/
92+
public static function getDurationSince($startTime)
93+
{
94+
return ((microtime(true) - $startTime) * 1000);
95+
96+
}//end getDurationSince()
97+
98+
7699
/**
77100
* Convert a duration in milliseconds to a human readable duration string.
78101
*

tests/Core/Util/Timing/TimingTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ public function testGetDurationWithStartReturnsMilliseconds()
6060
}//end testGetDurationWithStartReturnsMilliseconds()
6161

6262

63+
/**
64+
* Verify that getDurationSince() returns the time in milliseconds.
65+
*
66+
* @return void
67+
*/
68+
public function testGetDurationSinceReturnsMilliseconds()
69+
{
70+
$startTime = microtime(true);
71+
usleep(1500);
72+
$duration = Timing::getDurationSince($startTime);
73+
74+
$this->assertIsFloat($duration);
75+
$this->assertGreaterThan(1, $duration);
76+
$this->assertLessThan(15, $duration);
77+
78+
}//end testGetDurationSinceReturnsMilliseconds()
79+
80+
6381
/**
6482
* Verify that printRunTime() doesn't print anything if the timer wasn't started.
6583
*

0 commit comments

Comments
 (0)