|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
6
|
|
|
use Rx\ObservableInterface; |
|
7
|
|
|
use WyriHaximus\React\Inspector\CollectorInterface; |
|
8
|
|
|
use WyriHaximus\React\Inspector\Metric; |
|
9
|
|
|
use function ApiClients\Tools\Rx\observableFromArray; |
|
10
|
|
|
use function React\Promise\resolve; |
|
11
|
|
|
|
|
12
|
|
|
final class MeasureMiddleware implements CollectorInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private const DEFAULT_PREFIX = ''; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private $prefix = self::DEFAULT_PREFIX; |
|
18
|
|
|
|
|
19
|
|
|
/** @var int */ |
|
20
|
|
|
private $current = 0; |
|
21
|
|
|
|
|
22
|
|
|
/** @var int */ |
|
23
|
|
|
private $total = 0; |
|
24
|
|
|
|
|
25
|
|
|
/** @var null|float */ |
|
26
|
|
|
private $tookMin = null; |
|
27
|
|
|
|
|
28
|
|
|
/** @var float */ |
|
29
|
|
|
private $tookMax = 0.0; |
|
30
|
|
|
|
|
31
|
|
|
/** @var float */ |
|
32
|
|
|
private $tookAvg = 0.0; |
|
33
|
|
|
|
|
34
|
|
|
/** @var float */ |
|
35
|
|
|
private $tookTotal = 0.0; |
|
36
|
|
|
|
|
37
|
1 |
|
public function __construct(string $prefix = self::DEFAULT_PREFIX) |
|
38
|
|
|
{ |
|
39
|
1 |
|
$this->prefix = $prefix; |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function __invoke(ServerRequestInterface $request, callable $next) |
|
43
|
|
|
{ |
|
44
|
1 |
|
$this->current++; |
|
45
|
1 |
|
$start = microtime(true); |
|
46
|
|
|
|
|
47
|
|
|
return resolve($next($request))->always(function () use ($start) { |
|
48
|
1 |
|
$took = microtime(true) - $start; |
|
49
|
1 |
|
$this->current--; |
|
50
|
1 |
|
$this->total++; |
|
51
|
1 |
|
$this->tookTotal += $took; |
|
52
|
|
|
|
|
53
|
1 |
|
if ($this->tookMin === null || $took < $this->tookMin) { |
|
54
|
1 |
|
$this->tookMin = $took; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
if ($this->tookMax < $took) { |
|
58
|
1 |
|
$this->tookMax = $took; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
$this->tookAvg = $this->tookTotal / $this->total; |
|
62
|
1 |
|
}); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function collect(): ObservableInterface |
|
66
|
|
|
{ |
|
67
|
|
|
$metrics = [ |
|
68
|
1 |
|
new Metric($this->prefix . 'current', $this->current), |
|
69
|
1 |
|
new Metric($this->prefix . 'total', $this->total), |
|
70
|
1 |
|
new Metric($this->prefix . 'took.min', $this->tookMin === null ? 0.0 : $this->tookMin), |
|
71
|
1 |
|
new Metric($this->prefix . 'took.max', $this->tookMax), |
|
72
|
1 |
|
new Metric($this->prefix . 'took.average', $this->tookAvg), |
|
73
|
1 |
|
new Metric($this->prefix . 'took.total', $this->tookTotal), |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
1 |
|
$this->total = 0; |
|
77
|
1 |
|
$this->tookMin = null; |
|
78
|
1 |
|
$this->tookMax = 0.0; |
|
79
|
1 |
|
$this->tookAvg = 0.0; |
|
80
|
1 |
|
$this->tookTotal = 0; |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
1 |
|
return observableFromArray($metrics); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function cancel(): void |
|
86
|
|
|
{ |
|
87
|
|
|
// Does not apply to this class |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.