PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/magmi/inc/timecounter.php

https://gitlab.com/myurd/magmi-git
PHP | 251 lines | 132 code | 16 blank | 103 comment | 28 complexity | c67ba7d5cb50d1b1db40d58bff81c69e MD5 | raw file
  1. <?php
  2. /**
  3. * Time counter class
  4. * This class provides a way to measure :
  5. *
  6. * - time
  7. * - counters
  8. *
  9. * store results into many categories for many timing aspects
  10. *
  11. * It is based on 2 levels :
  12. * - sources : labels under which timing categories will be stored, it is a label, most of the time, a method name
  13. * - categories : for each source, you can store many infos like "inDB","processing" and so on....
  14. *
  15. * time measure can be divided into phases, so you can measure different subparts of your processing like "initialization","lookup" aso....
  16. * many counters can also be defined at the same category leve
  17. * At the end, the timecounter will give this kind of results
  18. *
  19. * + cat1
  20. * +timers
  21. * + phase1 => time for phase 1 of cat1 of source 1
  22. * + phase2 => time for phase 2 of cat1 of source 1
  23. * +counters
  24. * + counter1 => value of counter 1 of cat 1 of source 1
  25. * + cat2
  26. *
  27. * Sources, categories & phases could be added dynamically, each time a new phase/counter/category or source is
  28. * declared trough calls to addCounter, initTime,exitTime that does not exist yet, a container is created for it.
  29. *
  30. * sources are tags. categories are more like containers
  31. */
  32. class timecounter
  33. {
  34. protected $_timingcats = array();
  35. protected $_defaultsrc = "";
  36. protected $_timingcontext = array();
  37. /**
  38. * Constructor
  39. *
  40. * @param string $defaultsrc
  41. * : default timing source
  42. */
  43. public function __construct($defaultsrc = "*")
  44. {
  45. $this->_defaultsrc = $defaultsrc;
  46. }
  47. /**
  48. * Initializes default timing categories to use
  49. *
  50. * @param unknown $tcats
  51. * array of timing categories
  52. */
  53. public function initTimingCats($tcats)
  54. {
  55. foreach ($tcats as $tcat) {
  56. $this->_timingcats[$tcat] = array("_counters"=>array(),"_timers"=>array());
  57. }
  58. }
  59. /**
  60. * returns the content for a given timing category name
  61. *
  62. * @param string $cat
  63. * : timing category name
  64. * @return array informations for given category
  65. */
  66. public function getTimingCategory($cat)
  67. {
  68. return $this->_timingcats[$cat];
  69. }
  70. /**
  71. * return all timers
  72. *
  73. * @return all timers info by category
  74. */
  75. public function getTimers()
  76. {
  77. $timers = array();
  78. foreach ($this->_timingcats as $cname => $info) {
  79. $timers[$cname] = $info['_timers'];
  80. }
  81. return $timers;
  82. }
  83. /**
  84. * return all counters
  85. *
  86. * @return all counters info by category
  87. */
  88. public function getCounters()
  89. {
  90. $counters = array();
  91. foreach ($this->_timingcats as $cname => $info) {
  92. $counters[$cname] = $info['_counters'];
  93. }
  94. return $counters;
  95. }
  96. /**
  97. * creates a new counter
  98. *
  99. * @param string $cname
  100. * : counter name
  101. * @param string $tcats
  102. * : array of category names to add counter to, if null => all categories
  103. */
  104. public function addCounter($cname, $tcats = null)
  105. {
  106. if ($tcats == null) {
  107. $tcats = array_keys($this->_timingcats);
  108. }
  109. foreach ($tcats as $tcat) {
  110. if (!isset($this->_timingcats[$tcat])) {
  111. $this->_timingcats[$tcat] = array("_counters"=>array(),"_timers"=>array());
  112. }
  113. $this->_timingcats[$tcat]["_counters"][$cname] = 0;
  114. }
  115. }
  116. /**
  117. * initializes a new counter
  118. *
  119. * @param string $cname
  120. * : counter name
  121. * @param string $tcats
  122. * : array of category names to initialize counter for, if null => all categories
  123. */
  124. public function initCounter($cname, $tcats = null)
  125. {
  126. if ($tcats == null) {
  127. $tcats = array_keys($this->_timingcats);
  128. }
  129. foreach ($tcats as $tcat) {
  130. $this->_timingcats[$tcat]["_counters"][$cname] = 0;
  131. }
  132. }
  133. /**
  134. * increments a counter
  135. *
  136. * @param string $cname
  137. * : counter name
  138. * @param string $tcats
  139. * : array of category names to initialize counter for, if null => all categories
  140. */
  141. public function incCounter($cname, $tcats = null)
  142. {
  143. if ($tcats == null) {
  144. $tcats = array_keys($this->_timingcats);
  145. }
  146. foreach ($tcats as $tcat) {
  147. if (!isset($this->_timingcats[$tcat]["_counters"][$cname])) {
  148. $this->_timingcats[$tcat]["_counters"][$cname] = 0;
  149. }
  150. $this->_timingcats[$tcat]["_counters"][$cname]++;
  151. }
  152. }
  153. /**
  154. * Initializes a timer
  155. *
  156. * @param string $phase
  157. * : timer phase to initialize
  158. * @param string $src
  159. * : source tag
  160. * @param string $tcat
  161. * : timing category to initialize timer for
  162. */
  163. public function initTime($phase = "global", $src = null, $tcat = null)
  164. {
  165. if (isset($src)) {
  166. array_push($this->_timingcontext, $src);
  167. $this->_timingcontext = array_values(array_unique($this->_timingcontext));
  168. }
  169. if (count($this->_timingcontext) == 0) {
  170. return;
  171. }
  172. if (!isset($tcat)) {
  173. $tcats = $this->_timingcats;
  174. } else {
  175. $tcats = array($tcat=>$this->_timingcats[$tcat]);
  176. }
  177. $t = microtime(true);
  178. foreach ($tcats as $tcat => $dummy) {
  179. if (!isset($this->_timingcats[$tcat]["_timers"][$phase])) {
  180. $this->_timingcats[$tcat]["_timers"][$phase] = array();
  181. }
  182. $ctxc = count($this->_timingcontext);
  183. for ($i = 0; $i < $ctxc; $i++) {
  184. $src = $this->_timingcontext[$i];
  185. if (!isset($this->_timingcats[$tcat]["_timers"][$phase][$src])) {
  186. $this->_timingcats[$tcat]["_timers"][$phase][$src] = array("init"=>$t,"dur"=>0);
  187. }
  188. $this->_timingcats[$tcat]["_timers"][$phase][$src]["start"] = $t;
  189. }
  190. }
  191. }
  192. /**
  193. * closes a timer for a given phase on a given category for a given source
  194. *
  195. * @param string $phase
  196. * : time phase to exit
  197. * @param string $src
  198. * : source tag
  199. * @param string $tcat
  200. * : timing category
  201. */
  202. public function exitTime($phase, $src = null, $tcat = null)
  203. {
  204. $targets = $this->_timingcontext;
  205. if (count($targets) == 0) {
  206. return;
  207. }
  208. if (isset($src) && in_array($src, $this->_timingcontext)) {
  209. $targets = array($src);
  210. }
  211. $ctargets = count($targets);
  212. if ($ctargets == 0) {
  213. return;
  214. }
  215. if ($tcat == null) {
  216. $tcats = $this->_timingcats;
  217. } else {
  218. $tcats = array($tcat=>$this->_timingcats[$tcat]);
  219. }
  220. $end = microtime(true);
  221. foreach ($tcats as $tcat => $phasetimes) {
  222. for ($i = 0; $i < $ctargets; $i++) {
  223. $src = $targets[$i];
  224. if (isset($this->_timingcats[$tcat]["_timers"][$phase][$src])) {
  225. $this->_timingcats[$tcat]["_timers"][$phase][$src]["end"] = $end;
  226. $this->_timingcats[$tcat]["_timers"][$phase][$src]["dur"] += $end -
  227. $this->_timingcats[$tcat]["_timers"][$phase][$src]["start"];
  228. } else {
  229. echo "Invalid timing source : $src";
  230. }
  231. }
  232. }
  233. $this->_timingcontext = array_diff($this->_timingcontext, $targets);
  234. }
  235. }