-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.php
More file actions
186 lines (133 loc) · 4.18 KB
/
tracker.php
File metadata and controls
186 lines (133 loc) · 4.18 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
*
* Description: Process time tracker class
* Author: Denis Vororpaev
* Author Email: d.n.voropaev@gmail.com
* Version: 0.1.0
* Copyright: Denis Voropaev © 2016
*
**/
class Tracker {
private static $startTime = null;
private static $events = null;
private static $actions = null;
private static $name = null;
private static $bypass = null;
private static $format = 'Y/m/d, H:i:s';
public static function start ($name = false, $bypass = false) {
if (Tracker::$bypass = $bypass) { return; }
Tracker::$startTime = microtime (true);
Tracker::$events = array ();
Tracker::$actions = array ();
Tracker::$name = is_string ($name) ? $name : 'Process';
}
public static function log ($event = null) {
if (Tracker::$bypass) { return; }
if (!Tracker::isStarted ()) {
Tracker::start ();
}
if (!$event) {
$event = 'Event #' . count (Tracker::$events) + 1;
}
Tracker::$events[$event] = microtime (true);
}
public static function run ($action = null, $subject = null) {
if (Tracker::$bypass) { return; }
if (!Tracker::isStarted ()) {
Tracker::start ();
}
if (!$action) {
$action = 'Action #' . count (Tracker::$actions) + 1;
}
if (!$subject) {
$count = isset (Tracker::$actions[$action]) ?
count (Tracker::$actions[$action]) : 0;
$subject = 'Subject #' . $count + 1;
}
Tracker::$actions[$action][$subject]['start'] = microtime (true);
Tracker::$events["{$action}ing {$subject}"] = microtime (true);
}
public static function halt ($action, $subject) {
if (Tracker::$bypass) { return; }
if (!Tracker::isStarted () ||
!isset (Tracker::$actions[$action]) ||
!isset (Tracker::$actions[$action][$subject]) ||
!isset (Tracker::$actions[$action][$subject]['start'])){
return;
}
Tracker::$actions[$action][$subject]['end'] = microtime (true);
Tracker::$events["{$action}ing {$subject}"] = microtime (true);
Tracker::$actions[$action][$subject]['total'] = (
Tracker::$actions[$action][$subject]['end'] -
Tracker::$actions[$action][$subject]['start']);
}
public static function report ($onEvents = false, $onActions = true,
$threshold = 1.0) {
if (Tracker::$bypass || !Tracker::isStarted ()) {
return;
}
$report = sprintf ("%s timing list:\n", ucfirst (Tracker::$name));
$report .= sprintf ("Started at %s\n",
Tracker::microDate (
Tracker::$startTime));
$stop = microtime (true);
$time = $stop - Tracker::$startTime;
if ($onEvents && floatval ($time) > $threshold) {
$report .= Tracker::reportOnEvents ();
}
$report .= sprintf ("Stopped at %s\n\n", Tracker::microDate ($stop));
if ($onActions && floatval ($time) > $threshold) {
$report .= Tracker::reportOnActions ();
}
$report .= sprintf ("Total time: %s.\n", $time);
Log::obj ($report, 'Tracker report');
Tracker::stop ();
}
public static function setFormat ($format) {
if (Tracker::$bypass ||
!is_string ($format) ||
!date ($format, time ())){
return;
}
Tracker::$format = $format;
}
public static function microDate ($micro) {
if (Tracker::$bypass) { return; }
list ($seconds, $microseconds) = explode (".", $micro);
return date (Tracker::$format, $seconds) . ".$microseconds";
}
private static function reportOnEvents () {
$report = '';
$trace = Tracker::$startTime;
foreach (Tracker::$events as $name => $time) {
$report .= sprintf ("+%s secs : %s\n", $time - $trace, $name);
$trace = $time;
}
return $report;
}
private static function reportOnActions () {
$report = '';
foreach (Tracker::$actions as $name => $subjects) {
$total = 0;
foreach ($subjects as $subject => $time) {
$report .= sprintf ("Time spent on %sing %s: %s\n",
$name, $subject, $time['total']);
$total += $time['total'];
}
$report .= sprintf ("Total time on %sing: %s\n", $name, $total);
}
return $report;
}
public static function stop () {
Tracker::$bypass = null;
Tracker::$startTime = null;
Tracker::$events = null;
Tracker::$actions = null;
Tracker::$name = null;
}
private static function isStarted () {
return (Tracker::$startTime) ? true : false;
}
}
?>