-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Counter.php
157 lines (136 loc) · 4.18 KB
/
Counter.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
<?php declare(strict_types=1);
/*
* This file is part of the PHP Event Correlation package.
*
* (c) James Lucas <james@lucas.net.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EdgeTelemetrics\EventCorrelation;
use DateInterval;
use DateTimeImmutable;
use function microtime;
use function round;
class Counter {
const RESOLUTION_MILLISECONDS = 1;
const RESOLUTION_SECONDS = 2;
/**
* @var int
*/
protected int $counter_length;
/**
* @var int
*/
protected int $resolution;
/**
* @var array
*/
protected array $counter;
/**
* @var int
*/
protected int $lastEventTime = 0;
/**
* @param DateInterval $interval
* @param int $resolution
*/
public function __construct(DateInterval $interval, int $resolution) {
$counter_length = (new DateTimeImmutable('@0'))->add($interval)->getTimestamp();
if (self::RESOLUTION_MILLISECONDS === $resolution) {
$counter_length *= 1000;
}
$this->counter_length = $counter_length;
$this->resolution = $resolution;
$this->initCounter();
}
/**
* Increment the counter for the current time
*/
public function increment(): void
{
$time = $this->getTime();
$this->flushOldSlots($time);
$index = $this->getIndexForTime($time);
$this->counter[$index]++;
$this->lastEventTime = $time;
}
/**
* Return the current counters as an array shifted to ensure
* @return array
*/
public function getCounter() : array {
$time = $this->getTime();
$index = $this->getIndexForTime($time);
$this->flushOldSlots($time);
/** Shift the counter so that the current time modulus is the last item in the array */
return array_merge(array_slice($this->counter, $index+1), array_slice($this->counter, 0, $index+1));
}
/**
* @return int
*/
public function getLastEventTime() : int {
$timeSinceLastEvent = $this->getTime()-$this->lastEventTime;
return (self::RESOLUTION_MILLISECONDS === $this->resolution)? (int)round(microtime(true)-$timeSinceLastEvent) : time()-$timeSinceLastEvent;
}
/**
* Initialise the counter data structure
*/
protected function initCounter(): void
{
$this->counter = array_fill(0,$this->counter_length, 0);
}
/**
* Get the current time as an integer relative to the required resolution
* @return int
*/
protected function getTime() : int
{
return (self::RESOLUTION_MILLISECONDS === $this->resolution)? (int)round(hrtime(true)/1e+6) : (int)round(hrtime(true)/1e+9);
}
/**
* @param int $time
* @return int
*/
protected function getIndexForTime(int $time) : int {
return $time % $this->counter_length;
}
/**
* Set any slots between the last event time and current time to 0
* @param int $time
*/
protected function flushOldSlots(int $time): void
{
/** Don't flush if we are tracking the current time */
if ($time === $this->lastEventTime)
{
return;
}
/** Check if we have not processed an event for the max measurement period and reset all counters */
if (($time - $this->lastEventTime) >= $this->counter_length) {
$this->initCounter();
} else {
/** We are a new time period */
$lastIndex = $this->getIndexForTime($this->lastEventTime);
$currentIndex = $this->getIndexForTime($time);
if ($currentIndex >= $lastIndex)
{
for($i = $lastIndex+1; $i <= $currentIndex; $i++)
{
$this->counter[$i] = 0;
}
}
else
{
for($i = 0; $i <= $currentIndex; $i++)
{
$this->counter[$i] = 0;
}
for($i = $lastIndex+1; $i < $this->counter_length; $i++)
{
$this->counter[$i] = 0;
}
}
}
}
}