Skip to content

Commit 15f33bf

Browse files
authored
V1.1.0 (#2)
* v1.1.0
1 parent da12926 commit 15f33bf

File tree

3 files changed

+69
-22
lines changed

3 files changed

+69
-22
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cheprasov/php-simple-profiler",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Simple profiler for PHP",
55
"homepage": "https://github.com/cheprasov/php-simple-profiler",
66
"minimum-stability": "stable",

src/SimpleProfiler/Profiler.php

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class 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
}

tests/Unit/ProfilerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ public function testCommonGroups2() {
9696
$this->checkTimer('one', 10, '0.15', '0.015', $result['bar']['one']);
9797
}
9898

99+
public function testTableCommonGroups() {
100+
for ($i = 0; $i < 10; ++$i) {
101+
Profiler::start('foo.one');
102+
usleep(10000);
103+
Profiler::stop();
104+
Profiler::start('bar.two');
105+
usleep(15000);
106+
Profiler::stop();
107+
}
108+
109+
$this->assertSame(
110+
"+-------+-------+------+\n" .
111+
"| cost | count | name |\n" .
112+
"+-------+-------+------+\n" .
113+
"| 100 % | 10 | one |\n" .
114+
"+-------+-------+------+\n" .
115+
"| 100 % | 10 | two |\n" .
116+
"+-------+-------+------+",
117+
Profiler::getTimerTableStat(['cost', 'count', 'name'])
118+
);
119+
}
120+
99121
public function testCounter1() {
100122
Profiler::count('foo');
101123
Profiler::count('bar');

0 commit comments

Comments
 (0)