1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Rpc\Decorators; |
4
|
|
|
|
5
|
|
|
use ScayTrase\Api\Rpc\RpcRequestInterface; |
6
|
|
|
use ScayTrase\Api\Rpc\RpcResponseInterface; |
7
|
|
|
|
8
|
|
|
final class ProfiledClientStorage |
9
|
|
|
{ |
10
|
|
|
/** @var array[] */ |
11
|
|
|
private $pairs = []; |
12
|
|
|
/** @var array[] */ |
13
|
|
|
private $unmatchedResponse = []; |
14
|
|
|
/** @var string */ |
15
|
|
|
private $clientName; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* RpcProfiler constructor. |
19
|
|
|
* |
20
|
|
|
* @param string $clientName |
21
|
|
|
*/ |
22
|
2 |
|
public function __construct($clientName) |
23
|
|
|
{ |
24
|
2 |
|
$this->clientName = (string)$clientName; |
25
|
2 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param RpcRequestInterface|RpcRequestInterface[] $calls |
29
|
|
|
*/ |
30
|
2 |
|
public function registerCalls($calls) |
31
|
|
|
{ |
32
|
2 |
|
if (!is_array($calls)) { |
33
|
1 |
|
$calls = [$calls]; |
34
|
1 |
|
} |
35
|
|
|
|
36
|
2 |
|
$time = microtime(true); |
37
|
2 |
|
foreach ($calls as $call) { |
38
|
2 |
|
$this->pairs[spl_object_hash($call)]['start'] = $time; |
39
|
2 |
|
$this->pairs[spl_object_hash($call)]['request'] = $call; |
40
|
2 |
|
} |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param RpcResponseInterface $response |
45
|
|
|
* @param RpcRequestInterface $request |
46
|
|
|
*/ |
47
|
2 |
|
public function registerResponse(RpcResponseInterface $response, RpcRequestInterface $request = null) |
48
|
|
|
{ |
49
|
2 |
|
$time = microtime(true); |
50
|
|
|
|
51
|
2 |
|
if (null === $request) { |
52
|
1 |
|
$this->unmatchedResponse[]['response'] = $response; |
53
|
|
|
|
54
|
1 |
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$this->pairs[spl_object_hash($request)]['stop'] = $time; |
58
|
1 |
|
$this->pairs[spl_object_hash($request)]['response'] = $response; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array[] |
63
|
|
|
*/ |
64
|
1 |
|
public function getFullPairs() |
65
|
|
|
{ |
66
|
1 |
|
return array_filter( |
67
|
1 |
|
$this->pairs, |
68
|
|
|
function (array $row) { |
69
|
1 |
|
return isset($row['response']); |
70
|
|
|
} |
71
|
1 |
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
public function getUnmatchedRequestPairs() |
75
|
|
|
{ |
76
|
1 |
|
return array_filter( |
77
|
1 |
|
$this->pairs, |
78
|
1 |
|
function (array $row) { |
79
|
1 |
|
return !isset($row['response']); |
80
|
|
|
} |
81
|
1 |
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array[] |
86
|
|
|
*/ |
87
|
1 |
|
public function getUnmatchedResponsePairs() |
88
|
|
|
{ |
89
|
1 |
|
return $this->unmatchedResponse; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
1 |
|
public function getClientName() |
96
|
|
|
{ |
97
|
1 |
|
return $this->clientName; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|