| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- /**
- * @file
- * TeamSpeak 3 PHP Framework
- *
- * $Id: Timer.php 06/06/2016 22:27:13 scp@Svens-iMac $
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * @package TeamSpeak3
- * @version 1.1.24
- * @author Sven 'ScP' Paulsen
- * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
- */
- /**
- * @class TeamSpeak3_Helper_Profiler_Timer
- * @brief Helper class providing profiler timers.
- */
- class TeamSpeak3_Helper_Profiler_Timer
- {
- /**
- * Indicates wether the timer is running or not.
- *
- * @var boolean
- */
- protected $running = FALSE;
- /**
- * Stores the timestamp when the timer was last started.
- *
- * @var integer
- */
- protected $started = 0;
- /**
- * Stores the timer name.
- *
- * @var string
- */
- protected $name = null;
- /**
- * Stores various information about the server environment.
- *
- * @var array
- */
- protected $data = array();
- /**
- * The TeamSpeak3_Helper_Profiler_Timer constructor.
- *
- * @param string $name
- * @return TeamSpeak3_Helper_Profiler_Timer
- */
- public function __construct($name)
- {
- $this->name = (string) $name;
- $this->data["runtime"] = 0;
- $this->data["realmem"] = 0;
- $this->data["emalloc"] = 0;
- $this->start();
- }
- /**
- * Starts the timer.
- *
- * @return void
- */
- public function start()
- {
- if($this->isRunning()) return;
- $this->data["realmem_start"] = memory_get_usage(TRUE);
- $this->data["emalloc_start"] = memory_get_usage();
- $this->started = microtime(TRUE);
- $this->running = TRUE;
- }
- /**
- * Stops the timer.
- *
- * @return void
- */
- public function stop()
- {
- if(!$this->isRunning()) return;
- $this->data["runtime"] += microtime(TRUE) - $this->started;
- $this->data["realmem"] += memory_get_usage(TRUE) - $this->data["realmem_start"];
- $this->data["emalloc"] += memory_get_usage() - $this->data["emalloc_start"];
- $this->started = 0;
- $this->running = FALSE;
- }
- /**
- * Return the timer runtime.
- *
- * @return mixed
- */
- public function getRuntime()
- {
- if($this->isRunning())
- {
- $this->stop();
- $this->start();
- }
- return $this->data["runtime"];
- }
- /**
- * Returns the amount of memory allocated to PHP in bytes.
- *
- * @param boolean $realmem
- * @return integer
- */
- public function getMemUsage($realmem = FALSE)
- {
- if($this->isRunning())
- {
- $this->stop();
- $this->start();
- }
- return ($realmem !== FALSE) ? $this->data["realmem"] : $this->data["emalloc"];
- }
- /**
- * Returns TRUE if the timer is running.
- *
- * @return boolean
- */
- public function isRunning()
- {
- return $this->running;
- }
- }
|