RSI helpers  0.1
RSI helpers
Timer.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi;
4 
5 class Timer{
6 
7  public $decimals = 3;
8  public $times = []; //!< Times (value) per id (key).
9  public $count = [];
10 
11  protected $_id = null; //!< Active id.
12  protected $_start = null; //!< Time the active id started.
13 
14  /**
15  * Stop the running timer.
16  */
17  public function stop(){
18  if($this->_id) $this->times[$this->_id] += microtime(true) - $this->_start;
19  $this->_id = null;
20  }
21  /**
22  * Start a timer.
23  * @param string $id ID for the timer.
24  */
25  public function start($id){
26  $this->stop();
27  if(!array_key_exists($this->_id = $id,$this->times)) $this->times[$id] = $this->count[$id] = 0;
28  $this->_start = microtime(true);
29  $this->count[$id]++;
30  }
31  /**
32  * Time for a single ID.
33  * @param string $id ID of the timer.
34  * @return float Time (including active timer).
35  */
36  public function time($id){
37  if(!array_key_exists($id,$this->times)) return false;
38  $time = $this->times[$id];
39  if($this->_id == $id) $time += microtime(true) - $this->_start;
40  return $time;
41  }
42  /**
43  * Total time.
44  * @return float
45  */
46  public function total(){
47  $total = array_sum($this->times);
48  if($this->_id) $total += microtime(true) - $this->_start;
49  return $total;
50  }
51 
52  protected function pad($value,$length){
53  return str_pad($value,$length,' ',STR_PAD_LEFT);
54  }
55  /**
56  * Text representation of times.
57  * @return string
58  */
59  public function asText(){
60  if(!$this->times) return false;
61  $id_length = max(5,max(array_map('strlen',array_keys($this->times))));
62  $time_length = max(8,strlen($total_str = number_format($total_time = $this->total(),$this->decimals)));
63  $count_length = max(5,strlen($count_total_str = number_format(array_sum($this->count))));
64  $average = [];
65  $average_length = 6;
66  foreach($this->times as $id => $time) $average_length = max($average_length,strlen($average[$id] = number_format($time / $this->count[$id],min(6,$this->decimals + ceil(log($this->count[$id],10))))));
67  $text =
68  implode(' | ',[str_pad('ID',$id_length),$this->pad('time [s]',$time_length),' %',$this->pad('count',$count_length),$this->pad('avg [s]',$average_length)]) . "\n" .
69  ($border = implode('-+-',[str_repeat('-',$id_length),str_repeat('-',$time_length),'-----',str_repeat('-',$count_length),str_repeat('-',$average_length)]) . "\n");
70  foreach($this->times as $id => $time) $text .= implode(' | ',[
71  str_pad($id,$id_length),
72  $this->pad(number_format($time,$this->decimals),$time_length),
73  $this->pad(number_format(100 * $time / $total_time,1),5),
74  $this->pad(number_format($this->count[$id]),$count_length),
75  $this->pad($average[$id],$average_length)
76  ]) . "\n";
77  return $text . $border . implode(' | ',[str_pad('Total',$id_length),$this->pad($total_str,$time_length),number_format(100,1),$this->pad($count_total_str,$count_length),'']);
78  }
79  /**
80  * HTML representation of times.
81  * @return string
82  */
83  public function asHtml(){
84  $total_time = $this->total();
85  $html = "<table class='timer'>\n<tr><th>ID</th><th>time [s]</th><th>%</th><th>count</th><th>avg [s]</th></tr>\n";
86  foreach($this->times as $id => $time) $html .= '<tr><td>' . implode('</td><td>',[
87  $id,
88  number_format($time,$this->decimals),
89  number_format(100 * $time / $total_time,1),
90  number_format($this->count[$id]),
91  number_format($time / $this->count[$id],$this->decimals + ceil(log($this->count[$id],10)))
92  ]) . "</td></tr>\n";
93  return $html .
94  '<tr><th>' .
95  implode('</th><th>',['Total',number_format($total_time,$this->decimals),number_format(100,1),number_format(array_sum($this->count)),'']) .
96  "</th></tr>\n</table>";
97  }
98 
99  public function __get($name){
100  return $this->time($name);
101  }
102 
103  public function __call($name,$arguments){
104  $this->start($name);
105  }
106 
107  public function __toString(){
108  return $this->asText();
109  }
110 
111 }
__call($name, $arguments)
Definition: Timer.php:103
__get($name)
Definition: Timer.php:99
total()
Total time.
Definition: Timer.php:46
__toString()
Definition: Timer.php:107
asHtml()
HTML representation of times.
Definition: Timer.php:83
time($id)
Time for a single ID.
Definition: Timer.php:36
Definition: Color.php:3
start($id)
Start a timer.
Definition: Timer.php:25
asText()
Text representation of times.
Definition: Timer.php:59
stop()
Stop the running timer.
Definition: Timer.php:17
pad($value, $length)
Definition: Timer.php:52