|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Resource\SemanticLog\Profile; |
|
6
|
|
|
|
|
7
|
|
|
use JsonSerializable; |
|
8
|
|
|
use Override; |
|
9
|
|
|
|
|
10
|
|
|
use function function_exists; |
|
11
|
|
|
use function xhprof_disable; |
|
12
|
|
|
use function xhprof_enable; |
|
13
|
|
|
|
|
14
|
|
|
use const XHPROF_FLAGS_CPU; |
|
15
|
|
|
use const XHPROF_FLAGS_MEMORY; |
|
16
|
|
|
use const XHPROF_FLAGS_NO_BUILTINS; |
|
17
|
|
|
|
|
18
|
|
|
final class XHProfResult implements JsonSerializable |
|
19
|
|
|
{ |
|
20
|
|
|
/** @param array<string, mixed>|null $data */ |
|
21
|
|
|
public function __construct( |
|
22
|
|
|
public readonly ?array $data = null, |
|
23
|
|
|
) { |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public static function start(): self |
|
27
|
|
|
{ |
|
28
|
|
|
if (! function_exists('xhprof_enable')) { |
|
29
|
|
|
return new self(); // @codeCoverageIgnore |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** @psalm-suppress UndefinedConstant, MixedArgument */ |
|
33
|
|
|
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY); |
|
34
|
|
|
|
|
35
|
|
|
return new self(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function stop(string $uri): self |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
if (! function_exists('xhprof_disable')) { |
|
41
|
|
|
return new self(); // @codeCoverageIgnore |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** @var array<string, array<string, int>>|false $xhprofData */ |
|
45
|
|
|
$xhprofData = xhprof_disable(); |
|
46
|
|
|
|
|
47
|
|
|
if ($xhprofData === false || $xhprofData === []) { |
|
48
|
|
|
return new self(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return new self($xhprofData); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** @return array<string, mixed> */ |
|
55
|
|
|
#[Override] |
|
56
|
|
|
public function jsonSerialize(): array |
|
57
|
|
|
{ |
|
58
|
|
|
if ($this->data === null) { |
|
59
|
|
|
return []; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return [ |
|
63
|
|
|
'data' => $this->data, |
|
64
|
|
|
'spec_url' => 'https://github.com/tideways/php-xhprof-extension?tab=readme-ov-file#data-format', |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.