Timer.php 3.2 KB

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