1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\Benchmark; |
4
|
|
|
|
5
|
|
|
use BestServedCold\Benchmark\Factory\Measure, |
6
|
|
|
BestServedCold\Benchmark\Factory\Peak; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Benchmark |
10
|
|
|
* |
11
|
|
|
* @package BestServedCold\Benchmark |
12
|
|
|
*/ |
13
|
|
|
class Benchmark |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected static $markers = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected static $lastName; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Benchmark constructor. |
27
|
|
|
* |
28
|
|
|
* @param array $markers |
29
|
|
|
*/ |
30
|
4 |
|
public function __construct($markers = []) |
31
|
|
|
{ |
32
|
4 |
|
self::$markers = $markers; |
33
|
4 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param null|string $name |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
6 |
|
public static function start($name = null) |
40
|
|
|
{ |
41
|
6 |
|
$name = self::getName($name); |
42
|
6 |
|
self::$markers[$name] = Measure::now(); |
43
|
6 |
|
return $name; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param null|string $name |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
6 |
|
public static function stop($name = null) |
51
|
|
|
{ |
52
|
6 |
|
$name = $name ?: self::getLastName(); |
53
|
6 |
|
self::$markers[$name] = array_merge(Measure::diff(self::$markers[$name]), Peak::now()); |
54
|
6 |
|
return $name; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param null|string $name |
59
|
|
|
* @return array|mixed |
60
|
|
|
*/ |
61
|
3 |
|
public static function getMarkers($name = null) |
62
|
|
|
{ |
63
|
3 |
|
return $name ? [ $name => self::$markers[$name]] : self::$markers; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string|null $name |
68
|
|
|
* @return static |
69
|
|
|
*/ |
70
|
4 |
|
public static function get($name = null) |
71
|
|
|
{ |
72
|
4 |
|
return $name ? new self([$name => self::$markers[$name]]) : new static(self::$markers); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
2 |
|
private static function getLastName() |
79
|
|
|
{ |
80
|
2 |
|
$name = self::$lastName; |
81
|
2 |
|
self::$lastName = null; |
82
|
2 |
|
return $name; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param null|string $name |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
6 |
|
private static function getName($name = null) |
90
|
|
|
{ |
91
|
6 |
|
self::$lastName = $name ?: uniqid(); |
92
|
6 |
|
return self::$lastName; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
6 |
|
public static function reset() |
99
|
|
|
{ |
100
|
6 |
|
self::$lastName = null; |
101
|
6 |
|
self::$markers = []; |
102
|
6 |
|
} |
103
|
|
|
} |
104
|
|
|
|