1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10: class QTimer {
11: 12: 13: 14:
15: protected $strName;
16: 17: 18: 19:
20: protected $intCountStarted = 0;
21: 22: 23: 24:
25: protected $fltTimeStart = -1;
26: 27: 28: 29:
30: protected $fltTime = 0;
31:
32: 33: 34: 35: 36: 37:
38: protected function __construct($strName, $blnStart = false) {
39: $this->strName = $strName;
40: if ($blnStart) {
41: $this->startTimer();
42: }
43: }
44:
45: 46: 47: 48:
49: public function StartTimer() {
50: if ($this->fltTimeStart != -1) {
51: throw new QCallerException("Timer was already started");
52: }
53: $this->fltTimeStart = microtime(true);
54: $this->intCountStarted++;
55: return $this;
56: }
57:
58: 59: 60: 61:
62: public function GetTimerTime() {
63: if ($this->fltTimeStart == -1) {
64: return $this->fltTime;
65: }
66: return $this->fltTime + microtime(true) - $this->fltTimeStart;
67: }
68:
69: 70: 71: 72:
73: public function ResetTimer() {
74: $fltTime = $this->StopTimer();
75: $this->fltTime = 0;
76: $this->StartTimer();
77: return $fltTime;
78: }
79:
80: 81: 82: 83:
84: public function StopTimer() {
85: $this->fltTime = $this->GetTimerTime();
86: $this->fltTimeStart = -1;
87: return $this->fltTime;
88: }
89:
90: 91: 92: 93:
94: public function __toString() {
95: return sprintf("%s - start count: %s - execution time: %f",
96: $this->strName,
97: $this->intCountStarted,
98: $this->GetTimerTime());
99: }
100:
101: public function __get($strName) {
102: switch ($strName) {
103: case 'CountStarted':
104: return $this->intCountStarted;
105: case 'TimeStart':
106: return $this->fltTimeStart;
107: default:
108: throw new QCallerException('Invalid property: $strName');
109: }
110: }
111:
112:
113:
114:
115: 116: 117: 118:
119: protected static $objTimerArray = array();
120:
121: 122: 123: 124: 125:
126: public static function Start($strName = 'default') {
127: $objTimer = static::GetTimerInstance($strName);
128: return $objTimer->StartTimer();
129: }
130:
131: 132: 133: 134: 135:
136: public static function GetTime($strName = 'default') {
137: $objTimer = static::GetTimerInstance($strName, false);
138: if ($objTimer) {
139: return $objTimer->GetTimerTime();
140: } else {
141: throw new QCallerException('Timer with name ' . $strName . ' was not started, cannot get its value');
142: }
143: }
144:
145: 146: 147: 148: 149:
150: public static function Stop($strName = 'default') {
151: $objTimer = static::GetTimerInstance($strName, false);
152: if ($objTimer) {
153: return $objTimer->StopTimer();
154: } else {
155: throw new QCallerException('Timer with name ' . $strName . ' was not started, cannot stop it');
156: }
157: }
158:
159: 160: 161: 162: 163:
164: public static function Reset($strName = 'default') {
165: $objTimer = static::GetTimerInstance($strName, false);
166: if ($objTimer) {
167: return $objTimer->ResetTimer();
168: }
169: return null;
170: }
171:
172: 173: 174: 175: 176:
177: public static function GetTimer($strName = 'default') {
178: $objTimer = static::GetTimerInstance($strName, false);
179: if ($objTimer) {
180: return $objTimer;
181: }
182:
183: return null;
184: }
185:
186: protected static function GetTimerInstance($strName, $blnCreateNew = true) {
187: if (!isset(static::$objTimerArray[$strName])) {
188: if ($blnCreateNew) {
189: static::$objTimerArray[$strName] = new QTimer($strName);
190: } else {
191: return null;
192: }
193: }
194: return static::$objTimerArray[$strName];
195: }
196:
197:
198:
199: 200: 201: 202: 203:
204: public static function VarDump($blnDisplayOutput = true) {
205: $strToReturn = '';
206: foreach (static::$objTimerArray as $objTimer) {
207: $strToReturn .= $objTimer->__toString() . "\n";
208: }
209: if ($blnDisplayOutput) {
210: echo nl2br($strToReturn);
211: } else {
212: return $strToReturn;
213: }
214: }
215: }
216: