1 | <?php |
||
15 | class SpeedRateCounter |
||
16 | { |
||
17 | /** |
||
18 | * Amount of processed bytes |
||
19 | * |
||
20 | * @var int |
||
21 | */ |
||
22 | private $totalBytesProcessed; |
||
23 | |||
24 | /** |
||
25 | * Time when request is started |
||
26 | * |
||
27 | * @var double |
||
28 | */ |
||
29 | private $initialTime; |
||
30 | |||
31 | /** |
||
32 | * Time of last measurement |
||
33 | * |
||
34 | * @var double |
||
35 | */ |
||
36 | private $currentTime; |
||
37 | |||
38 | /** |
||
39 | * Time when speed felt below minimal |
||
40 | * |
||
41 | * @var double |
||
42 | */ |
||
43 | private $slowStartTime; |
||
44 | |||
45 | /** |
||
46 | * Minimum allowed speed in bytes per second |
||
47 | * |
||
48 | * @var double |
||
49 | */ |
||
50 | private $minSpeed; |
||
51 | |||
52 | /** |
||
53 | * Maximum duration of minimum speed in seconds |
||
54 | * |
||
55 | * @var double |
||
56 | */ |
||
57 | private $maxDuration; |
||
58 | |||
59 | /** |
||
60 | * Current speed |
||
61 | * |
||
62 | * @var double |
||
63 | */ |
||
64 | private $currentSpeed; |
||
65 | |||
66 | /** |
||
67 | * SpeedRateCounter constructor. |
||
68 | * |
||
69 | * @param double $minSpeed Minimum allowed speed in bytes per second |
||
70 | * @param double $maxDuration Maximum duration of minimum speed in seconds |
||
71 | */ |
||
72 | 89 | public function __construct($minSpeed, $maxDuration) |
|
78 | |||
79 | /** |
||
80 | * Resets this counter |
||
81 | * |
||
82 | * @return void |
||
83 | */ |
||
84 | 89 | public function reset() |
|
92 | |||
93 | /** |
||
94 | * Process next measurement |
||
95 | * |
||
96 | * @param double $time Time in seconds |
||
97 | * @param double $value Amount of received data in bytes |
||
98 | * |
||
99 | * @return void |
||
100 | * @throws \OverflowException When speed is slower then desired |
||
101 | */ |
||
102 | 89 | public function advance($time, $value) |
|
122 | |||
123 | /** |
||
124 | * Adds measure for counter |
||
125 | * |
||
126 | * @param double $time Time for given value in absolute timestamp |
||
127 | * @param double $value A value |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | 89 | private function measure($time, $value) |
|
141 | |||
142 | /** |
||
143 | * Return average speed for measurements |
||
144 | * |
||
145 | * @return double|null |
||
146 | */ |
||
147 | 89 | private function getAverageSpeed() |
|
153 | |||
154 | /** |
||
155 | * Return current speed in bytes per seconds |
||
156 | * |
||
157 | * @return float |
||
158 | */ |
||
159 | 88 | public function getCurrentSpeed() |
|
163 | |||
164 | /** |
||
165 | * Return duration of current slow speed |
||
166 | * |
||
167 | * @return double |
||
168 | */ |
||
169 | 10 | public function getCurrentDuration() |
|
173 | } |
||
174 |