PocketMine-MP 5.18.1 git-9381fc4172e5dce4cada1cb356050c8a2ab57b94
BandwidthStatsTracker.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network;
25
26use function array_fill;
27use function array_sum;
28use function count;
29
32 private array $history;
33 private int $nextHistoryIndex = 0;
34 private int $bytesSinceLastRotation = 0;
35 private int $totalBytes = 0;
36
38 public function __construct(int $historySize){
39 $this->history = array_fill(0, $historySize, 0);
40 }
41
42 public function add(int $bytes) : void{
43 $this->totalBytes += $bytes;
44 $this->bytesSinceLastRotation += $bytes;
45 }
46
47 public function getTotalBytes() : int{ return $this->totalBytes; }
48
54 public function rotateHistory() : void{
55 $this->history[$this->nextHistoryIndex] = $this->bytesSinceLastRotation;
56 $this->bytesSinceLastRotation = 0;
57 $this->nextHistoryIndex = ($this->nextHistoryIndex + 1) % count($this->history);
58 }
59
63 public function getAverageBytes() : float{
64 return array_sum($this->history) / count($this->history);
65 }
66
67 public function resetHistory() : void{
68 $this->history = array_fill(0, count($this->history), 0);
69 }
70}