Timer.php 3.1 KB

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