Conditions | 9 |
Paths | 22 |
Total Lines | 40 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
22 | function calculateNetworkThroughput($interface): float |
||
|
|||
23 | { |
||
24 | $memory = Memory::getInstance(); |
||
25 | |||
26 | $file = new File(); |
||
27 | |||
28 | if (!$file->fileExists('/proc/net/dev')) return 0; |
||
29 | |||
30 | $lines = $file->getContents('/proc/net/dev', true); |
||
31 | foreach ($lines as $line) { |
||
32 | if (strpos($line, $interface . ':') !== false) { |
||
33 | $parts = preg_split('/\s+/', trim($line)); |
||
34 | $rx = (int)$parts[1]; |
||
35 | $tx = (int)$parts[9]; |
||
36 | $total = $rx + $tx; |
||
37 | break; |
||
38 | } |
||
39 | } |
||
40 | |||
41 | if (!isset($total)) return 0; |
||
42 | |||
43 | $history = $memory->getData(self::MEMORY_KEY) ?? []; |
||
44 | $lastTotal = end($history); |
||
45 | |||
46 | if (!$lastTotal) $lastTotal = 0; |
||
47 | |||
48 | if (!empty($history) && $total < $lastTotal) { |
||
49 | $memory->addData(self::MEMORY_KEY, $total); |
||
50 | return $total; |
||
51 | } |
||
52 | |||
53 | if (!empty($history)) { |
||
54 | $mb = $total - $lastTotal; |
||
55 | } else { |
||
56 | $mb = 0; |
||
57 | } |
||
58 | |||
59 | $memory->addData(self::MEMORY_KEY, $total); |
||
60 | |||
61 | return $mb; |
||
62 | } |
||
63 | } |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.