-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathRuntimeProfileTrait.php
135 lines (115 loc) · 3.26 KB
/
RuntimeProfileTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php declare(strict_types=1);
/**
* The file is part of inhere/console
*
* @author https://github.com/inhere
* @homepage https://github.com/inhere/php-console
* @license https://github.com/inhere/php-console/blob/master/LICENSE
*/
namespace Inhere\Console\Decorate;
use InvalidArgumentException;
use Toolkit\Stdlib\Helper\PhpHelper;
use function array_pop;
use function explode;
use function in_array;
use function memory_get_usage;
use function microtime;
/**
* Trait RuntimeProfileTrait
*
* @package Inhere\Console\Decorate
*/
trait RuntimeProfileTrait
{
/**
* profile data
*
* @var array
*/
private static array $profiles = [];
/**
* @var array
* [
* profileKey0,
* profileKey1,
* profileKey2,
* ...
* ]
*/
private static array $keyQueue = [];
/**
* mark data analysis start
*
* @param $name
* @param array $context
* @param string $category
*
* @throws InvalidArgumentException
*/
public static function profile($name, array $context = [], string $category = 'application'): void
{
$data = [
'_profile_stats' => [
'startTime' => microtime(true),
'startMem' => memory_get_usage(),
],
'_profile_start' => $context,
'_profile_end' => null,
'_profile_msg' => null,
];
$profileKey = $category . '|' . $name;
if (in_array($profileKey, self::$keyQueue, true)) {
throw new InvalidArgumentException("Your added profile name [$name] have been exists!");
}
self::$keyQueue[] = $profileKey;
self::$profiles[$category][$name] = $data;
}
/**
* mark data analysis end
*
* @param string|null $msg
* @param array $context
*
* @return bool|array
*/
public static function profileEnd(string $msg = null, array $context = []): bool|array
{
if (!$latestKey = array_pop(self::$keyQueue)) {
return false;
}
[$category, $name] = explode('|', $latestKey);
if (isset(self::$profiles[$category][$name])) {
$data = self::$profiles[$category][$name];
$old = $data['_profile_stats'];
$data['_profile_stats'] = PhpHelper::runtime($old['startTime'], $old['startMem']);
$data['_profile_end'] = $context;
$data['_profile_msg'] = $msg;
// $title = $category . ' - ' . ($title ?: $name);
self::$profiles[$category][$name] = $data;
// self::$log(Logger::DEBUG, $title, $data);
return $data;
}
return false;
}
/**
* @param null|string $name
* @param string $category
*
* @return array
*/
public static function getProfileData(string $name = null, string $category = 'application'): array
{
if ($name) {
return self::$profiles[$category][$name] ?? [];
}
if ($category) {
return self::$profiles[$category] ?? [];
}
return self::$profiles;
}
public function clearProfileData(): void
{
self::$profiles = [];
self::$keyQueue = [];
}
}