|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @maintainer Timur Shagiakhmetov <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Badoo\LiveProfiler; |
|
8
|
|
|
|
|
9
|
|
|
class SimpleProfiler |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var self |
|
13
|
|
|
*/ |
|
14
|
|
|
private static $instance; |
|
15
|
|
|
|
|
16
|
|
|
private $methods = []; |
|
17
|
|
|
/** @var \SplStack */ |
|
18
|
|
|
private $stack; |
|
19
|
|
|
private $is_enabled = false; |
|
20
|
|
|
|
|
21
|
4 |
|
public static function getInstance() |
|
22
|
|
|
{ |
|
23
|
4 |
|
if (self::$instance === null) { |
|
24
|
1 |
|
self::$instance = new static(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
4 |
|
return self::$instance; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
4 |
|
public function enable() |
|
31
|
|
|
{ |
|
32
|
4 |
|
$this->is_enabled= true; |
|
33
|
4 |
|
$this->stack = new \SplStack(); |
|
34
|
|
|
|
|
35
|
4 |
|
$this->startTimer('main()'); |
|
36
|
4 |
|
} |
|
37
|
|
|
|
|
38
|
4 |
|
public function disable() |
|
39
|
|
|
{ |
|
40
|
4 |
|
$this->endTimer('main()'); |
|
41
|
|
|
|
|
42
|
4 |
|
if (!$this->stack->isEmpty()) { |
|
43
|
2 |
|
return []; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
$this->is_enabled = false; |
|
47
|
|
|
|
|
48
|
2 |
|
return $this->methods; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
4 |
|
public function startTimer($tag) |
|
52
|
|
|
{ |
|
53
|
4 |
|
if (!$this->is_enabled) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
4 |
|
$full_method = $this->getFullMethod($tag); |
|
58
|
4 |
|
$this->stack->push($tag); |
|
59
|
|
|
|
|
60
|
4 |
|
if (!isset($this->methods[$full_method])) { |
|
61
|
2 |
|
$this->methods[$full_method] = [ |
|
62
|
|
|
'wt' => 0, |
|
63
|
|
|
'mu' => 0, |
|
64
|
|
|
'ct' => 0, |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
$time = (int)(microtime(true) * 1e6); |
|
69
|
4 |
|
$memory = memory_get_usage(); |
|
70
|
4 |
|
$this->methods[$full_method]['wt'] = $time - $this->methods[$full_method]['wt']; |
|
71
|
4 |
|
$this->methods[$full_method]['mu'] = $memory - $this->methods[$full_method]['mu']; |
|
72
|
|
|
|
|
73
|
4 |
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
4 |
|
public function endTimer($tag) |
|
77
|
|
|
{ |
|
78
|
4 |
|
if (!$this->is_enabled) { |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
if ($this->stack->isEmpty() || $this->stack->top() !== $tag) { |
|
83
|
2 |
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
$tag = $this->stack->pop(); |
|
87
|
|
|
|
|
88
|
2 |
|
$full_method = $this->getFullMethod($tag); |
|
89
|
|
|
|
|
90
|
2 |
|
$time = (int)(microtime(true) * 1e6); |
|
91
|
2 |
|
$memory = memory_get_usage(); |
|
92
|
2 |
|
$this->methods[$full_method]['ct']++; |
|
93
|
2 |
|
$this->methods[$full_method]['wt'] = $time - $this->methods[$full_method]['wt']; |
|
94
|
2 |
|
$this->methods[$full_method]['mu'] = $memory - $this->methods[$full_method]['mu']; |
|
95
|
|
|
|
|
96
|
2 |
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
4 |
|
private function getFullMethod($tag) |
|
100
|
|
|
{ |
|
101
|
4 |
|
if ($this->stack->isEmpty()) { |
|
102
|
4 |
|
return $tag; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
3 |
|
return $this->stack->top() . '==>' . $tag; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|