TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
Timer.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: Timer.php 8/31/2012 11:06:09 scp@orilla $
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @package TeamSpeak3
23  * @version 1.1.16
24  * @author Sven 'ScP' Paulsen
25  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
26  */
27 
28 /**
29  * @class TeamSpeak3_Helper_Profiler_Timer
30  * @brief Helper class providing profiler timers.
31  */
33 {
34  /**
35  * Indicates wether the timer is running or not.
36  *
37  * @var boolean
38  */
39  protected $running = FALSE;
40 
41  /**
42  * Stores the timestamp when the timer was last started.
43  *
44  * @var integer
45  */
46  protected $started = 0;
47 
48  /**
49  * Stores the timer name.
50  *
51  * @var string
52  */
53  protected $name = null;
54 
55  /**
56  * Stores various information about the server environment.
57  *
58  * @var array
59  */
60  protected $data = array();
61 
62  /**
63  * The TeamSpeak3_Helper_Profiler_Timer constructor.
64  *
65  * @param string $name
66  * @return TeamSpeak3_Helper_Profiler_Timer
67  */
68  public function __construct($name)
69  {
70  $this->name = (string) $name;
71 
72  $this->data["runtime"] = 0;
73  $this->data["realmem"] = 0;
74  $this->data["emalloc"] = 0;
75 
76  $this->start();
77  }
78 
79  /**
80  * Starts the timer.
81  *
82  * @return void
83  */
84  public function start()
85  {
86  if($this->isRunning()) return;
87 
88  $this->data["realmem_start"] = memory_get_usage(TRUE);
89  $this->data["emalloc_start"] = memory_get_usage();
90 
91  $this->started = microtime(TRUE);
92  $this->running = TRUE;
93  }
94 
95  /**
96  * Stops the timer.
97  *
98  * @return void
99  */
100  public function stop()
101  {
102  if(!$this->isRunning()) return;
103 
104  $this->data["runtime"] += microtime(TRUE) - $this->started;
105  $this->data["realmem"] += memory_get_usage(TRUE) - $this->data["realmem_start"];
106  $this->data["emalloc"] += memory_get_usage() - $this->data["emalloc_start"];
107 
108  $this->started = 0;
109  $this->running = FALSE;
110  }
111 
112  /**
113  * Return the timer runtime.
114  *
115  * @return mixed
116  */
117  public function getRuntime()
118  {
119  if($this->isRunning())
120  {
121  $this->stop();
122  $this->start();
123  }
124 
125  return $this->data["runtime"];
126  }
127 
128  /**
129  * Returns the amount of memory allocated to PHP in bytes.
130  *
131  * @param boolean $realmem
132  * @return integer
133  */
134  public function getMemUsage($realmem = FALSE)
135  {
136  if($this->isRunning())
137  {
138  $this->stop();
139  $this->start();
140  }
141 
142  return ($realmem !== FALSE) ? $this->data["realmem"] : $this->data["emalloc"];
143  }
144 
145  /**
146  * Returns TRUE if the timer is running.
147  *
148  * @return boolean
149  */
150  public function isRunning()
151  {
152  return $this->running;
153  }
154 }