|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools; |
|
4
|
|
|
|
|
5
|
|
|
use function AlecRabbit\typeOf; |
|
6
|
|
|
use const AlecRabbit\Helpers\Constants\INT_SIZE_64BIT; |
|
7
|
|
|
|
|
8
|
|
|
class HRTimer extends AbstractTimer |
|
9
|
|
|
{ |
|
10
|
|
|
public const VALUE_COEFFICIENT = HRTIMER_VALUE_COEFFICIENT; |
|
11
|
|
|
|
|
12
|
|
|
/** @var bool */ |
|
13
|
|
|
public static $ignoreVersionRestrictions = false; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @return int |
|
17
|
|
|
*/ |
|
18
|
16 |
|
public function current(): int |
|
19
|
|
|
{ |
|
20
|
|
|
return |
|
21
|
16 |
|
(int)hrtime(true); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
20 |
|
protected function checkEnvironment(): void |
|
25
|
|
|
{ |
|
26
|
|
|
// @codeCoverageIgnoreStart |
|
27
|
|
|
if (PHP_VERSION_ID < 70300 && false === static::$ignoreVersionRestrictions) { |
|
28
|
|
|
// `HRTimer::class` uses `hrtime()` function of PHP ^7.3. |
|
29
|
|
|
// There is almost no sense in using polyfill function. |
|
30
|
|
|
// If you're REALLY need to use HRTimer set `$ignoreVersionRestrictions` to true. |
|
31
|
|
|
// Otherwise use `Timer::class` instance instead. |
|
32
|
|
|
throw new \RuntimeException('[' . static::class . '] Your php version is below 7.3.0.'); |
|
33
|
|
|
} |
|
34
|
|
|
if (PHP_INT_SIZE < INT_SIZE_64BIT) { |
|
35
|
|
|
// `HRTimer::class` is designed and tested in 64bit environment |
|
36
|
|
|
// So it can be used in 64bit environments only |
|
37
|
|
|
// Maybe with some minor modification it can run on 32bit installations too |
|
38
|
|
|
throw new \RuntimeException(' You\'re using 32bit php installation.'); |
|
39
|
|
|
} |
|
40
|
|
|
// @codeCoverageIgnoreEnd |
|
41
|
20 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param int $start |
|
45
|
|
|
* @param int $stop |
|
46
|
|
|
*/ |
|
47
|
5 |
|
protected function assertStartAndStop($start, $stop): void |
|
48
|
|
|
{ |
|
49
|
5 |
|
$this->assertStart($start); |
|
50
|
4 |
|
$this->assertStop($stop); |
|
51
|
3 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $start |
|
55
|
|
|
*/ |
|
56
|
5 |
|
protected function assertStart($start): void |
|
57
|
|
|
{ |
|
58
|
5 |
|
if (!\is_int($start)) { |
|
59
|
1 |
|
throw new \RuntimeException('Start value is NOT ok. [' . typeOf($start) . ']'); |
|
60
|
|
|
} |
|
61
|
4 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param $stop |
|
65
|
|
|
*/ |
|
66
|
4 |
|
protected function assertStop($stop): void |
|
67
|
|
|
{ |
|
68
|
4 |
|
if (!is_int($stop)) { |
|
69
|
1 |
|
throw new \RuntimeException('Stop value is NOT ok. [' . typeOf($stop) . ']'); |
|
70
|
|
|
} |
|
71
|
3 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|