|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Gorlum 05.08.2018 8:05 |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
class Timer |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var array[] $times [ |
|
10
|
|
|
* 'time' => (float)microtime(true), |
|
11
|
|
|
* 'location' => (string)filename&line, |
|
12
|
|
|
* 'message' => (string)attachedMessage |
|
13
|
|
|
* ] |
|
14
|
|
|
*/ |
|
15
|
|
|
private static $times = []; |
|
16
|
|
|
|
|
17
|
|
|
public static function init() |
|
18
|
|
|
{ |
|
19
|
|
|
if (empty(static::$times)) { |
|
|
|
|
|
|
20
|
|
|
static::$times[] = ['time' => microtime(true)]; |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $message Message to attach to timestamp |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function mark($message = '') |
|
28
|
|
|
{ |
|
29
|
|
|
static::init(); |
|
30
|
|
|
|
|
31
|
|
|
foreach (debug_backtrace() as $trace) { |
|
32
|
|
|
if (empty($trace['class']) || $trace['class'] != static::class) { |
|
33
|
|
|
break; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$realCall = $trace; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
static::$times[] = [ |
|
|
|
|
|
|
40
|
|
|
'time' => microtime(true), |
|
41
|
|
|
'location' => $realCall['file'] . '@' . $realCall['line'], |
|
|
|
|
|
|
42
|
|
|
'message' => $message, |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $message |
|
49
|
|
|
* |
|
50
|
|
|
* @param bool $fromStart |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function elapsed($message = '', $fromStart = false) |
|
55
|
|
|
{ |
|
56
|
|
|
$prevTime = $fromStart ? static::first()['time'] : static::last()['time']; |
|
57
|
|
|
|
|
58
|
|
|
static::mark($message); |
|
59
|
|
|
|
|
60
|
|
|
empty($prevTime) ? $prevTime = static::first()['time'] : false; |
|
61
|
|
|
|
|
62
|
|
|
return number_format(static::last()['time'] - $prevTime, 6) . 's'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public static function msg($message, $fromStart = false) |
|
66
|
|
|
{ |
|
67
|
|
|
print |
|
68
|
|
|
$message . |
|
69
|
|
|
($fromStart ? ' FROM START ' : ' in ') . |
|
70
|
|
|
static::elapsed($message, $fromStart) . |
|
71
|
|
|
' ---- ' . static::last()['location'] . |
|
72
|
|
|
"\n<br />"; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function last() |
|
79
|
|
|
{ |
|
80
|
|
|
return end(static::$times); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function first() |
|
87
|
|
|
{ |
|
88
|
|
|
return reset(static::$times); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public static function getLog() |
|
92
|
|
|
{ |
|
93
|
|
|
return static::$times; |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|