-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added high resolution time to pipeline report execution time (#1410)
- Loading branch information
1 parent
41154dd
commit 3731b3f
Showing
6 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/core/etl/src/Flow/ETL/Dataset/Statistics/HighResolutionTime.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Dataset\Statistics; | ||
|
||
final readonly class HighResolutionTime implements \Stringable | ||
{ | ||
/** | ||
* @param int $seconds | ||
* @param int $nanoseconds | ||
*/ | ||
public function __construct(public int $seconds, public int $nanoseconds) | ||
{ | ||
} | ||
|
||
public static function now() : self | ||
{ | ||
$timeParts = \hrtime(as_number: false); | ||
|
||
return new self($timeParts[0], $timeParts[1]); | ||
} | ||
|
||
public function __toString() : string | ||
{ | ||
return $this->toString(); | ||
} | ||
|
||
public function diff(self $other) : self | ||
{ | ||
$diffSeconds = $other->seconds - $this->seconds; | ||
$diffNanoseconds = $other->nanoseconds - $this->nanoseconds; | ||
|
||
// Adjust for negative nanoseconds | ||
if ($diffNanoseconds < 0) { | ||
$diffNanoseconds += 1_000_000_000; | ||
$diffSeconds--; | ||
} | ||
|
||
return new self($diffSeconds, $diffNanoseconds); | ||
} | ||
|
||
/** | ||
* @return array<int> | ||
*/ | ||
public function toArray() : array | ||
{ | ||
return [$this->seconds, $this->nanoseconds]; | ||
} | ||
|
||
public function toSeconds() : float | ||
{ | ||
return $this->seconds + $this->nanoseconds / 1_000_000_000; | ||
} | ||
|
||
public function toString() : string | ||
{ | ||
$formatted = number_format($this->toSeconds(), 9, '.', ''); | ||
$formatted = rtrim($formatted, '0'); | ||
$formatted = rtrim($formatted, '.'); | ||
|
||
return $formatted . 's'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/core/etl/tests/Flow/ETL/Tests/Unit/Dataset/Statistics/HighResolutionTimeTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Unit\Dataset\Statistics; | ||
|
||
use Flow\ETL\Dataset\Statistics\HighResolutionTime; | ||
use Flow\ETL\Tests\FlowTestCase; | ||
|
||
final class HighResolutionTimeTest extends FlowTestCase | ||
{ | ||
public function test_constructor_initializes_correctly() : void | ||
{ | ||
$time = new HighResolutionTime(123, 456789000); | ||
|
||
self::assertSame([123, 456789000], $time->toArray()); | ||
self::assertEquals(123.456789, $time->toSeconds()); | ||
} | ||
|
||
public function test_diff_when_nanoseconds_need_adjustment() : void | ||
{ | ||
$t1 = new HighResolutionTime(10, 500000000); // 10.5s | ||
$t2 = new HighResolutionTime(10, 300000000); // 10.3s | ||
|
||
$diff = $t1->diff($t2); | ||
|
||
// 10.3s - 10.5s = -0.2s => [-1, 800000000] | ||
self::assertSame([-1, 800000000], $diff->toArray()); | ||
self::assertEqualsWithDelta(-0.2, $diff->toSeconds(), 1e-9); | ||
} | ||
|
||
public function test_diff_with_no_difference() : void | ||
{ | ||
$t1 = new HighResolutionTime(10, 500000000); | ||
$diff = $t1->diff($t1); | ||
|
||
self::assertSame([0, 0], $diff->toArray()); | ||
self::assertEquals(0.0, $diff->toSeconds()); | ||
} | ||
|
||
public function test_diff_with_positive_difference() : void | ||
{ | ||
$t1 = new HighResolutionTime(10, 500000000); // 10.5s | ||
$t2 = new HighResolutionTime(12, 100000000); // 12.1s | ||
|
||
$diff = $t1->diff($t2); | ||
|
||
// We expect 1.6s difference | ||
self::assertSame([1, 600000000], $diff->toArray()); | ||
self::assertEqualsWithDelta(1.6, $diff->toSeconds(), 1e-9); | ||
} | ||
|
||
public function test_now_creates_current_time_instance() : void | ||
{ | ||
$time = HighResolutionTime::now(); | ||
self::assertInstanceOf(HighResolutionTime::class, $time); | ||
} | ||
|
||
public function test_to_formatted_string_removes_trailing_zeroes() : void | ||
{ | ||
// 5.250000000 => should display as "5.25" | ||
$time = new HighResolutionTime(5, 250000000); | ||
self::assertSame('5.25s', $time->toString()); | ||
|
||
// 0.001000000 => should display as "0.001" | ||
$time = new HighResolutionTime(0, 1000000); // 1,000,000ns = 0.001s | ||
self::assertSame('0.001s', $time->toString()); | ||
|
||
// 3.000000000 => should display as "3" | ||
$time = new HighResolutionTime(3, 0); | ||
self::assertSame('3s', $time->toString()); | ||
} | ||
} |