Hoa central
Mark.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Bench;
38 
48 class Mark
49 {
55  const GLOBAL_NAME = '__global__';
56 
62  protected $_id = null;
63 
69  protected $start = 0.0;
70 
76  protected $stop = 0.0;
77 
83  protected $pause = 0.0;
84 
90  protected $_running = false;
91 
97  protected $_pause = false;
98 
99 
100 
107  public function __construct($id)
108  {
109  $this->setId($id);
110 
111  return;
112  }
113 
120  protected function setId($id)
121  {
122  $old = $this->_id;
123  $this->_id = $id;
124 
125  return $old;
126  }
127 
133  public function getId()
134  {
135  return $this->_id;
136  }
137 
146  public function start()
147  {
148  if (true === $this->isRunning()) {
149  if (false === $this->isPause()) {
150  throw new Exception(
151  'Cannot start the %s mark, because it is running.',
152  0,
153  $this->getId()
154  );
155  }
156  }
157 
158  if (true === $this->isPause()) {
159  $this->pause += microtime(true) - $this->stop;
160  } else {
161  $this->reset();
162  $this->start = microtime(true);
163  }
164 
165  $this->_running = true;
166  $this->_pause = false;
167 
168  return $this;
169  }
170 
181  public function stop($silent = false)
182  {
183  if (false === $this->isRunning()) {
184  if (false === $silent) {
185  throw new Exception(
186  'Cannot stop the %s mark, because it is not running.',
187  1,
188  $this->getId()
189  );
190  } else {
191  return $this;
192  }
193  }
194 
195  $this->stop = microtime(true);
196  $this->_running = false;
197  $this->_pause = false;
198 
199  return $this;
200  }
201 
207  public function reset()
208  {
209  $this->start = 0.0;
210  $this->stop = 0.0;
211  $this->pause = 0.0;
212  $this->_running = false;
213  $this->_pause = false;
214 
215  return $this;
216  }
217 
229  public function pause($silent = false)
230  {
231  if (false === $this->isRunning()) {
232  if (false === $silent) {
233  throw new Exception(
234  'Cannot stop the %s mark, because it is not running.',
235  2,
236  $this->getId()
237  );
238  } else {
239  return $this;
240  }
241  }
242 
243  if (true === $this->isPause()) {
244  if (false === $silent) {
245  throw new Exception(
246  'The %s mark is still in pause. Cannot pause it again.',
247  3,
248  $this->getId()
249  );
250  } else {
251  return $this;
252  }
253  }
254 
255  $this->stop = microtime(true);
256  $this->_pause = true;
257 
258  return $this;
259  }
260 
268  public function diff()
269  {
270  if (false === $this->isRunning() || true === $this->isPause()) {
271  return $this->stop - $this->start - $this->pause;
272  }
273 
274  return microtime(true) - $this->start - $this->pause;
275  }
276 
286  public function compareTo(Mark $mark)
287  {
288  $a = $this->diff();
289  $b = $mark->diff();
290 
291  if ($a < $b) {
292  return -1;
293  } elseif ($a == $b) {
294  return 0;
295  } else {
296  return 1;
297  }
298  }
299 
305  public function isRunning()
306  {
307  return $this->_running;
308  }
309 
315  public function isPause()
316  {
317  return $this->_pause;
318  }
319 
325  public function __toString()
326  {
327  return (string) $this->diff();
328  }
329 }
stop($silent=false)
Definition: Mark.php:181
compareTo(Mark $mark)
Definition: Mark.php:286
setId($id)
Definition: Mark.php:120
const GLOBAL_NAME
Definition: Mark.php:55
__construct($id)
Definition: Mark.php:107
pause($silent=false)
Definition: Mark.php:229