Skip to content

Commit 5df4da0

Browse files
committed
Profiler v3
1 parent 0625beb commit 5df4da0

File tree

12 files changed

+1416
-925
lines changed

12 files changed

+1416
-925
lines changed

README.md

Lines changed: 1117 additions & 676 deletions
Large diffs are not rendered by default.

src/Counter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static function clear()
2626
* @param string $name
2727
* @param int $count
2828
*/
29-
public static function increment($name, int $count = 1): int
29+
public static function increment(string $name, int $count = 1): int
3030
{
3131
if (isset(self::$counter[$name])) {
3232
self::$counter[$name] += $count;
@@ -40,7 +40,7 @@ public static function increment($name, int $count = 1): int
4040
* @param string|null $name
4141
* @param int $count
4242
*/
43-
public static function decrement($name, int $count = 1): int
43+
public static function decrement(string $name, int $count = 1): int
4444
{
4545
return self::increment($name, -$count);
4646
}

src/Profiler.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Profiler {
2525
*/
2626
const REGEXP_PHP_VAR = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/';
2727

28-
protected static $defaultUnitVarName = '$ProfilerUnit';
28+
protected static $profilerUnitVarName = '$ProfilerUnit';
2929

3030
protected static $profilerUnitClass = FunctionUnit::class;
3131

@@ -40,15 +40,18 @@ class Profiler {
4040
protected static $lastElement;
4141

4242
/**
43-
* @param string $defaultUnitVarName
43+
* @param string $profilerUnitVarName
4444
* @returns bool
4545
*/
46-
public static function setDefaultUnitVarName(string $defaultUnitVarName): bool
46+
public static function setProfilerUnitVarName(string $profilerUnitVarName): bool
4747
{
48-
if (!preg_match(self::REGEXP_PHP_VAR, ltrim($defaultUnitVarName, '$'))) {
48+
if (!$profilerUnitVarName || $profilerUnitVarName[0] !== '$') {
4949
return false;
5050
}
51-
self::$defaultUnitVarName = $defaultUnitVarName;
51+
if (!preg_match(self::REGEXP_PHP_VAR, substr($profilerUnitVarName, 1))) {
52+
return false;
53+
}
54+
self::$profilerUnitVarName = $profilerUnitVarName;
5255
return true;
5356
}
5457

@@ -68,12 +71,11 @@ public static function setProfilerUnitClass(string $profilerUnitClass)
6871
/**
6972
* @return array
7073
*/
71-
protected function &getLastElement()
74+
protected static function &getLastElement()
7275
{
7376
if (!self::$callsTree) {
7477
self::$callsTree = [
7578
'parent' => null,
76-
'data' => null,
7779
'timeBeg' => microtime(true),
7880
'duration' => 0,
7981
'items' => [],
@@ -102,7 +104,6 @@ public static function addUnit(UnitInterface $Unit)
102104
'name' => $Unit->getName(),
103105
'count' => 1,
104106
'duration' => 0,
105-
'data' => null,
106107
'items' => [],
107108
];
108109
}
@@ -158,9 +159,13 @@ public static function getLog(): string
158159
return $output;
159160
}
160161

162+
/**
163+
* @param mixed $data
164+
* @return string
165+
*/
161166
protected static function formatData($data)
162167
{
163-
return json_encode($data);
168+
return json_encode($data, JSON_UNESCAPED_UNICODE);
164169
}
165170

166171
/**
@@ -169,7 +174,7 @@ protected static function formatData($data)
169174
* @param int $totalDuration
170175
* @return string
171176
*/
172-
protected static function formatElement($element, $level = 0, $totalDuration = 0)
177+
protected static function formatElement($element, $level = 0, $totalDuration = 0, &$num = 0)
173178
{
174179
$output = '';
175180
$hasItems = !empty($element['items']);
@@ -180,14 +185,17 @@ protected static function formatElement($element, $level = 0, $totalDuration = 0
180185
$total = sprintf('%02.6f', $element['duration']);
181186
$output .= "Profiler, total: {$total} sec \n";
182187
} else {
188+
$num++;
189+
$sp = str_repeat(' ', strlen($num));
190+
183191
$output .= str_repeat($spaces, $level) . PHP_EOL;
184192
$output .= str_repeat($spaces, $level - 1);
185-
$output .= '> ' . $element['name'] . PHP_EOL;
193+
$output .= $num . ' | ' . $element['name'] . PHP_EOL;
186194

187195
if (!empty($element['data'])) {
188196
$data = self::formatData($element['data']);
189197
$output .= str_repeat($spaces, $level - 1);
190-
$output .= " | data: {$data}\n";
198+
$output .= "{$sp} | data: {$data}\n";
191199
}
192200

193201
$output .= str_repeat($spaces, $level - 1);
@@ -196,11 +204,11 @@ protected static function formatElement($element, $level = 0, $totalDuration = 0
196204
$total = sprintf('%02.6f', $element['duration']);
197205
$cost = sprintf('%02.1f', $element['duration'] / ($totalDuration ?: 1) * 100);
198206

199-
$output .= " | cost: {$cost} %, count: {$count}, avg: {$avg} sec, total: {$total} sec\n";
207+
$output .= "{$sp} | cost: {$cost} %, count: {$count}, avg: {$avg} sec, total: {$total} sec\n";
200208
}
201209
if ($hasItems) {
202210
foreach ($element['items'] as $el) {
203-
$output .= self::formatElement($el, $level + 1, $element['duration']);
211+
$output .= self::formatElement($el, $level + 1, $element['duration'], $num);
204212
}
205213
}
206214
return $output;
@@ -210,9 +218,9 @@ protected static function formatElement($element, $level = 0, $totalDuration = 0
210218
* @param string $filename
211219
* @param bool $withArguments
212220
* @param bool $withResult
213-
* @param string $regExpFilter
221+
* @param string|null $regExpFilter
214222
*/
215-
public static function includeFile(string $filename, string $regExpFilter = '')
223+
public static function includeFile(string $filename, string $regExpFilter = null)
216224
{
217225
$file = file_get_contents($filename);
218226
$file = self::injectProfilerUnitToCode($file, self::$profilerUnitClass, $regExpFilter);
@@ -235,12 +243,12 @@ public static function includeFile(string $filename, string $regExpFilter = '')
235243
* @param string $regExpFilter
236244
* @return string
237245
*/
238-
protected static function injectProfilerUnitToCode(string $source, string $unitClassName, string $regExpFilter = '')
246+
protected static function injectProfilerUnitToCode(string $source, string $unitClassName, string $regExpFilter = null)
239247
{
240248
$tokens = token_get_all($source);
241249
unset($source);
242250

243-
$defaultUnitVarName = self::$defaultUnitVarName;
251+
$unitVarName = self::$profilerUnitVarName;
244252
$withArguments = is_subclass_of($unitClassName, CollectArgumentsInterface::class);
245253
$withResult = is_subclass_of($unitClassName, CollectResultInterface::class);
246254
$unitClass = '\\' . ltrim($unitClassName, '\\');
@@ -294,7 +302,7 @@ protected static function injectProfilerUnitToCode(string $source, string $unitC
294302

295303
if ($withResult && $functionFound && ($id === T_RETURN || $id === T_THROW) && $functionName) {
296304
if (!$regExpFilter || preg_match($regExpFilter, $functionName)) {
297-
$code .= " {$defaultUnitVarName}->result =";
305+
$code .= " {$unitVarName}->result =";
298306
}
299307
continue;
300308
}
@@ -303,9 +311,9 @@ protected static function injectProfilerUnitToCode(string $source, string $unitC
303311
if ($text === '{') {
304312
if (!$stack) {
305313
if (!$regExpFilter || preg_match($regExpFilter, $functionName)) {
306-
$code .= "{$defaultUnitVarName} = {$unitClass}::create(__METHOD__, {$functionLine}, {$functionColumn});";
314+
$code .= "{$unitVarName} = {$unitClass}::create(__METHOD__, {$functionLine}, {$functionColumn});";
307315
if ($withArguments) {
308-
$code .= "{$defaultUnitVarName}->setArguments(func_get_args());";
316+
$code .= "{$unitVarName}->setArguments(func_get_args());";
309317
}
310318
}
311319
}

src/Unit/DetailedFunctionUnit.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,24 @@ class DetailedFunctionUnit extends FunctionUnit implements CollectArgumentsInter
2828
protected $resultIsDefined = false;
2929

3030
/**
31-
* @param string $methodName
32-
* @param array $args
31+
* @return null
3332
*/
34-
protected function __construct(string $methodName)
35-
{
36-
if (isset($args)) {
37-
$this->arguments = array_map(['DetailedFunctionUnit', 'prepareValue'], $args);
38-
}
39-
parent::__construct($methodName);
40-
}
41-
4233
public function getKey()
4334
{
4435
return null;
4536
}
4637

38+
/**
39+
* @inheritdoc
40+
*/
4741
public function setArguments(array $arguments)
4842
{
4943
$this->arguments = array_map(['self', 'prepareValue'], $arguments);
5044
}
5145

46+
/**
47+
* @return array|null
48+
*/
5249
public function getData()
5350
{
5451
$data = [];
@@ -71,10 +68,14 @@ public function setResult($value)
7168
$this->resultIsDefined = true;
7269
}
7370

71+
/**
72+
* @param string $name
73+
* @param mixed $value
74+
*/
7475
public function __set($name, $value)
7576
{
7677
if ($name === 'result') {
77-
return $this->setResult($value);
78+
$this->setResult($value);
7879
}
7980
}
8081
}

src/Unit/FunctionUnit.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,42 @@ class FunctionUnit implements UnitInterface
2525
protected function __construct(string $methodName)
2626
{
2727
$this->methodName = $methodName;
28-
Profiler::addUnit($this);
2928
}
3029

30+
/**
31+
* @inheritdoc
32+
*/
3133
public static function create(string $methodName, int $line, int $column): UnitInterface
3234
{
33-
return new static("{$methodName} {$line}:{$column}");
35+
$Unit = new static("{$methodName} {$line}:{$column}");
36+
Profiler::addUnit($Unit);
37+
return $Unit;
3438
}
3539

3640
public function __destruct()
3741
{
3842
Profiler::closeUnit($this);
3943
}
4044

45+
/**
46+
* @inheritdoc
47+
*/
4148
public function getName(): string
4249
{
4350
return $this->methodName;
4451
}
4552

53+
/**
54+
* @inheritdoc
55+
*/
4656
public function getKey()
4757
{
4858
return $this->methodName;
4959
}
5060

61+
/**
62+
* @return null
63+
*/
5164
public function getData()
5265
{
5366
return null;

src/Unit/UnitInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function getName(): string;
3131
public function getKey();
3232

3333
/**
34-
* @return mixed
34+
* @return mixed|null
3535
*/
3636
public function getData();
3737
}

tests/Profiler.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)