Hoa central
Bench.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Bench;
38 
39 use Hoa\Core;
40 use Hoa\Iterator;
41 
53 class Bench implements Iterator, \Countable
54 {
60  const STAT_RESULT = 0;
61 
67  const STAT_POURCENT = 1;
68 
74  protected static $_mark = [];
75 
81  protected $_filters = [];
82 
83 
84 
93  public function __get($id)
94  {
95  if (true === empty(self::$_mark)) {
96  $global = new Mark(Mark::GLOBAL_NAME);
97  self::$_mark[Mark::GLOBAL_NAME] = $global;
98  $global->start();
99  }
100 
101  if (true === $this->markExists($id)) {
102  return self::$_mark[$id];
103  }
104 
105  $mark = new Mark($id);
106  self::$_mark[$id] = $mark;
107 
108  return $mark;
109  }
110 
118  public function __isset($id)
119  {
120  return $this->markExists($id);
121  }
122 
129  public function __unset($id)
130  {
131  unset(self::$_mark[$id]);
132 
133  return;
134  }
135 
141  public function unsetAll()
142  {
143  self::$_mark = [];
144 
145  return;
146  }
147 
154  protected function markExists($id)
155  {
156  return isset(self::$_mark[$id]);
157  }
158 
164  public function current()
165  {
166  return current(self::$_mark);
167  }
168 
174  public function key()
175  {
176  return key(self::$_mark);
177  }
178 
185  public function next()
186  {
187  return next(self::$_mark);
188  }
189 
195  public function rewind()
196  {
197  return reset(self::$_mark);
198  }
199 
206  public function valid()
207  {
208  if (empty(self::$_mark)) {
209  return false;
210  }
211 
212  $key = key(self::$_mark);
213  $return = (next(self::$_mark) ? true : false);
214  prev(self::$_mark);
215 
216  if (false === $return) {
217  end(self::$_mark);
218 
219  if ($key === key(self::$_mark)) {
220  $return = true;
221  }
222  }
223 
224  return $return;
225  }
226 
236  public function filter($callable)
237  {
238  $this->_filters[] = xcallable($callable);
239 
240  return $this;
241  }
242 
248  public function getFilters()
249  {
250  return $this->_filters;
251  }
252 
263  public function getStatistic($considerFilters = true)
264  {
265  if (empty(self::$_mark)) {
266  return [];
267  }
268 
269  $max = $this->getLongest()->diff();
270  $out = [];
271 
272  foreach ($this as $id => $mark) {
273  $result = $mark->diff();
274  $pourcent = ($result * 100) / $max;
275 
276  if (true === $considerFilters) {
277  foreach ($this->getFilters() as $filter) {
278  if (true !== $filter($id, $result, $pourcent)) {
279  continue 2;
280  }
281  }
282  }
283 
284  $out[$id] = [
285  self::STAT_RESULT => $result,
286  self::STAT_POURCENT => $pourcent
287  ];
288  }
289 
290  return $out;
291  }
292 
298  public function getLongest()
299  {
300  $max = 0;
301  $outMark = null;
302 
303  foreach ($this as $id => $mark) {
304  if ($mark->diff() > $max) {
305  $outMark = $mark;
306  $max = $mark->diff();
307  }
308  }
309 
310  return $outMark;
311  }
312 
320  public function drawStatistic($width = 80)
321  {
322  if (empty(self::$_mark)) {
323  return '';
324  }
325 
326  if ($width < 1) {
327  throw new Exception(
328  'The graphic width must be positive, given %d.',
329  0,
330  $width
331  );
332  }
333 
334  $out = null;
335  $stats = $this->getStatistic();
336  $margin = 0;
337 
338  foreach ($stats as $id => $foo) {
339  strlen($id) > $margin and $margin = strlen($id);
340  }
341 
342  $width = $width - $margin - 18;
343  $format = '%-' . $margin . 's %-' . $width . 's %5dms, %5.1f%%' . "\n";
344 
345  foreach ($stats as $id => $stat) {
346  $out .= sprintf(
347  $format,
348  $id,
349  str_repeat(
350  '|',
351  round(($stat[self::STAT_POURCENT] * $width) / 100)
352  ),
353  round(1000 * $stat[self::STAT_RESULT]),
354  round($stat[self::STAT_POURCENT], 3)
355  );
356  }
357 
358  return $out;
359  }
360 
366  public function count()
367  {
368  return count(self::$_mark);
369  }
370 
376  public function __toString()
377  {
378  return $this->drawStatistic();
379  }
380 }
381 
385 Core\Consistency::flexEntity('Hoa\Bench\Bench');
getStatistic($considerFilters=true)
Definition: Bench.php:263
__unset($id)
Definition: Bench.php:129
const STAT_POURCENT
Definition: Bench.php:67
static $_mark
Definition: Bench.php:74
__isset($id)
Definition: Bench.php:118
const STAT_RESULT
Definition: Bench.php:60
markExists($id)
Definition: Bench.php:154
filter($callable)
Definition: Bench.php:236
drawStatistic($width=80)
Definition: Bench.php:320
const GLOBAL_NAME
Definition: Mark.php:55
__get($id)
Definition: Bench.php:93