|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Phperf; |
|
4
|
|
|
|
|
5
|
|
|
class Percentiller |
|
6
|
|
|
{ |
|
7
|
|
|
public $maxBuckets = 10; |
|
8
|
|
|
|
|
9
|
|
|
private $totalBuckets = 0; |
|
10
|
|
|
/** @var mixed */ |
|
11
|
|
|
public $min; |
|
12
|
|
|
/** @var mixed */ |
|
13
|
|
|
public $max; |
|
14
|
|
|
/** @var int */ |
|
15
|
|
|
public $totalCount = 0; |
|
16
|
|
|
/** @var double */ |
|
17
|
|
|
public $arithmeticAverage = 0; |
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
public $buckets; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** @var int */ |
|
23
|
|
|
public $captureTopItems = 0; |
|
24
|
|
|
private $topMetas = array(); |
|
25
|
|
|
private $topValues = array(); |
|
26
|
|
|
private $topCount = 0; |
|
27
|
|
|
private $topLast = null; |
|
28
|
|
|
|
|
29
|
|
|
/** @var int */ |
|
30
|
|
|
public $captureBottomItems = 0; |
|
31
|
|
|
private $bottomMetas = array(); |
|
32
|
|
|
private $bottomValues = array(); |
|
33
|
|
|
private $bottomCount = 0; |
|
34
|
|
|
private $bottomLast = null; |
|
35
|
|
|
|
|
36
|
|
|
private $sampleId = 0; |
|
37
|
|
|
|
|
38
|
|
|
public function add($value, $meta = false) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->arithmeticAverage = $this->arithmeticAverage * ($this->totalCount / ($this->totalCount + 1)) |
|
41
|
|
|
+ $value / ($this->totalCount + 1); |
|
42
|
|
|
|
|
43
|
|
|
++$this->totalCount; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
if ($this->captureTopItems) { |
|
47
|
|
|
if ($this->topCount < $this->captureTopItems || $this->topLast === null || $this->topLast < $value) { |
|
48
|
|
|
++$this->sampleId; |
|
49
|
|
|
$this->topMetas [$this->sampleId]= $meta; |
|
50
|
|
|
$this->topValues [$this->sampleId]= $value; |
|
51
|
|
|
++$this->topCount; |
|
52
|
|
|
|
|
53
|
|
|
if ($this->topCount > $this->captureTopItems) { |
|
54
|
|
|
arsort($this->topValues); |
|
55
|
|
|
end($this->topValues); |
|
56
|
|
|
$sampleId = key($this->topValues); |
|
57
|
|
|
unset($this->topValues[$sampleId]); |
|
58
|
|
|
unset($this->topMetas[$sampleId]); |
|
59
|
|
|
--$this->topCount; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
if ($this->captureBottomItems) { |
|
66
|
|
|
if ($this->bottomCount < $this->captureBottomItems || $this->bottomLast === null || $this->bottomLast > $value) { |
|
67
|
|
|
++$this->sampleId; |
|
68
|
|
|
$this->bottomMetas [$this->sampleId]= $meta; |
|
69
|
|
|
$this->bottomValues [$this->sampleId]= $value; |
|
70
|
|
|
++$this->bottomCount; |
|
71
|
|
|
|
|
72
|
|
|
if ($this->bottomCount > $this->captureBottomItems) { |
|
73
|
|
|
asort($this->bottomValues); |
|
74
|
|
|
end($this->bottomValues); |
|
75
|
|
|
$sampleId = key($this->bottomValues); |
|
76
|
|
|
unset($this->bottomValues[$sampleId]); |
|
77
|
|
|
unset($this->bottomMetas[$sampleId]); |
|
78
|
|
|
--$this->bottomCount; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if (null === $this->buckets) { |
|
85
|
|
|
$this->buckets = array(array($value, $value, 1, $meta)); |
|
86
|
|
|
$this->min = $this->max = $value; |
|
87
|
|
|
$this->totalBuckets = 1; |
|
88
|
|
|
} |
|
89
|
|
|
else { |
|
90
|
|
|
if ($value < $this->min) { |
|
91
|
|
|
$this->min = $value; |
|
92
|
|
|
array_unshift($this->buckets, array($value, $value, 1, $meta)); |
|
93
|
|
|
$this->totalBuckets++; |
|
94
|
|
|
} |
|
95
|
|
|
elseif ($value > $this->max) { |
|
96
|
|
|
$this->max = $value; |
|
97
|
|
|
array_push($this->buckets, array($value, $value, 1, $meta)); |
|
98
|
|
|
$this->totalBuckets++; |
|
99
|
|
|
} |
|
100
|
|
|
elseif ($value === $this->min) { |
|
101
|
|
|
++$this->buckets[0][2]; |
|
102
|
|
|
} |
|
103
|
|
|
elseif ($value === $this->max) { |
|
104
|
|
|
++$this->buckets[$this->totalBuckets - 1][2]; |
|
105
|
|
|
} |
|
106
|
|
|
else { |
|
107
|
|
|
$this->insert($value, $meta); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if ($this->totalBuckets > $this->maxBuckets) { |
|
112
|
|
|
$this->shrink(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
private function insert($value, $meta = false) { |
|
117
|
|
|
$left = 0; |
|
118
|
|
|
$right = $this->totalBuckets - 1; |
|
119
|
|
|
while ($right - $left > 0) { |
|
120
|
|
|
$position = (int)(($right + $left)/2); |
|
121
|
|
|
$item = $this->buckets[$position]; |
|
122
|
|
|
if ($value < $item[0]) { |
|
123
|
|
|
$right = $position - 1; |
|
124
|
|
|
} |
|
125
|
|
|
elseif ($value > $item[1]) { |
|
126
|
|
|
$left = $position + 1; |
|
127
|
|
|
} |
|
128
|
|
|
else { |
|
129
|
|
|
++$this->buckets[$position][2]; |
|
130
|
|
|
return; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$position = $left; |
|
135
|
|
|
$item = $this->buckets[$position]; |
|
136
|
|
|
if ($value < $item[0]) { |
|
137
|
|
|
array_splice($this->buckets, $position, 0, array(array($value, $value, 1, $meta))); |
|
138
|
|
|
++$this->totalBuckets; |
|
139
|
|
|
} |
|
140
|
|
|
elseif ($value > $item[1]) { |
|
141
|
|
|
array_splice($this->buckets, $position + 1, 0, array(array($value, $value, 1, $meta))); |
|
142
|
|
|
++$this->totalBuckets; |
|
143
|
|
|
} |
|
144
|
|
|
else { |
|
145
|
|
|
++$this->buckets[$position][2]; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
private function shrink() { |
|
151
|
|
|
$minPosition = 0; |
|
152
|
|
|
$minCount = $this->buckets[$minPosition][2]; |
|
153
|
|
|
$mergePosition = 1; |
|
154
|
|
|
$mergeCount = $this->buckets[$mergePosition][2]; |
|
155
|
|
|
|
|
156
|
|
|
for ($position = 1; $position < $this->totalBuckets; ++$position) { |
|
157
|
|
|
$currentCount = $this->buckets[$position][2]; |
|
158
|
|
|
if ($position === $this->totalBuckets - 1) { |
|
159
|
|
|
$currentMergePosition = $position - 1; |
|
160
|
|
|
} |
|
161
|
|
|
else { |
|
162
|
|
|
$currentMergePosition = $this->buckets[$position + 1][2] < $this->buckets[$position - 1][2] |
|
163
|
|
|
? $position + 1 |
|
164
|
|
|
: $position - 1; |
|
165
|
|
|
} |
|
166
|
|
|
$currentMergeCount = $this->buckets[$currentMergePosition][2]; |
|
167
|
|
|
if ($currentCount < $minCount |
|
168
|
|
|
|| ($currentCount === $minCount |
|
169
|
|
|
&& $currentMergeCount < $mergeCount)) { |
|
170
|
|
|
$minPosition = $position; |
|
171
|
|
|
$minCount = $currentCount; |
|
172
|
|
|
$mergePosition = $currentMergePosition; |
|
173
|
|
|
$mergeCount = $currentMergeCount; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if ($mergePosition > $minPosition) { |
|
178
|
|
|
$this->buckets[$minPosition][1] = $this->buckets[$mergePosition][1]; |
|
179
|
|
|
} |
|
180
|
|
|
else { |
|
181
|
|
|
$this->buckets[$minPosition][0] = $this->buckets[$mergePosition][0]; |
|
182
|
|
|
} |
|
183
|
|
|
$this->buckets[$minPosition][2] += $this->buckets[$mergePosition][2]; |
|
184
|
|
|
unset($this->buckets[$mergePosition]); |
|
185
|
|
|
$this->buckets = array_values($this->buckets); |
|
186
|
|
|
$this->totalBuckets--; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
View Code Duplication |
public function bottomPercentile($percentile = 0.9) { |
|
|
|
|
|
|
191
|
|
|
$currentCount = 0; |
|
192
|
|
|
$percentileCount = $this->totalCount * $percentile; |
|
193
|
|
|
$bucket = array(0, 0, 0); |
|
194
|
|
|
for ($i = 0; $i < $this->totalBuckets; ++$i) { |
|
195
|
|
|
$bucket = $this->buckets[$i]; |
|
196
|
|
|
$currentCount += $bucket[2]; |
|
197
|
|
|
if ($currentCount >= $percentileCount) { |
|
198
|
|
|
return $bucket[1]; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
return $bucket[1]; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
View Code Duplication |
public function topPercentile($percentile = 0.9) { |
|
|
|
|
|
|
205
|
|
|
$currentCount = 0; |
|
206
|
|
|
$percentileCount = $this->totalCount * $percentile; |
|
207
|
|
|
$bucket = array(0, 0, 0); |
|
208
|
|
|
for ($i = $this->totalBuckets - 1; $i >= 0; --$i) { |
|
209
|
|
|
$bucket = $this->buckets[$i]; |
|
210
|
|
|
$currentCount += $bucket[2]; |
|
211
|
|
|
if ($currentCount >= $percentileCount) { |
|
212
|
|
|
return $bucket[0]; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
return $bucket[0]; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public function getTopValues() { |
|
219
|
|
|
return array_values($this->topValues); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function getTopMetas() { |
|
223
|
|
|
$result = array(); |
|
224
|
|
|
foreach ($this->topValues as $sampleId => $tmp) { |
|
225
|
|
|
$result []= $this->topMetas[$sampleId]; |
|
226
|
|
|
} |
|
227
|
|
|
return $result; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function getBottomValues() { |
|
231
|
|
|
return array_values($this->bottomValues); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function getBottomMetas() { |
|
235
|
|
|
$result = array(); |
|
236
|
|
|
foreach ($this->bottomValues as $sampleId => $tmp) { |
|
237
|
|
|
$result []= $this->bottomMetas[$sampleId]; |
|
238
|
|
|
} |
|
239
|
|
|
return $result; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.