TeamSpeak 3 PHP Framework
1.1.12
|
00001 <?php 00002 00003 /** 00004 * @file 00005 * TeamSpeak 3 PHP Framework 00006 * 00007 * $Id: Timer.php 2/18/2012 12:42:46 scp@orilla $ 00008 * 00009 * This program is free software: you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation, either version 3 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * @package TeamSpeak3 00023 * @version 1.1.12 00024 * @author Sven 'ScP' Paulsen 00025 * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved. 00026 */ 00027 00028 /** 00029 * @class TeamSpeak3_Helper_Profiler_Timer 00030 * @brief Helper class providing profiler timers. 00031 */ 00032 class TeamSpeak3_Helper_Profiler_Timer 00033 { 00034 /** 00035 * Indicates wether the timer is running or not. 00036 * 00037 * @var boolean 00038 */ 00039 protected $running = FALSE; 00040 00041 /** 00042 * Stores the timestamp when the timer was last started. 00043 * 00044 * @var integer 00045 */ 00046 protected $started = 0; 00047 00048 /** 00049 * Stores the timer name. 00050 * 00051 * @var string 00052 */ 00053 protected $name = null; 00054 00055 /** 00056 * Stores various information about the server environment. 00057 * 00058 * @var array 00059 */ 00060 protected $data = array(); 00061 00062 /** 00063 * The TeamSpeak3_Helper_Profiler_Timer constructor. 00064 * 00065 * @param string $name 00066 * @return TeamSpeak3_Helper_Profiler_Timer 00067 */ 00068 public function __construct($name) 00069 { 00070 $this->name = (string) $name; 00071 00072 $this->data["runtime"] = 0; 00073 $this->data["realmem"] = 0; 00074 $this->data["emalloc"] = 0; 00075 00076 $this->start(); 00077 } 00078 00079 /** 00080 * Starts the timer. 00081 * 00082 * @return void 00083 */ 00084 public function start() 00085 { 00086 if($this->isRunning()) return; 00087 00088 $this->data["realmem_start"] = memory_get_usage(TRUE); 00089 $this->data["emalloc_start"] = memory_get_usage(); 00090 00091 $this->started = microtime(TRUE); 00092 $this->running = TRUE; 00093 } 00094 00095 /** 00096 * Stops the timer. 00097 * 00098 * @return void 00099 */ 00100 public function stop() 00101 { 00102 if(!$this->isRunning()) return; 00103 00104 $this->data["runtime"] += microtime(TRUE) - $this->started; 00105 $this->data["realmem"] += memory_get_usage(TRUE) - $this->data["realmem_start"]; 00106 $this->data["emalloc"] += memory_get_usage() - $this->data["emalloc_start"]; 00107 00108 $this->started = 0; 00109 $this->running = FALSE; 00110 } 00111 00112 /** 00113 * Return the timer runtime. 00114 * 00115 * @return mixed 00116 */ 00117 public function getRuntime() 00118 { 00119 if($this->isRunning()) 00120 { 00121 $this->stop(); 00122 $this->start(); 00123 } 00124 00125 return $this->data["runtime"]; 00126 } 00127 00128 /** 00129 * Returns the amount of memory allocated to PHP in bytes. 00130 * 00131 * @param boolean $realmem 00132 * @return integer 00133 */ 00134 public function getMemUsage($realmem = FALSE) 00135 { 00136 if($this->isRunning()) 00137 { 00138 $this->stop(); 00139 $this->start(); 00140 } 00141 00142 return ($realmem !== FALSE) ? $this->data["realmem"] : $this->data["emalloc"]; 00143 } 00144 00145 /** 00146 * Returns TRUE if the timer is running. 00147 * 00148 * @return boolean 00149 */ 00150 public function isRunning() 00151 { 00152 return $this->running; 00153 } 00154 }