-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaverage-speed-class.php
409 lines (367 loc) · 8.09 KB
/
average-speed-class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
/**
* Class Average_Speed
*/
class Average_Speed {
/**
* The string used to separate log file items.
*
* @var string
*/
private $item_separator = '--------------------------';
/**
* Property to store log file items.
*
* @var null
*/
private $items = null;
/**
* Log file path.
*
* @var null
*/
private $file_path = null;
/**
* Log file content.
*
* @var null
*/
private $file_content = null;
/**
* By hour statistics property.
*
* @var array
*/
private $by_hour = [];
/**
* By weekday statistics property.
*
* @var array
*/
private $by_weekday = [];
/**
* Server list.
*
* @var array
*/
private $servers = [];
/**
* Average_Speed constructor.
*
* @param $file_path
*/
public function __construct($file_path) {
$this->file_path = $file_path;
if ( ! $this->read_file() ) {
return false;
}
$this->separate_items();
$this->parse_items();
return true;
}
/**
* Separate the log file items into array.
*/
private function separate_items() {
$items = explode($this->item_separator, $this->file_content);
$items = array_filter($items, function($item) {
return ! empty( trim($item) );
});
$this->items = $items;
}
/**
* Read the log file.
*/
private function read_file() {
if ( ! file_exists($this->file_path) || ! is_readable($this->file_path) ) return false;
$this->file_content = file_get_contents($this->file_path);
return true;
}
/**
* Parse the datetime from log file item.
*
* @param $item
*
* @return DateTime
* @throws Exception
*/
private function parse_datetime($item) {
preg_match('/Speed test start: (.+)/', $item, $matches);
$datetime_raw = $matches[1];
$timestamp = strtotime($datetime_raw);
$date = new DateTime();
$date->setTimestamp($timestamp);
$date->setTimezone(new DateTimeZone('Europe/Oslo'));
return $date;
}
/**
* Parse download speed from log item.
*
* @param $item
*
* @return float
*/
private function parse_download_speed($item) {
preg_match('/Download: (.*) Mbps/', $item, $matches);
return isset($matches[1]) ? (float) $matches[1] : 0;
}
/**
* Parse upload speed from log item.
*
* @param $item
*
* @return float
*/
private function parse_upload_speed($item) {
preg_match('/Upload: (.*) Mbps/', $item, $matches);
return isset($matches[1]) ? (float) $matches[1] : 0;
}
/**
* Parse server from log item.
*
* @param $item
*
* @return string
*/
private function parse_server($item) {
preg_match('/Server: (.*) -/', $item, $matches);
return isset($matches[1]) ? trim($matches[1]) : false;
}
/**
* Parse speed from log item.
*
* @param $item
*
* @return array
*/
private function parse_speed($item) {
return [
'download_speed' => $this->parse_download_speed($item),
'upload_speed' => $this->parse_upload_speed($item),
];
}
/**
* Add item statistics to hour statistics.
*
* @param $date
* @param $speed_array
*/
private function add_to_hour($date, $speed_array) {
$hour = $date->format('H');
if ( ! array_key_exists($hour, $this->by_hour) ) $this->by_hour[$hour] = [];
$this->by_hour[$hour][] = $speed_array;
}
/**
* Add item statistics to weekday statistics.
*
* @param $date
* @param $speed_array
*/
private function add_to_weekday($date, $speed_array) {
$weekday = $date->format('N');
if ( ! array_key_exists($weekday, $this->by_weekday) ) $this->by_weekday[$weekday] = [];
$this->by_weekday[$weekday][] = $speed_array;
}
/**
* Add server to server list.
*
* @param $server
*/
private function add_to_server_list($server) {
if ( $server && ! in_array($server, $this->servers) ) {
$this->servers[] = $server;
}
}
/**
* Prepare results table.
*
* @param $first_column
*
* @return \LucidFrame\Console\ConsoleTable
*/
public function prepare_table($first_column) {
$table = new LucidFrame\Console\ConsoleTable();
$table->addHeader($first_column)
->addHeader('Number of measurements')
->addHeader('Average speed down')
->addHeader('Average speed up')
->addHeader('Speed down min')
->addHeader('Speed down max')
->addHeader('Speed up min')
->addHeader('Speed up max');
return $table;
}
/**
* Parse log file items.
*/
private function parse_items() {
$this->items = array_map(function($item) {
$date = $this->parse_datetime($item);
$datetime = $date->format('Y-m-d H:i:s (e)');
$speed_array = $this->parse_speed($item);
$server = $this->parse_server($item);
return array_merge($speed_array, compact('speed_array', 'datetime', 'date', 'server'));
}, $this->items);
$this->items = array_filter($this->items, function($item) {
return ( $item['download_speed'] > 0 && $item['upload_speed'] > 0);
});
$this->items = array_values($this->items);
$this->items = array_map(function($item) {
$this->add_to_server_list($item['server']);
$this->add_to_hour($item['date'], $item['speed_array']);
$this->add_to_weekday($item['date'], $item['speed_array']);
unset($item['speed_array']);
return $item;
}, $this->items);
}
/**
* Count items.
*
* @param $items
*
* @return int
*/
public function count_items($items) {
return count($items);
}
/**
* Get items.
*
* @return null
*/
public function get_items() {
return $this->items;
}
/**
* Get the first log item.
*
* @return mixed
*/
public function first_record_date() {
$items = $this->get_items();
return current($items)['datetime'];
}
/**
* Get the latest log item.
*
* @return mixed
*/
public function latest_record_date() {
$items = $this->get_items();
return end($items)['datetime'];
}
/**
* Get all servers included in the log file.
*
* @param bool $as_string
* @param string $separator
*
* @return array|string
*/
public function get_servers($as_string = false, $separator = ', ') {
if ( $as_string ) {
return implode($this->servers, $separator);
}
return $this->servers;
}
/**
* Format the speed value.
*
* @param $speed
*
* @return string
*/
private function format_speed($speed) {
return number_format($speed, 3, '.', '');
}
/**
* Flatten array based on a specific key.
*
* @param $key
* @param $items
*
* @return array
*/
private function isolate_values_by_key($key, $items) {
return array_map(function($item) use ($key) { return $item[$key . '_speed']; }, $items);
}
/**
* Calculate average speed.
*
* @param $key
* @param $items
*
* @return string
*/
private function calculate_average_speed($key, $items) {
$sum = array_sum( $this->isolate_values_by_key($key, $items) );
return $this->format_speed($sum / $this->count_items($items));
}
/**
* Get average up/down speed.
* @param array $items
*
* @return object
*/
public function get_average_speed($items = []) {
if ( empty($items) ) {
$items = $this->get_items();
}
return (object) [
'down' => $this->calculate_average_speed('download', $items),
'up' => $this->calculate_average_speed('upload', $items),
];
}
/**
* Get minimum and maximum speed value.
*
* @param $key
* @param $items
*
* @return object
*/
public function get_min_max_speed($key, $items) {
$values = $this->isolate_values_by_key($key, $items);
return (object) [
'min' => min($values),
'max' => max($values),
];
}
/**
* Parse speed data by key.
*
* @param $key
*
* @return array
*/
private function parse_by_key($key) {
$items = [];
foreach ( $this->{$key} as $key => $value ) {
$min_max = (object) [
'down' => $this->get_min_max_speed('download', $value),
'up' => $this->get_min_max_speed('upload', $value),
];
$count = count($value);
$average = $this->get_average_speed($value);
$items[$key] = (object) compact('min_max', 'count', 'average');
}
ksort($items);
return $items;
}
/**
* Get items by hour.
*
* @return array
*/
public function get_items_by_hour() {
return $this->parse_by_key('by_hour');
}
/**
* Get items by weekday.
*
* @return array
*/
public function get_items_by_weekday() {
return $this->parse_by_key('by_weekday');
}
}