Skip to content
This repository was archived by the owner on Jan 8, 2022. It is now read-only.

Commit

Permalink
Wrap into checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlotte Dunois committed Jan 15, 2020
1 parent 573af85 commit adaed6a
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 165 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
vendor
composer.lock
composer.lock
coverage.clover.xml
35 changes: 0 additions & 35 deletions bootstrap.php

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"autoload": {
"files": [
"bootstrap.php"
"src/bootstrap.php"
]
},
"autoload-dev": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</filter>
<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true" />
<log type="coverage-clover" target="./coverage.clover.xml"/>
<log type="coverage-clover" target="./coverage.clover.xml" />
</logging>
</phpunit>
37 changes: 37 additions & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Obsidian
* Copyright 2020 ObsidianPHP, All Rights Reserved
*
* License: https://github.com/ObsidianPHP/polyfill-hrtime/blob/master/LICENSE
*/

if(!function_exists('\\Obsidian\\Polyfill\\Hrtime\\hrtime_fallback\\hrtime_fallback')) {
require 'functions.php';
}

if(function_exists('hrtime')) {
return;
}

use function Obsidian\Polyfill\Hrtime\hrtime_ext_uv;
use function Obsidian\Polyfill\Hrtime\hrtime_ext_hrtime;
use function Obsidian\Polyfill\Hrtime\hrtime_fallback;

if(function_exists('uv_hrtime')) {
function hrtime(bool $get_as_number = false) {
return hrtime_ext_uv($get_as_number);
}
} elseif(extension_loaded('hrtime')) {
function hrtime(bool $get_as_number = false) {
return hrtime_ext_hrtime($get_as_number);
}

hrtime();
} else {
function hrtime(bool $get_as_number = false) {
return hrtime_fallback($get_as_number);
}

hrtime();
}
96 changes: 50 additions & 46 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,61 @@
use HrTime\Unit;

function internal_conversion_nanoseconds_to_seconds(int $nanoseconds): array {
$seconds = \intdiv($nanoseconds, 1e9);
$nanoseconds -= (int) ($seconds * 1e9);
return array($seconds, $nanoseconds);
$seconds = \intdiv($nanoseconds, 1e9);
$nanoseconds -= (int) ($seconds * 1e9);
return array($seconds, $nanoseconds);
}

function hrtime_ext_uv(bool $get_as_number = false) {
$nanoseconds = \uv_hrtime();

if($get_as_number) {
return $nanoseconds;
}

return internal_conversion_nanoseconds_to_seconds($nanoseconds);
if(function_exists('uv_hrtime')) {
function hrtime_ext_uv(bool $get_as_number = false) {
$nanoseconds = \uv_hrtime();

if($get_as_number) {
return $nanoseconds;
}

return internal_conversion_nanoseconds_to_seconds($nanoseconds);
}
}

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);
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);
}
66 changes: 33 additions & 33 deletions tests/HrtimeLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
96 changes: 48 additions & 48 deletions tests/HrtimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

0 comments on commit adaed6a

Please sign in to comment.