11<?php
2-
3- namespace SimpleProfiler ;
4-
52/**
6- * Class Profiler
7- * @package SimpleProfiler
3+ * This file is part of RedisClient.
4+ * git: https://github.com/cheprasov/php-simple-profiler
5+ *
6+ * (C) Alexander Cheprasov <cheprasov.84@ya.ru>
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
810 */
11+ namespace SimpleProfiler ;
12+
13+ use Console_Table ;
14+
915class Profiler {
1016
11- const VERSION = '0.1.0 ' ;
17+ const VERSION = '1.0.0 ' ;
18+
19+ const GROUP_DELIMITER = '. ' ;
1220
1321 /**
1422 * @var array
@@ -35,6 +43,17 @@ class Profiler {
3543 */
3644 protected static $ workTimers = [];
3745
46+ /**
47+ *
48+ */
49+ public static function clear () {
50+ self ::$ timers = [];
51+ self ::$ timerCounters = [];
52+ self ::$ timerNames = [];
53+ self ::$ counters = [];
54+ self ::$ workTimers = [];
55+ }
56+
3857 /**
3958 * @param string $name
4059 */
@@ -77,25 +96,113 @@ public static function count($name, $incr = 1) {
7796 }
7897
7998 /**
80- * @return string[]
99+ * @return array
81100 */
82- public static function getStat () {
101+ public static function getTimerStat () {
83102 $ result = [];
103+ $ groups = [];
84104 foreach (self ::$ timers as $ name => $ time ) {
85- $ result [] = sprintf ('PROFILER TIMER: %s: %s sec / %s = %s ' ,
86- $ name , $ time , self ::$ timerCounters [$ name ], $ time / self ::$ timerCounters [$ name ]
87- );
105+ $ group = null ;
106+ if (strpos ($ name , self ::GROUP_DELIMITER )) { // point pos > 0
107+ list ($ group , $ shortName ) = explode (self ::GROUP_DELIMITER , $ name , 2 );
108+ if (!isset ($ result [$ group ])) {
109+ $ result [$ group ] = [];
110+ }
111+ $ link = &$ result [$ group ];
112+ $ groups [] = &$ result [$ group ];
113+ } else {
114+ $ shortName = $ name ;
115+ $ link = &$ result ;
116+ }
117+
118+ $ link [$ shortName ] = [
119+ 'group ' => $ group ,
120+ 'name ' => $ shortName ,
121+ 'count ' => self ::$ timerCounters [$ name ],
122+ 'time ' => $ time ,
123+ 'single ' => $ time / self ::$ timerCounters [$ name ],
124+ 'cost ' => null ,
125+ ];
88126 }
127+ if ($ groups ) {
128+ self ::calculateGroupData ($ groups );
129+ }
130+ return $ result ;
131+ }
132+
133+ /**
134+ * @param array $groups
135+ */
136+ protected static function calculateGroupData (array &$ groups ) {
137+ foreach ($ groups as $ groupName => $ items ) {
138+ $ minTime = PHP_INT_MAX ;
139+ foreach ($ items as $ item ) {
140+ $ minTime = min ($ minTime , $ item ['single ' ]);
141+ }
142+ foreach ($ items as $ itemName => $ item ) {
143+ $ groups [$ groupName ][$ itemName ]['cost ' ] = round ($ item ['single ' ] / $ minTime * 100 ) . ' % ' ;
144+ }
145+ }
146+ }
147+
148+ /**
149+ *
150+ */
151+ public static function echoTimerStat () {
152+ $ stats = self ::getTimerStat ();
153+ $ Table = new Console_Table ();
154+ $ Table ->setHeaders (['GROUP ' , 'NAME ' , 'COUNT ' , 'TIME ' , 'SINGLE ' , 'COST ' ]);
155+ $ first = true ;
156+ foreach ($ stats as $ item ) {
157+ if ($ first ) {
158+ $ first = false ;
159+ } else {
160+ $ Table ->addSeparator ();
161+ }
162+ if (isset ($ item ['name ' ])) {
163+ self ::addRowToTable ($ Table , $ item );
164+ } else {
165+ foreach ($ item as $ elem ) {
166+ self ::addRowToTable ($ Table , $ elem );
167+ }
168+ }
169+ }
170+ echo "\n" , $ Table ->getTable ();
171+ }
172+
173+
174+ /**
175+ * @return array
176+ */
177+ public static function getCounterStat () {
178+ $ result = [];
89179 foreach (self ::$ counters as $ name => $ count ) {
90- $ result [] = sprintf ('PROFILER COUNT: %s: %s ' , $ name , $ count );
180+ $ result [] = [
181+ 'name ' => $ name ,
182+ 'count ' => $ count ,
183+ ];
91184 }
92185 return $ result ;
93186 }
94187
95188 /**
96189 *
97190 */
98- public static function echoStat () {
99- echo "\n" . implode ("\n" , self ::getStat ()) ."\n" ;
191+ public static function echoCounterStat () {
192+ $ stats = self ::getCounterStat ();
193+ $ Table = new Console_Table ();
194+ $ Table ->setHeaders (['NAME ' , 'COUNT ' ]);
195+ foreach ($ stats as $ item ) {
196+ self ::addRowToTable ($ Table , $ item );
197+ }
198+ echo "\n" , $ Table ->getTable ();
199+ }
200+
201+ /**
202+ * @param Console_Table $Table
203+ * @param array $item
204+ */
205+ protected static function addRowToTable (Console_Table $ Table , array $ item ) {
206+ $ Table ->addRow ($ item );
100207 }
101208}
0 commit comments