Hoa central
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Hoa\Bench\Bench Class Reference
Inheritance diagram for Hoa\Bench\Bench:

Public Member Functions

 __get ($id)
 
 __isset ($id)
 
 __unset ($id)
 
 unsetAll ()
 
 current ()
 
 key ()
 
 next ()
 
 rewind ()
 
 valid ()
 
 filter ($callable)
 
 getFilters ()
 
 getStatistic ($considerFilters=true)
 
 getLongest ()
 
 drawStatistic ($width=80)
 
 count ()
 
 __toString ()
 

Public Attributes

const STAT_RESULT = 0
 
const STAT_POURCENT = 1
 

Protected Member Functions

 markExists ($id)
 

Protected Attributes

 $_filters = []
 

Static Protected Attributes

static $_mark = []
 

Detailed Description

Class .

The class allows to manage marks easily, and to make some statistics. The class implements Iterator and Countable interfaces to iterate marks, or count the number of marks.

Definition at line 53 of file Bench.php.

Member Function Documentation

Hoa\Bench\Bench::__get (   $id)

Get a mark. If the mark does not exist, it will be automatically create.

Parameters
string$idThe mark ID.
Returns
Exceptions

Definition at line 93 of file Bench.php.

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  }
markExists($id)
Definition: Bench.php:154
const GLOBAL_NAME
Definition: Mark.php:55

Here is the call graph for this function:

Hoa\Bench\Bench::__isset (   $id)

Check if a mark exists. Alias of the protected markExist method.

Parameters
string$idThe mark ID.
Returns
bool

Definition at line 118 of file Bench.php.

119  {
120  return $this->markExists($id);
121  }
markExists($id)
Definition: Bench.php:154

Here is the call graph for this function:

Hoa\Bench\Bench::__toString ( )

Alias of drawStatistic() method.

Returns
string

Definition at line 376 of file Bench.php.

377  {
378  return $this->drawStatistic();
379  }
drawStatistic($width=80)
Definition: Bench.php:320

Here is the call graph for this function:

Hoa\Bench\Bench::__unset (   $id)

Destroy a mark.

Parameters
string$idThe mark ID.
Returns
void

Definition at line 129 of file Bench.php.

130  {
131  unset(self::$_mark[$id]);
132 
133  return;
134  }
Hoa\Bench\Bench::count ( )

Count the number of mark.

Returns
int

Definition at line 366 of file Bench.php.

367  {
368  return count(self::$_mark);
369  }
Hoa\Bench\Bench::current ( )

Get the current mark for the iterator.

Returns

Definition at line 164 of file Bench.php.

165  {
166  return current(self::$_mark);
167  }
Hoa\Bench\Bench::drawStatistic (   $width = 80)

Draw statistic in text mode.

Parameters
int$widthThe graphic width.
Returns
string
Exceptions

Definition at line 320 of file Bench.php.

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  }
getStatistic($considerFilters=true)
Definition: Bench.php:263

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Bench\Bench::filter (   $callable)

Add a filter. Used in the self::getStatistic() method, no in iterator. A filter is a callable that will receive 3 values about a mark: ID, time result, and time pourcent. The callable must return a boolean.

Parameters
mixed$callableCallable.
Returns
void

Definition at line 236 of file Bench.php.

237  {
238  $this->_filters[] = xcallable($callable);
239 
240  return $this;
241  }
Hoa\Bench\Bench::getFilters ( )

Return all filters.

Returns
array

Definition at line 248 of file Bench.php.

249  {
250  return $this->_filters;
251  }

Here is the caller graph for this function:

Hoa\Bench\Bench::getLongest ( )

Get the maximum, i.e. the longest mark in time.

Returns

Definition at line 298 of file Bench.php.

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  }

Here is the caller graph for this function:

Hoa\Bench\Bench::getStatistic (   $considerFilters = true)

Get statistic. Return an associative array : id => sub-array. The sub-array contains the result time in second (given by the constant self::STAT_RESULT), and the result pourcent (given by the constant self::START_POURCENT).

Parameters
bool$considerFiltersWhether we should consider filters or not.
Returns
array

Definition at line 263 of file Bench.php.

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  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Bench\Bench::key ( )

Get the current mark ID for the iterator.

Returns
string

Definition at line 174 of file Bench.php.

175  {
176  return key(self::$_mark);
177  }

Here is the caller graph for this function:

Hoa\Bench\Bench::markExists (   $id)
protected

Check if a mark already exists.

Parameters
string$idThe mark ID.
Returns
bool

Definition at line 154 of file Bench.php.

155  {
156  return isset(self::$_mark[$id]);
157  }

Here is the caller graph for this function:

Hoa\Bench\Bench::next ( )

Advance the internal mark collection pointer, and return the current mark.

Returns

Definition at line 185 of file Bench.php.

186  {
187  return next(self::$_mark);
188  }

Here is the caller graph for this function:

Hoa\Bench\Bench::rewind ( )

Rewind the internal mark collection pointer, and return the first mark.

Returns

Definition at line 195 of file Bench.php.

196  {
197  return reset(self::$_mark);
198  }
Hoa\Bench\Bench::unsetAll ( )

Destroy all mark.

Returns
void

Definition at line 141 of file Bench.php.

142  {
143  self::$_mark = [];
144 
145  return;
146  }
Hoa\Bench\Bench::valid ( )

Check if there is a current element after calls the rewind or the next methods.

Returns
bool

Definition at line 206 of file Bench.php.

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  }

Here is the call graph for this function:

Member Data Documentation

Hoa\Bench\Bench::$_filters = []
protected

Definition at line 81 of file Bench.php.

Hoa\Bench\Bench::$_mark = []
staticprotected

Definition at line 74 of file Bench.php.

const Hoa\Bench\Bench::STAT_POURCENT = 1

Statistic : get the pourcent.

int

Definition at line 67 of file Bench.php.

const Hoa\Bench\Bench::STAT_RESULT = 0

Statistic : get the result.

int

Definition at line 60 of file Bench.php.


The documentation for this class was generated from the following file: