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
|
|
|
final 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
|
|
|
if (null !== $start) { |
50
|
|
|
$this->setStart($start); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function start() |
58
|
|
|
{ |
59
|
|
|
$t = \microtime(true); |
60
|
|
|
$this->setStart($t); |
61
|
|
|
|
62
|
|
|
return $this->dateTime($this->start); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function stop() |
69
|
|
|
{ |
70
|
|
|
$t = \microtime(true); |
71
|
|
|
$this->setEnd($t); |
72
|
|
|
|
73
|
|
|
return $this->dateTime($this->end); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function duration($precision = 0) |
80
|
|
|
{ |
81
|
|
|
$this->stop(); |
82
|
|
|
|
83
|
|
|
$start = $this->decimal($this->start); |
84
|
|
|
$end = $this->decimal($this->end); |
85
|
|
|
|
86
|
|
|
return Decimal::fromDecimal($end->sub($start) |
87
|
|
|
->mul(Decimal::fromInteger(1000)), $precision)->asFloat(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param float $t The time to format to a DateTimeImmutable |
92
|
|
|
* |
93
|
|
|
* @return \DateTimeImmutable |
94
|
|
|
*/ |
95
|
|
|
private function dateTime($t) |
96
|
|
|
{ |
97
|
|
|
$time = $this->decimal($t); |
98
|
|
|
$micro = sprintf('%06d', $this->mantissa($time)->asInteger()); |
99
|
|
|
|
100
|
|
|
return new DateTimeImmutable( |
101
|
|
|
\date('Y-m-d H:i:s.'.$micro, $time->asInteger()) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Decimal $time The time to get the mantissa from |
107
|
|
|
* |
108
|
|
|
* @return Decimal |
109
|
|
|
*/ |
110
|
|
|
private function mantissa(Decimal $time) |
111
|
|
|
{ |
112
|
|
|
$mantissa = ($time->sub($time->floor())); |
113
|
|
|
|
114
|
|
|
return $mantissa->mul(Decimal::fromInteger(1000000)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param float $t The float to turn into a Decimal |
119
|
|
|
* |
120
|
|
|
* @return Decimal |
121
|
|
|
*/ |
122
|
|
|
private function decimal($t) |
123
|
|
|
{ |
124
|
|
|
return Decimal::fromFloat($t); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param mixed $t A time to floaterize and save as start |
129
|
|
|
* |
130
|
|
|
* @return float |
131
|
|
|
*/ |
132
|
|
|
private function setStart($t) |
133
|
|
|
{ |
134
|
|
|
if (!$this->start) { |
135
|
|
|
$this->start = (float) $t; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->start; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param mixed $t A time to floaterize and save as stop time |
143
|
|
|
* |
144
|
|
|
* @return float |
145
|
|
|
*/ |
146
|
|
|
private function setEnd($t) |
147
|
|
|
{ |
148
|
|
|
if (!$this->end) { |
149
|
|
|
$this->end = (float) $t; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->end; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|