1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\PhalueObjects\Utility; |
4
|
|
|
|
5
|
|
|
use BestServedCold\PhalueObjects\Metric; |
6
|
|
|
use BestServedCold\PhalueObjects\Metric\DeclaredClass; |
7
|
|
|
use BestServedCold\PhalueObjects\Metric\DeclaredInterface; |
8
|
|
|
use BestServedCold\PhalueObjects\Metric\DeclaredTrait; |
9
|
|
|
use BestServedCold\PhalueObjects\Metric\DefinedConstant; |
10
|
|
|
use BestServedCold\PhalueObjects\Metric\DefinedFunction; |
11
|
|
|
use BestServedCold\PhalueObjects\Metric\IncludedFile; |
12
|
|
|
use BestServedCold\PhalueObjects\Metric\MemoryUsage; |
13
|
|
|
use BestServedCold\PhalueObjects\Metric\MicroTime; |
14
|
|
|
use BestServedCold\PhalueObjects\Metric\PeakMemoryUsage; |
15
|
|
|
|
16
|
|
|
class Benchmark |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected static $markers = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected static $lastName; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected static $metrics = [ |
32
|
|
|
MicroTime::class, |
33
|
|
|
MemoryUsage::class, |
34
|
|
|
DeclaredInterface::class, |
35
|
|
|
DeclaredTrait::class, |
36
|
|
|
DefinedFunction::class, |
37
|
|
|
DefinedConstant::class, |
38
|
|
|
IncludedFile::class, |
39
|
|
|
DeclaredClass::class, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param bool $name |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public static function human($name = false) |
47
|
|
|
{ |
48
|
|
|
return implode(array_map(function ($key, $value) { |
49
|
|
|
return '[' . $key . ']: [' . (string) $value . "]\n"; |
50
|
|
|
}, array_keys(self::get($name)), self::get($name))); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param bool $name |
55
|
|
|
* @return array|mixed |
56
|
|
|
*/ |
57
|
|
|
public static function get($name = false) |
58
|
|
|
{ |
59
|
|
|
return $name ? self::$markers[$name] : self::$markers; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param bool $name |
64
|
|
|
*/ |
65
|
|
|
public static function start($name = false) |
66
|
|
|
{ |
67
|
|
|
/** @var Metric $metric */ |
68
|
|
|
foreach (self::$metrics as $metric) { |
69
|
|
|
$now = $metric::now(); |
70
|
|
|
self::$markers[self::getName($name)][$now->getShortName()] = $now; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param bool $name |
76
|
|
|
*/ |
77
|
|
|
public static function stop($name = false) |
78
|
|
|
{ |
79
|
|
|
self::stopMarkers($name ?: self::getLastName()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $name |
84
|
|
|
*/ |
85
|
|
|
private static function stopMarkers($name) |
86
|
|
|
{ |
87
|
|
|
/** @var Metric $metric */ |
88
|
|
|
foreach (self::$markers[$name] as $metric) { |
89
|
|
|
self::$markers[$name][$metric->getShortName()] = $metric::now()->diff($metric); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
self::addPeak($name); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $name |
97
|
|
|
*/ |
98
|
|
|
private static function addPeak($name) |
99
|
|
|
{ |
100
|
|
|
$peak = PeakMemoryUsage::now(); |
101
|
|
|
self::$markers[$name][$peak->getShortName()] = $peak; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
private static function getLastName() |
108
|
|
|
{ |
109
|
|
|
$name = self::$lastName; |
110
|
|
|
self::$lastName = null; |
111
|
|
|
return $name; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param bool $name |
116
|
|
|
* @return string |
|
|
|
|
117
|
|
|
*/ |
118
|
|
|
private static function getName($name = false) |
119
|
|
|
{ |
120
|
|
|
self::$lastName = $name ?: uniqid(); |
|
|
|
|
121
|
|
|
return self::$lastName; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.