From adaed6a4ea60e3775b104351ecaf21effec6736d Mon Sep 17 00:00:00 2001 From: Charlotte Dunois Date: Wed, 15 Jan 2020 20:47:37 +0100 Subject: [PATCH] Wrap into checks --- .gitignore | 3 +- bootstrap.php | 35 -------------- composer.json | 2 +- phpunit.xml.dist | 2 +- src/bootstrap.php | 37 ++++++++++++++ src/functions.php | 96 +++++++++++++++++++------------------ tests/HrtimeLoadingTest.php | 66 ++++++++++++------------- tests/HrtimeTest.php | 96 ++++++++++++++++++------------------- 8 files changed, 172 insertions(+), 165 deletions(-) delete mode 100644 bootstrap.php create mode 100644 src/bootstrap.php diff --git a/.gitignore b/.gitignore index 8925195..b03a3ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea vendor -composer.lock \ No newline at end of file +composer.lock +coverage.clover.xml \ No newline at end of file diff --git a/bootstrap.php b/bootstrap.php deleted file mode 100644 index 52dd67b..0000000 --- a/bootstrap.php +++ /dev/null @@ -1,35 +0,0 @@ - - + diff --git a/src/bootstrap.php b/src/bootstrap.php new file mode 100644 index 0000000..3c6ecef --- /dev/null +++ b/src/bootstrap.php @@ -0,0 +1,37 @@ +start(); - } - - $timer->stop(); - $timer->start(); - - $nanoseconds = (int) $timer->getElapsedTime(Unit::NANOSECOND); - - if($get_as_number) { - return $nanoseconds; - } - - return internal_conversion_nanoseconds_to_seconds($nanoseconds); +if(extension_loaded('hrtime')) { + function hrtime_ext_hrtime(bool $get_as_number = false) { + /** @var StopWatch $timer */ + static $timer; + + if(!$timer) { + $timer = new StopWatch(); + $timer->start(); + } + + $timer->stop(); + $timer->start(); + + $nanoseconds = (int) $timer->getElapsedTime(Unit::NANOSECOND); + + if($get_as_number) { + return $nanoseconds; + } + + return internal_conversion_nanoseconds_to_seconds($nanoseconds); + } } function hrtime_fallback(bool $get_as_number = false) { - /** @var int $timer */ - static $timer; - - if(!$timer) { - $timer = (int) (\microtime(true) * 1e6); - } - - $now = (int) (\microtime(true) * 1e6); - $nanoseconds = (int) ($now - $timer); - - if($get_as_number) { - return $nanoseconds; - } - - return internal_conversion_nanoseconds_to_seconds($nanoseconds); + /** @var int $timer */ + static $timer; + + if(!$timer) { + $timer = (int) (\microtime(true) * 1e6); + } + + $now = (int) (\microtime(true) * 1e6); + $nanoseconds = (int) ($now - $timer); + + if($get_as_number) { + return $nanoseconds; + } + + return internal_conversion_nanoseconds_to_seconds($nanoseconds); } \ No newline at end of file diff --git a/tests/HrtimeLoadingTest.php b/tests/HrtimeLoadingTest.php index f64c277..f77db14 100644 --- a/tests/HrtimeLoadingTest.php +++ b/tests/HrtimeLoadingTest.php @@ -11,37 +11,37 @@ use PHPUnit\Framework\TestCase; class HrtimeLoadingTest extends TestCase { - function testLoading() { - if(\PHP_VERSION_ID >= 70300) { - $this->markTestSkipped('Test unnecessary, hrtime natively available'); - } - - \xdebug_start_function_monitor(array( - 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv', - 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime', - 'Obsidian\\Polyfill\\Hrtime\\hrtime_fallback' - )); - - \hrtime(); - - [ [ 'function' => $function ] ] = \xdebug_get_monitored_functions(); - \xdebug_stop_function_monitor(); - - switch(\getenv('EXT_INSTALL', true)) { - case 'all': - case 'uv': - $fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv'; - break; - case 'hrtime': - $fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime'; - break; - case 'none': - $fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_fallback'; - break; - default: - throw new \RuntimeException('Unknown "EXT_INSTALL" env var value'); - } - - $this->assertSame($fun, $function); - } + function testLoading() { + if(\PHP_VERSION_ID >= 70300) { + $this->markTestSkipped('Test unnecessary, hrtime natively available'); + } + + \xdebug_start_function_monitor(array( + 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv', + 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime', + 'Obsidian\\Polyfill\\Hrtime\\hrtime_fallback' + )); + + \hrtime(); + + [ [ 'function' => $function ] ] = \xdebug_get_monitored_functions(); + \xdebug_stop_function_monitor(); + + switch(\getenv('EXT_INSTALL', true)) { + case 'all': + case 'uv': + $fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv'; + break; + case 'hrtime': + $fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime'; + break; + case 'none': + $fun = 'Obsidian\\Polyfill\\Hrtime\\hrtime_fallback'; + break; + default: + throw new \RuntimeException('Unknown "EXT_INSTALL" env var value'); + } + + $this->assertSame($fun, $function); + } } diff --git a/tests/HrtimeTest.php b/tests/HrtimeTest.php index 45d5a70..d38dd23 100644 --- a/tests/HrtimeTest.php +++ b/tests/HrtimeTest.php @@ -15,52 +15,52 @@ * @requires extension uv */ class HrtimeTest extends TestCase { - function providerTestFunctions() { - return array( - array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv', false), - array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime', false), - array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_fallback', true) - ); - } - - /** - * @dataProvider providerTestFunctions - * @param callable $function - * @param bool $sleep - */ - function testHrtimeAsNumber(callable $function, bool $sleep) { - $number = $function(true); - $this->assertIsInt($number); - - if($sleep) { - \sleep(1); - } - - $number2 = $function(true); - $this->assertIsInt($number2); - - $this->assertGreaterThan($number, $number2); - } - - /** - * @dataProvider providerTestFunctions - * @param callable $function - * @param bool $sleep - */ - function testHrtimeAsArray(callable $function, bool $sleep) { - $arr = $function(false); - $this->assertIsArray($arr); - $this->assertCount(2, $arr); - - if($sleep) { - \sleep(1); - } - - $arr2 = $function(false); - $this->assertIsArray($arr2); - $this->assertCount(2, $arr2); - - $this->assertSame($arr[0], $arr2[0]); - $this->assertGreaterThan($arr[1], $arr2[1]); - } + function providerTestFunctions() { + return array( + array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_uv', false), + array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_ext_hrtime', false), + array('\\Obsidian\\Polyfill\\Hrtime\\hrtime_fallback', true) + ); + } + + /** + * @dataProvider providerTestFunctions + * @param callable $function + * @param bool $sleep + */ + function testHrtimeAsNumber(callable $function, bool $sleep) { + $number = $function(true); + $this->assertIsInt($number); + + if($sleep) { + \sleep(1); + } + + $number2 = $function(true); + $this->assertIsInt($number2); + + $this->assertGreaterThan($number, $number2); + } + + /** + * @dataProvider providerTestFunctions + * @param callable $function + * @param bool $sleep + */ + function testHrtimeAsArray(callable $function, bool $sleep) { + $arr = $function(false); + $this->assertIsArray($arr); + $this->assertCount(2, $arr); + + if($sleep) { + \sleep(1); + } + + $arr2 = $function(false); + $this->assertIsArray($arr2); + $this->assertCount(2, $arr2); + + $this->assertSame($arr[0], $arr2[0]); + $this->assertGreaterThan($arr[1], $arr2[1]); + } }