From 25a7d35bf27e40b6c364102f1e7648406d4b1b87 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 19 Jun 2017 11:42:01 +0200 Subject: [PATCH 01/12] Add support for microseconds in Stopwatch --- .../Component/Stopwatch/StopwatchPeriod.php | 14 ++--- .../Stopwatch/Tests/StopwatchPeriodTest.php | 55 +++++++++++++++++++ 2 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php diff --git a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php index 9876f179aadb6..6822210060cee 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php +++ b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php @@ -25,20 +25,20 @@ class StopwatchPeriod /** * Constructor. * - * @param int $start The relative time of the start of the period (in milliseconds) - * @param int $end The relative time of the end of the period (in milliseconds) + * @param float $start The relative time of the start of the period (in milliseconds) + * @param float $end The relative time of the end of the period (in milliseconds) */ public function __construct($start, $end) { - $this->start = (int) $start; - $this->end = (int) $end; + $this->start = $start; + $this->end = $end; $this->memory = memory_get_usage(true); } /** * Gets the relative time of the start of the period. * - * @return int The time (in milliseconds) + * @return float The time (in milliseconds) */ public function getStartTime() { @@ -48,7 +48,7 @@ public function getStartTime() /** * Gets the relative time of the end of the period. * - * @return int The time (in milliseconds) + * @return float The time (in milliseconds) */ public function getEndTime() { @@ -58,7 +58,7 @@ public function getEndTime() /** * Gets the time spent in this period. * - * @return int The period duration (in milliseconds) + * @return float The period duration (in milliseconds) */ public function getDuration() { diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php new file mode 100644 index 0000000000000..683f0a6084d5b --- /dev/null +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Stopwatch\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Stopwatch\StopwatchPeriod; + +class StopwatchPeriodTest extends TestCase +{ + /** + * @dataProvider provideTimeValues + */ + public function testGetStartTime($start, $end) + { + $period = new StopwatchPeriod($start, $end); + $this->assertSame($start, $period->getStartTime()); + } + + /** + * @dataProvider provideTimeValues + */ + public function testGetEndTime($start, $end) + { + $period = new StopwatchPeriod($start, $end); + $this->assertSame($end, $period->getEndTime()); + } + + /** + * @dataProvider provideTimeValues + */ + public function testGetDuration($start, $end, $duration) + { + $period = new StopwatchPeriod($start, $end); + $this->assertSame($duration, $period->getDuration()); + } + + public function provideTimeValues() + { + yield [0, 0, 0]; + yield [0.0, 0.0, 0.0]; + yield [0.0, 2.7182, 2.7182]; + yield [3, 7, 4]; + yield [3, 3.14, 0.14]; + yield [3.10, 3.14, 0.04]; + } +} From eaba9d3e7dc756cb429876de4fe3ff630c8319f7 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 19 Jun 2017 11:47:19 +0200 Subject: [PATCH 02/12] Fixed code syntax --- .../Stopwatch/Tests/StopwatchPeriodTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php index 683f0a6084d5b..02d5a192c7182 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php @@ -45,11 +45,11 @@ public function testGetDuration($start, $end, $duration) public function provideTimeValues() { - yield [0, 0, 0]; - yield [0.0, 0.0, 0.0]; - yield [0.0, 2.7182, 2.7182]; - yield [3, 7, 4]; - yield [3, 3.14, 0.14]; - yield [3.10, 3.14, 0.04]; + yield array(0, 0, 0); + yield array(0.0, 0.0, 0.0); + yield array(0.0, 2.7182, 2.7182); + yield array(3, 7, 4); + yield array(3, 3.14, 0.14); + yield array(3.10, 3.14, 0.04); } } From e002ff0d99e10002ff85508be2c78e2cd7597a2c Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 19 Jun 2017 17:47:09 +0200 Subject: [PATCH 03/12] Added a new $useMicroPrecision param --- .../Component/Stopwatch/StopwatchPeriod.php | 22 ++++++---- .../Stopwatch/Tests/StopwatchPeriodTest.php | 42 ++++++++++++------- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php index 6822210060cee..1d78122f56b5a 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php +++ b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php @@ -25,20 +25,26 @@ class StopwatchPeriod /** * Constructor. * - * @param float $start The relative time of the start of the period (in milliseconds) - * @param float $end The relative time of the end of the period (in milliseconds) + * @param int|float $start The relative time of the start of the period (in milliseconds) + * @param int|float $end The relative time of the end of the period (in milliseconds) */ - public function __construct($start, $end) + public function __construct($start, $end /*, $useMicroPrecision = false*/) { - $this->start = $start; - $this->end = $end; + if (func_num_args() > 2 && true === func_get_arg(2)) { + $this->start = $start; + $this->end = $end; + } else { + $this->start = (int) $start; + $this->end = (int) $end; + } + $this->memory = memory_get_usage(true); } /** * Gets the relative time of the start of the period. * - * @return float The time (in milliseconds) + * @return int|float The time (in milliseconds) */ public function getStartTime() { @@ -48,7 +54,7 @@ public function getStartTime() /** * Gets the relative time of the end of the period. * - * @return float The time (in milliseconds) + * @return int|float The time (in milliseconds) */ public function getEndTime() { @@ -58,7 +64,7 @@ public function getEndTime() /** * Gets the time spent in this period. * - * @return float The period duration (in milliseconds) + * @return int|float The period duration (in milliseconds) */ public function getDuration() { diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php index 02d5a192c7182..596e5a2302344 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php @@ -19,37 +19,49 @@ class StopwatchPeriodTest extends TestCase /** * @dataProvider provideTimeValues */ - public function testGetStartTime($start, $end) + public function testGetStartTime($start, $useMorePrecision, $expected) { - $period = new StopwatchPeriod($start, $end); - $this->assertSame($start, $period->getStartTime()); + $period = new StopwatchPeriod($start, $start, $useMorePrecision); + $this->assertSame($expected, $period->getStartTime()); } /** * @dataProvider provideTimeValues */ - public function testGetEndTime($start, $end) + public function testGetEndTime($end, $useMorePrecision, $expected) { - $period = new StopwatchPeriod($start, $end); - $this->assertSame($end, $period->getEndTime()); + $period = new StopwatchPeriod($end, $end, $useMorePrecision); + $this->assertSame($expected, $period->getEndTime()); } /** - * @dataProvider provideTimeValues + * @dataProvider provideDurationValues */ - public function testGetDuration($start, $end, $duration) + public function testGetDuration($start, $end, $useMorePrecision, $duration) { - $period = new StopwatchPeriod($start, $end); + $period = new StopwatchPeriod($start, $end, $useMorePrecision); $this->assertSame($duration, $period->getDuration()); } public function provideTimeValues() { - yield array(0, 0, 0); - yield array(0.0, 0.0, 0.0); - yield array(0.0, 2.7182, 2.7182); - yield array(3, 7, 4); - yield array(3, 3.14, 0.14); - yield array(3.10, 3.14, 0.04); + yield array(0, false, 0); + yield array(0, true, 0); + yield array(0.0, false, 0); + yield array(0.0, true, 0.0); + yield array(2.71, false, 2); + yield array(2.71, true, 2.71); + } + + public function provideDurationValues() + { + yield array(0, 0, false, 0); + yield array(0, 0, true, 0); + yield array(0.0, 0.0, false, 0); + yield array(0.0, 0.0, true, 0.0); + yield array(2, 3.14, false, 1); + yield array(2, 3.14, true, 1.14); + yield array(2.71, 3.14, false, 1); + yield array(2.71, 3.14, true, 0.43); } } From a12fb579b372954e2fef7d43fa65681417b7b128 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 19 Jun 2017 17:47:25 +0200 Subject: [PATCH 04/12] Added the CHANGELOG file for the component --- src/Symfony/Component/Stopwatch/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Symfony/Component/Stopwatch/CHANGELOG.md b/src/Symfony/Component/Stopwatch/CHANGELOG.md index 2a03545ed1b82..5483fe2a40b0b 100644 --- a/src/Symfony/Component/Stopwatch/CHANGELOG.md +++ b/src/Symfony/Component/Stopwatch/CHANGELOG.md @@ -5,3 +5,5 @@ CHANGELOG ----- * added the `Stopwatch::reset()` method + * allowed to measure sub-millisecond times by introducing a third argument to + the constructor of `StopwatchPeriod` From 20f9e55eb6831bb7edfb833187f287a61b7cddeb Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 19 Jun 2017 20:51:06 +0200 Subject: [PATCH 05/12] Simplified the implementation --- .../Component/Stopwatch/StopwatchPeriod.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php index 1d78122f56b5a..56bd7aca05702 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php +++ b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php @@ -25,19 +25,14 @@ class StopwatchPeriod /** * Constructor. * - * @param int|float $start The relative time of the start of the period (in milliseconds) - * @param int|float $end The relative time of the end of the period (in milliseconds) + * @param int|float $start The relative time of the start of the period (in milliseconds) + * @param int|float $end The relative time of the end of the period (in milliseconds) + * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision */ - public function __construct($start, $end /*, $useMicroPrecision = false*/) + public function __construct($start, $end, /* bool */ $morePrecision = false) { - if (func_num_args() > 2 && true === func_get_arg(2)) { - $this->start = $start; - $this->end = $end; - } else { - $this->start = (int) $start; - $this->end = (int) $end; - } - + $this->start = $morePrecision ? $start : (int) $start; + $this->end = $morePrecision ? $end : (int) $end; $this->memory = memory_get_usage(true); } From ce2b842427e643ba60f793047beedf285f2cb206 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 19 Jun 2017 21:31:16 +0200 Subject: [PATCH 06/12] Use more precision by default --- src/Symfony/Component/Stopwatch/StopwatchEvent.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Stopwatch/StopwatchEvent.php b/src/Symfony/Component/Stopwatch/StopwatchEvent.php index 16a30db2aa50e..05e37e2b250f5 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchEvent.php +++ b/src/Symfony/Component/Stopwatch/StopwatchEvent.php @@ -97,7 +97,7 @@ public function stop() throw new \LogicException('stop() called but start() has not been called before.'); } - $this->periods[] = new StopwatchPeriod(array_pop($this->started), $this->getNow()); + $this->periods[] = new StopwatchPeriod(array_pop($this->started), $this->getNow(), true); return $this; } @@ -177,7 +177,7 @@ public function getDuration() for ($i = 0; $i < $left; ++$i) { $index = $stopped + $i; - $periods[] = new StopwatchPeriod($this->started[$index], $this->getNow()); + $periods[] = new StopwatchPeriod($this->started[$index], $this->getNow(), true); } $total = 0; From f734c2a37d78eb63f118986448d4e6e5d7ecb4b0 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 6 Jul 2017 09:40:22 +0200 Subject: [PATCH 07/12] Moved the argument from constructor to methods --- .../Component/Stopwatch/StopwatchPeriod.php | 32 +++++++++++++------ .../Stopwatch/Tests/StopwatchPeriodTest.php | 14 ++++---- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php index 56bd7aca05702..17596c59be087 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php +++ b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php @@ -29,10 +29,10 @@ class StopwatchPeriod * @param int|float $end The relative time of the end of the period (in milliseconds) * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision */ - public function __construct($start, $end, /* bool */ $morePrecision = false) + public function __construct($start, $end) { - $this->start = $morePrecision ? $start : (int) $start; - $this->end = $morePrecision ? $end : (int) $end; + $this->start = $start; + $this->end = $end; $this->memory = memory_get_usage(true); } @@ -41,9 +41,13 @@ public function __construct($start, $end, /* bool */ $morePrecision = false) * * @return int|float The time (in milliseconds) */ - public function getStartTime() + public function getStartTime(/*bool $morePrecision = false*/) { - return $this->start; + if (func_num_args() > 0 && true === func_get_arg(0)) { + return $this->start; + } + + return (int) $this->start; } /** @@ -51,9 +55,13 @@ public function getStartTime() * * @return int|float The time (in milliseconds) */ - public function getEndTime() + public function getEndTime(/*bool $morePrecision = false*/) { - return $this->end; + if (func_num_args() > 0 && true === func_get_arg(0)) { + return $this->end; + } + + return (int) $this->end; } /** @@ -61,9 +69,15 @@ public function getEndTime() * * @return int|float The period duration (in milliseconds) */ - public function getDuration() + public function getDuration(/*bool $morePrecision = false*/) { - return $this->end - $this->start; + $duration = $this->end - $this->start; + + if (func_num_args() > 0 && true === func_get_arg(0)) { + return $duration; + } + + return (int) $duration; } /** diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php index 596e5a2302344..58af660fb1f8b 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php @@ -21,8 +21,8 @@ class StopwatchPeriodTest extends TestCase */ public function testGetStartTime($start, $useMorePrecision, $expected) { - $period = new StopwatchPeriod($start, $start, $useMorePrecision); - $this->assertSame($expected, $period->getStartTime()); + $period = new StopwatchPeriod($start, $start); + $this->assertSame($expected, $period->getStartTime($useMorePrecision)); } /** @@ -30,8 +30,8 @@ public function testGetStartTime($start, $useMorePrecision, $expected) */ public function testGetEndTime($end, $useMorePrecision, $expected) { - $period = new StopwatchPeriod($end, $end, $useMorePrecision); - $this->assertSame($expected, $period->getEndTime()); + $period = new StopwatchPeriod($end, $end); + $this->assertSame($expected, $period->getEndTime($useMorePrecision)); } /** @@ -39,8 +39,8 @@ public function testGetEndTime($end, $useMorePrecision, $expected) */ public function testGetDuration($start, $end, $useMorePrecision, $duration) { - $period = new StopwatchPeriod($start, $end, $useMorePrecision); - $this->assertSame($duration, $period->getDuration()); + $period = new StopwatchPeriod($start, $end); + $this->assertSame($duration, $period->getDuration($useMorePrecision)); } public function provideTimeValues() @@ -61,7 +61,7 @@ public function provideDurationValues() yield array(0.0, 0.0, true, 0.0); yield array(2, 3.14, false, 1); yield array(2, 3.14, true, 1.14); - yield array(2.71, 3.14, false, 1); + yield array(2.71, 3.14, false, 0); yield array(2.71, 3.14, true, 0.43); } } From e0ca56e01c1f11f65be65556e9679d268b3a5d57 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 6 Jul 2017 10:14:05 +0200 Subject: [PATCH 08/12] Proposed a new implementation --- src/Symfony/Component/Stopwatch/Section.php | 15 ++++++--- src/Symfony/Component/Stopwatch/Stopwatch.php | 13 ++++++-- .../Component/Stopwatch/StopwatchEvent.php | 17 +++++++--- .../Component/Stopwatch/StopwatchPeriod.php | 32 ++++++------------- .../Stopwatch/Tests/StopwatchPeriodTest.php | 14 ++++---- 5 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/Symfony/Component/Stopwatch/Section.php b/src/Symfony/Component/Stopwatch/Section.php index 2337e03140c7f..e730b5edd2af3 100644 --- a/src/Symfony/Component/Stopwatch/Section.php +++ b/src/Symfony/Component/Stopwatch/Section.php @@ -28,6 +28,11 @@ class Section */ private $origin; + /** + * @var bool + */ + private $morePrecision; + /** * @var string */ @@ -41,11 +46,13 @@ class Section /** * Constructor. * - * @param float|null $origin Set the origin of the events in this section, use null to set their origin to their start time + * @param float|null $origin Set the origin of the events in this section, use null to set their origin to their start time + * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision */ - public function __construct($origin = null) + public function __construct($origin = null, /* bool */ $morePrecision = false) { $this->origin = is_numeric($origin) ? $origin : null; + $this->morePrecision = $morePrecision; } /** @@ -74,7 +81,7 @@ public function get($id) public function open($id) { if (null === $session = $this->get($id)) { - $session = $this->children[] = new self(microtime(true) * 1000); + $session = $this->children[] = new self(microtime(true) * 1000, $this->morePrecision); } return $session; @@ -113,7 +120,7 @@ public function setId($id) public function startEvent($name, $category) { if (!isset($this->events[$name])) { - $this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category); + $this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category, $this->morePrecision); } return $this->events[$name]->start(); diff --git a/src/Symfony/Component/Stopwatch/Stopwatch.php b/src/Symfony/Component/Stopwatch/Stopwatch.php index 562f220f673b1..2a905004d265e 100644 --- a/src/Symfony/Component/Stopwatch/Stopwatch.php +++ b/src/Symfony/Component/Stopwatch/Stopwatch.php @@ -18,6 +18,11 @@ */ class Stopwatch { + /** + * @var bool + */ + private $morePrecision; + /** * @var Section[] */ @@ -28,9 +33,13 @@ class Stopwatch */ private $activeSections; - public function __construct() + /** + * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision + */ + public function __construct(/* bool */ $morePrecision = false) { $this->reset(); + $this->morePrecision = $morePrecision; } /** @@ -162,6 +171,6 @@ public function getSectionEvents($id) */ public function reset() { - $this->sections = $this->activeSections = array('__root__' => new Section('__root__')); + $this->sections = $this->activeSections = array('__root__' => new Section('__root__', $this->morePrecision)); } } diff --git a/src/Symfony/Component/Stopwatch/StopwatchEvent.php b/src/Symfony/Component/Stopwatch/StopwatchEvent.php index 05e37e2b250f5..c6535f95dc36b 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchEvent.php +++ b/src/Symfony/Component/Stopwatch/StopwatchEvent.php @@ -33,6 +33,11 @@ class StopwatchEvent */ private $category; + /** + * @var bool + */ + private $morePrecision; + /** * @var float[] */ @@ -41,15 +46,17 @@ class StopwatchEvent /** * Constructor. * - * @param float $origin The origin time in milliseconds - * @param string|null $category The event category or null to use the default + * @param float $origin The origin time in milliseconds + * @param string|null $category The event category or null to use the default + * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision * * @throws \InvalidArgumentException When the raw time is not valid */ - public function __construct($origin, $category = null) + public function __construct($origin, $category = null, /* bool */ $morePrecision = false) { $this->origin = $this->formatTime($origin); $this->category = is_string($category) ? $category : 'default'; + $this->morePrecision = $morePrecision; } /** @@ -97,7 +104,7 @@ public function stop() throw new \LogicException('stop() called but start() has not been called before.'); } - $this->periods[] = new StopwatchPeriod(array_pop($this->started), $this->getNow(), true); + $this->periods[] = new StopwatchPeriod(array_pop($this->started), $this->getNow(), $this->morePrecision); return $this; } @@ -177,7 +184,7 @@ public function getDuration() for ($i = 0; $i < $left; ++$i) { $index = $stopped + $i; - $periods[] = new StopwatchPeriod($this->started[$index], $this->getNow(), true); + $periods[] = new StopwatchPeriod($this->started[$index], $this->getNow(), $this->morePrecision); } $total = 0; diff --git a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php index 17596c59be087..56bd7aca05702 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php +++ b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php @@ -29,10 +29,10 @@ class StopwatchPeriod * @param int|float $end The relative time of the end of the period (in milliseconds) * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision */ - public function __construct($start, $end) + public function __construct($start, $end, /* bool */ $morePrecision = false) { - $this->start = $start; - $this->end = $end; + $this->start = $morePrecision ? $start : (int) $start; + $this->end = $morePrecision ? $end : (int) $end; $this->memory = memory_get_usage(true); } @@ -41,13 +41,9 @@ public function __construct($start, $end) * * @return int|float The time (in milliseconds) */ - public function getStartTime(/*bool $morePrecision = false*/) + public function getStartTime() { - if (func_num_args() > 0 && true === func_get_arg(0)) { - return $this->start; - } - - return (int) $this->start; + return $this->start; } /** @@ -55,13 +51,9 @@ public function getStartTime(/*bool $morePrecision = false*/) * * @return int|float The time (in milliseconds) */ - public function getEndTime(/*bool $morePrecision = false*/) + public function getEndTime() { - if (func_num_args() > 0 && true === func_get_arg(0)) { - return $this->end; - } - - return (int) $this->end; + return $this->end; } /** @@ -69,15 +61,9 @@ public function getEndTime(/*bool $morePrecision = false*/) * * @return int|float The period duration (in milliseconds) */ - public function getDuration(/*bool $morePrecision = false*/) + public function getDuration() { - $duration = $this->end - $this->start; - - if (func_num_args() > 0 && true === func_get_arg(0)) { - return $duration; - } - - return (int) $duration; + return $this->end - $this->start; } /** diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php index 58af660fb1f8b..596e5a2302344 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php @@ -21,8 +21,8 @@ class StopwatchPeriodTest extends TestCase */ public function testGetStartTime($start, $useMorePrecision, $expected) { - $period = new StopwatchPeriod($start, $start); - $this->assertSame($expected, $period->getStartTime($useMorePrecision)); + $period = new StopwatchPeriod($start, $start, $useMorePrecision); + $this->assertSame($expected, $period->getStartTime()); } /** @@ -30,8 +30,8 @@ public function testGetStartTime($start, $useMorePrecision, $expected) */ public function testGetEndTime($end, $useMorePrecision, $expected) { - $period = new StopwatchPeriod($end, $end); - $this->assertSame($expected, $period->getEndTime($useMorePrecision)); + $period = new StopwatchPeriod($end, $end, $useMorePrecision); + $this->assertSame($expected, $period->getEndTime()); } /** @@ -39,8 +39,8 @@ public function testGetEndTime($end, $useMorePrecision, $expected) */ public function testGetDuration($start, $end, $useMorePrecision, $duration) { - $period = new StopwatchPeriod($start, $end); - $this->assertSame($duration, $period->getDuration($useMorePrecision)); + $period = new StopwatchPeriod($start, $end, $useMorePrecision); + $this->assertSame($duration, $period->getDuration()); } public function provideTimeValues() @@ -61,7 +61,7 @@ public function provideDurationValues() yield array(0.0, 0.0, true, 0.0); yield array(2, 3.14, false, 1); yield array(2, 3.14, true, 1.14); - yield array(2.71, 3.14, false, 0); + yield array(2.71, 3.14, false, 1); yield array(2.71, 3.14, true, 0.43); } } From 9c299c2449073b1ffd2418c29eaf525745e8916c Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 6 Jul 2017 10:23:12 +0200 Subject: [PATCH 09/12] Misc fixes --- src/Symfony/Component/Stopwatch/CHANGELOG.md | 4 ++-- src/Symfony/Component/Stopwatch/Section.php | 2 +- src/Symfony/Component/Stopwatch/Stopwatch.php | 2 +- src/Symfony/Component/Stopwatch/StopwatchEvent.php | 2 +- src/Symfony/Component/Stopwatch/StopwatchPeriod.php | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Stopwatch/CHANGELOG.md b/src/Symfony/Component/Stopwatch/CHANGELOG.md index 5483fe2a40b0b..36d0c25f1a9f7 100644 --- a/src/Symfony/Component/Stopwatch/CHANGELOG.md +++ b/src/Symfony/Component/Stopwatch/CHANGELOG.md @@ -5,5 +5,5 @@ CHANGELOG ----- * added the `Stopwatch::reset()` method - * allowed to measure sub-millisecond times by introducing a third argument to - the constructor of `StopwatchPeriod` + * allowed to measure sub-millisecond times by introducing an argument to the + constructor of `Stopwatch` diff --git a/src/Symfony/Component/Stopwatch/Section.php b/src/Symfony/Component/Stopwatch/Section.php index e730b5edd2af3..d4ff9928f83ac 100644 --- a/src/Symfony/Component/Stopwatch/Section.php +++ b/src/Symfony/Component/Stopwatch/Section.php @@ -49,7 +49,7 @@ class Section * @param float|null $origin Set the origin of the events in this section, use null to set their origin to their start time * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision */ - public function __construct($origin = null, /* bool */ $morePrecision = false) + public function __construct($origin = null, $morePrecision = false) { $this->origin = is_numeric($origin) ? $origin : null; $this->morePrecision = $morePrecision; diff --git a/src/Symfony/Component/Stopwatch/Stopwatch.php b/src/Symfony/Component/Stopwatch/Stopwatch.php index 2a905004d265e..cbe08cc7c1f0f 100644 --- a/src/Symfony/Component/Stopwatch/Stopwatch.php +++ b/src/Symfony/Component/Stopwatch/Stopwatch.php @@ -36,7 +36,7 @@ class Stopwatch /** * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision */ - public function __construct(/* bool */ $morePrecision = false) + public function __construct($morePrecision = false) { $this->reset(); $this->morePrecision = $morePrecision; diff --git a/src/Symfony/Component/Stopwatch/StopwatchEvent.php b/src/Symfony/Component/Stopwatch/StopwatchEvent.php index c6535f95dc36b..0d20f49b13f3f 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchEvent.php +++ b/src/Symfony/Component/Stopwatch/StopwatchEvent.php @@ -52,7 +52,7 @@ class StopwatchEvent * * @throws \InvalidArgumentException When the raw time is not valid */ - public function __construct($origin, $category = null, /* bool */ $morePrecision = false) + public function __construct($origin, $category = null, $morePrecision = false) { $this->origin = $this->formatTime($origin); $this->category = is_string($category) ? $category : 'default'; diff --git a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php index 56bd7aca05702..c3ae266c9aecb 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php +++ b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php @@ -29,10 +29,10 @@ class StopwatchPeriod * @param int|float $end The relative time of the end of the period (in milliseconds) * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision */ - public function __construct($start, $end, /* bool */ $morePrecision = false) + public function __construct($start, $end, $morePrecision = false) { - $this->start = $morePrecision ? $start : (int) $start; - $this->end = $morePrecision ? $end : (int) $end; + $this->start = $morePrecision ? (float) $start : (int) $start; + $this->end = $morePrecision ? (float) $end : (int) $end; $this->memory = memory_get_usage(true); } From 24a50593e3ffefccdd735a465d2fee61cda8c3d2 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 6 Jul 2017 10:28:04 +0200 Subject: [PATCH 10/12] Use more precision in the Stopwatch of the FrameworkBundle --- .../FrameworkBundle/DependencyInjection/FrameworkExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index f3b77f0a12409..009b2b7bfc666 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -652,7 +652,7 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con $loader->load('debug_prod.xml'); if (class_exists(Stopwatch::class)) { - $container->register('debug.stopwatch', Stopwatch::class); + $container->register('debug.stopwatch', Stopwatch::class)->addArgument('true'); $container->setAlias(Stopwatch::class, 'debug.stopwatch'); } From 1355390549d04c18463ed9f132a2ba4121317970 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 6 Jul 2017 10:36:12 +0200 Subject: [PATCH 11/12] Use a real boolean argument --- .../FrameworkBundle/DependencyInjection/FrameworkExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 009b2b7bfc666..a3c92a026be52 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -652,7 +652,7 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con $loader->load('debug_prod.xml'); if (class_exists(Stopwatch::class)) { - $container->register('debug.stopwatch', Stopwatch::class)->addArgument('true'); + $container->register('debug.stopwatch', Stopwatch::class)->addArgument(true); $container->setAlias(Stopwatch::class, 'debug.stopwatch'); } From 906116742aac236dbd0307a8826d4e6dc1e3595f Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 6 Jul 2017 10:44:47 +0200 Subject: [PATCH 12/12] Fixed some tests --- src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php index 596e5a2302344..f2387b8285bec 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchPeriodTest.php @@ -46,7 +46,7 @@ public function testGetDuration($start, $end, $useMorePrecision, $duration) public function provideTimeValues() { yield array(0, false, 0); - yield array(0, true, 0); + yield array(0, true, 0.0); yield array(0.0, false, 0); yield array(0.0, true, 0.0); yield array(2.71, false, 2); @@ -56,7 +56,7 @@ public function provideTimeValues() public function provideDurationValues() { yield array(0, 0, false, 0); - yield array(0, 0, true, 0); + yield array(0, 0, true, 0.0); yield array(0.0, 0.0, false, 0); yield array(0.0, 0.0, true, 0.0); yield array(2, 3.14, false, 1); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy