1414
1515class Profiler {
1616
17- const VERSION = '1.0 .0 ' ;
17+ const VERSION = '1.1 .0 ' ;
1818
1919 const GROUP_DELIMITER = '. ' ;
2020
@@ -59,7 +59,8 @@ public static function clear() {
5959 */
6060 public static function start ($ name ) {
6161 self ::$ timerNames [] = $ name ;
62- self ::$ workTimers [$ name ] = microtime (true );
62+ $ link = &self ::$ workTimers [$ name ];
63+ $ link = microtime (true );
6364 }
6465
6566 /**
@@ -77,21 +78,21 @@ public static function stop($name = null) {
7778 self ::$ timerCounters [$ name ] = 1 ;
7879 self ::$ timers [$ name ] = $ time - self ::$ workTimers [$ name ];
7980 } else {
80- self ::$ timerCounters [$ name ]++ ;
81+ ++ self ::$ timerCounters [$ name ];
8182 self ::$ timers [$ name ] += $ time - self ::$ workTimers [$ name ];
8283 }
8384 unset(self ::$ workTimers [$ name ]);
8485 }
8586
8687 /**
8788 * @param string $name
88- * @param int $incr
89+ * @param int $count
8990 */
90- public static function count ($ name , $ incr = 1 ) {
91+ public static function count ($ name , $ count = 1 ) {
9192 if (isset (self ::$ counters [$ name ])) {
92- self ::$ counters [$ name ] += $ incr ;
93+ self ::$ counters [$ name ] += $ count ;
9394 } else {
94- self ::$ counters [$ name ] = $ incr ;
95+ self ::$ counters [$ name ] = $ count ;
9596 }
9697 }
9798
@@ -103,7 +104,7 @@ public static function getTimerStat() {
103104 $ groups = [];
104105 foreach (self ::$ timers as $ name => $ time ) {
105106 $ group = null ;
106- if (strpos ($ name , self ::GROUP_DELIMITER )) { // point pos > 0
107+ if (strpos ($ name , self ::GROUP_DELIMITER )) { // pos > 0
107108 list ($ group , $ shortName ) = explode (self ::GROUP_DELIMITER , $ name , 2 );
108109 if (!isset ($ result [$ group ])) {
109110 $ result [$ group ] = [];
@@ -146,12 +147,16 @@ protected static function calculateGroupData(array &$groups) {
146147 }
147148
148149 /**
149- *
150+ * @param string[] $fields
151+ * @return string
150152 */
151- public static function echoTimerStat () {
153+ public static function getTimerTableStat ($ fields = []) {
154+ if (!$ fields ) {
155+ $ fields = ['group ' , 'name ' , 'count ' , 'time ' , 'single ' , 'cost ' ];
156+ }
152157 $ stats = self ::getTimerStat ();
153158 $ Table = new Console_Table ();
154- $ Table ->setHeaders ([ ' GROUP ' , ' NAME ' , ' COUNT ' , ' TIME ' , ' SINGLE ' , ' COST ' ] );
159+ $ Table ->setHeaders ($ fields );
155160 $ first = true ;
156161 foreach ($ stats as $ item ) {
157162 if ($ first ) {
@@ -160,16 +165,22 @@ public static function echoTimerStat() {
160165 $ Table ->addSeparator ();
161166 }
162167 if (isset ($ item ['name ' ])) {
163- self ::addRowToTable ($ Table , $ item );
168+ self ::addRowToTable ($ Table , $ item, $ fields );
164169 } else {
165170 foreach ($ item as $ elem ) {
166- self ::addRowToTable ($ Table , $ elem );
171+ self ::addRowToTable ($ Table , $ elem, $ fields );
167172 }
168173 }
169174 }
170- echo "\n" , $ Table ->getTable ();
175+ return trim ( $ Table ->getTable () );
171176 }
172177
178+ /**
179+ * @param array $fields
180+ */
181+ public static function echoTimerStat ($ fields = []) {
182+ echo "\n" , self ::getTimerTableStat ($ fields ), "\n" ;
183+ }
173184
174185 /**
175186 * @return array
@@ -186,23 +197,37 @@ public static function getCounterStat() {
186197 }
187198
188199 /**
189- *
200+ * @return string
190201 */
191- public static function echoCounterStat () {
202+ public static function getCounterTableStat () {
192203 $ stats = self ::getCounterStat ();
193204 $ Table = new Console_Table ();
194- $ Table ->setHeaders (['NAME ' , 'COUNT ' ]);
205+ $ Table ->setHeaders (['name ' , 'count ' ]);
195206 foreach ($ stats as $ item ) {
196- self ::addRowToTable ($ Table , $ item );
207+ self ::addRowToTable ($ Table , $ item, [ ' name ' , ' count ' ] );
197208 }
198- echo "\n" , $ Table ->getTable ();
209+ return trim ($ Table ->getTable ());
210+ }
211+
212+ /**
213+ *
214+ */
215+ public static function echoCounterStat () {
216+ echo "\n" , self ::getCounterTableStat (), "\n" ;
199217 }
200218
201219 /**
202220 * @param Console_Table $Table
203221 * @param array $item
222+ * @param string[] $fields
204223 */
205- protected static function addRowToTable (Console_Table $ Table , array $ item ) {
206- $ Table ->addRow ($ item );
224+ protected static function addRowToTable (Console_Table $ Table , array $ item , array $ fields ) {
225+ $ row = [];
226+ foreach ($ fields as $ field ) {
227+ if (array_key_exists ($ field , $ item )) {
228+ $ row [] = $ item [$ field ];
229+ }
230+ }
231+ $ Table ->addRow ($ row );
207232 }
208233}
0 commit comments