1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @codingStandardsIgnoreStart |
4
|
|
|
* |
5
|
|
|
* @author Barney Hanlon <[email protected]> |
6
|
|
|
* @copyright Barney Hanlon 2017 |
7
|
|
|
* @license https://opensource.org/licenses/MIT |
8
|
|
|
* |
9
|
|
|
* @codingStandardsIgnoreEnd |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Shrikeh\GuzzleMiddleware\TimerLogger\Timer; |
13
|
|
|
|
14
|
|
|
use DateTimeImmutable; |
15
|
|
|
use Litipk\BigNumbers\Decimal; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Stopwatch. |
19
|
|
|
*/ |
20
|
|
|
class Stopwatch implements TimerInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var float |
24
|
|
|
*/ |
25
|
|
|
private $start; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var float |
29
|
|
|
*/ |
30
|
|
|
private $end; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return self |
34
|
|
|
*/ |
35
|
|
|
public static function startStopWatch() |
36
|
|
|
{ |
37
|
|
|
$t = \microtime(true); |
38
|
|
|
|
39
|
|
|
return new self($t); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Stopwatch constructor. |
44
|
|
|
* |
45
|
|
|
* @param float|null $start The start time |
46
|
|
|
*/ |
47
|
|
|
public function __construct($start = null) |
48
|
|
|
{ |
49
|
|
|
$this->start = $start; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function start() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$t = \microtime(true); |
58
|
|
|
if (!$this->start) { |
59
|
|
|
$this->start = $t; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->dateTime($this->start); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function stop() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$t = \microtime(true); |
71
|
|
|
if (!$this->end) { |
72
|
|
|
$this->end = $t; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->dateTime($this->end); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function duration($precision = 0) |
82
|
|
|
{ |
83
|
|
|
$this->stop(); |
84
|
|
|
|
85
|
|
|
$start = $this->decimal($this->start); |
86
|
|
|
$end = $this->decimal($this->end); |
87
|
|
|
|
88
|
|
|
return Decimal::fromDecimal($end->sub($start) |
89
|
|
|
->mul(Decimal::fromInteger(1000)), $precision)->asFloat(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Decimal $time The time to format to a DateTimeImmutable |
94
|
|
|
* |
95
|
|
|
* @return \DateTimeImmutable |
96
|
|
|
*/ |
97
|
|
|
private function dateTime($time) |
98
|
|
|
{ |
99
|
|
|
$time = $this->decimal($time); |
|
|
|
|
100
|
|
|
$micro = sprintf('%06d', $this->mantissa($time)->asInteger()); |
101
|
|
|
|
102
|
|
|
return new DateTimeImmutable( |
103
|
|
|
\date('Y-m-d H:i:s.'.$micro, $time->asFloat()) |
|
|
|
|
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Decimal $time The time to get the mantissa from |
109
|
|
|
* |
110
|
|
|
* @return Decimal |
111
|
|
|
*/ |
112
|
|
|
private function mantissa(Decimal $time) |
113
|
|
|
{ |
114
|
|
|
$mantissa = ($time->sub($time->floor())); |
115
|
|
|
|
116
|
|
|
return $mantissa->mul(Decimal::fromInteger(1000000)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param float $t The float to turn into a Decimal |
121
|
|
|
* |
122
|
|
|
* @return Decimal |
123
|
|
|
*/ |
124
|
|
|
private function decimal($t) |
125
|
|
|
{ |
126
|
|
|
return Decimal::fromFloat($t); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|