-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfiler.php
124 lines (94 loc) · 2.91 KB
/
Profiler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
require_once 'ProfilingSession.php';
/**
* Simple Profiler
*
*/
class Profiler {
/**
* Path to the folder that will contain profiling logs. Will be set in
* constructor.
* @var string logsPath
*/
protected $logsPath;
/**
* Set of profilingSessions that were started. Contains instances of type ProfilingSession.
* @var Array
*/
protected $profilingSessions = Array();
/**
* @param string $logsPath Path to the folder that will contain profiling logs.
*/
public function __construct( $logsPath ) {
$this->logsPath = $this->initLogsPath( $logsPath );
}
/**
*
* @return string
*/
public function getLogsPath() {
return $this->logsPath;
}
/**
* Creates log path if it not exists.
*
* @param string $logsPath Path to check.
* @return string Path to logs folder with trailing '/'.
*/
protected function initLogsPath( $logsPath ) {
$logsPath = rtrim( $logsPath, '/' ).'/';
if ( !file_exists( $logsPath ) ) {
//TODO make mode configurable
mkdir( $logsPath, 0777, true );
}
// TODO add logic to check if derectory is writable
return $logsPath;
}
/**
* Starts profiling session.
*
* @param string $key Unique key for profiling session.
* @param string $message Custom start message. If not specified will be used default one.
*
* @return ProfilingSession Instance of measurer to operate with.
*/
public function start(
$key,
$message = ''
) {
// TODO handle exceptional cases
$this->profilingSessions[$key] = new ProfilingSession( $this, $key, $message );
return $this->profilingSessions[$key];
}
/**
* This is a decorator for the ProfilingSession::step() method.
*
* @param string $key Unique key for profiling session.
* @param string $message Message to describe current step.
*/
public function step(
$key,
$message = ''
) {
// TODO handle exceptional cases
if ( isset( $this->profilingSessions[$key] ) ) {
$this->profilingSessions[$key]->step( $message );
}
}
/**
* This is a decorator for the ProfilingSession::stop() method.
*
* @param string $key Unique key for profiling session.
* @param string $message Custom stop message. If not specified will be used default one.
*/
public function stop(
$key,
$message = ''
) {
// TODO handle exceptional cases
if ( isset( $this->profilingSessions[$key] ) ) {
$this->profilingSessions[$key]->stop( $message );
}
}
}
?>