forked from joehoyle/Time-Stack-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time-stack.php
532 lines (362 loc) · 11.8 KB
/
time-stack.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
<?php
/*
Plugin Name: WordPress TimeStack
Description: WordPress Profiling
Author: Human Made Limited
Version: 0.9
*/
class HM_Time_Stack {
private static $instance;
public $stack;
private $start_time;
public static function instance() {
if ( !isset( self::$instance ) )
self::$instance = new HM_Time_Stack();
return self::$instance;
}
public static function get_data( $clear = false ) {
if ( function_exists( 'apc_store' ) && apc_store( '__test', '123' ) ) {
$data = apc_fetch( '_hm_all_stacks' );
} else {
$data = wp_cache_get( '_hm_all_stacks' );
}
if ( $clear )
self::set_data( array() );
return $data;
}
public static function set_data( $data ) {
if ( function_exists( 'apc_store' ) && apc_store( '__test', '123' ) ) {
return apc_store( '_hm_all_stacks', $data, 60 );
} else {
return wp_cache_set( '_hm_all_stacks', $data, null, 60 );
}
}
function __construct() {
if ( ! empty( $_GET['action'] ) && ( $_GET['action'] == 'hm_display_stacks' || $_GET['action'] == 'hm_get_stacks' ) )
return;
global $hm_time_stack_start;
if( !empty( $hm_time_stack_start ) )
$this->start_time = $hm_time_stack_start;
else
$this->start_time = hm_time_stack_time();
// store it in object cache for persistant logging
$t = $this;
add_action( 'shutdown', function() use ( $t ) {
$t->end();
}, 11 );
$this->setup_hooks();
$this->stack = new HM_Time_Stack_Operation( 'wp' );
$this->stack->start_time = $this->start_time;
//$this->stack->start_memory_usage = 0;
}
public function end() {
$this->end_operation( 'wp' );
$all_stacks = HM_Time_Stack::get_data();
if ( is_user_logged_in() )
$user = wp_get_current_user()->display_name;
else
$user = 'Anonymouse';
$all_stacks = array_reverse( (array) $all_stacks );
$all_stacks[] = $this->archived_data = array(
'stack' => $this->stack->archive(),
'date' => time(),
'url' => $_SERVER['REQUEST_URI'],
'full_url' => $_SERVER['HTTP_HOST'],
'site_id' => get_current_blog_id(),
'get_vars' => stripslashes_deep( $_GET ),
'request_type' => $_SERVER['REQUEST_METHOD'],
'user' => $user
);
$all_stacks = array_reverse( $all_stacks );
HM_Time_Stack::set_data( $all_stacks );
}
public function get_archived_data() {
return $this->archived_data['stack'];
}
public function get_id() {
return $_SERVER['REQUEST_URI'] . time();
}
public function start_operation( $id, $label = '' ) {
if ( ! $this->stack )
return;
$operation = new HM_Time_Stack_Operation( $id, $label );
$this->stack->add_operation( $operation );
}
public function end_operation( $id, $vars = null ) {
if ( ! $this->stack )
return;
$this->stack->end_operation( $this->stack->get_child_operation_by_id( $id ), $vars );
}
public function add_event( $id, $label = '' ) {
$event = new HM_Time_Stack_Event( $id, $label );
$this->stack->add_event( $event );
}
private function setup_hooks() {
$t = $this;
// global adding from actions
add_action( 'start_operation', function( $id, $label = '' ) use ( $t ) {
$t->start_operation( $id, $label );
}, 10, 2 );
add_action( 'end_operation', function( $id, $args = array() ) use ( $t ) {
if ( $t->stack->get_child_operation_by_id( $id ) ) {
$t->stack->get_child_operation_by_id( $id )->vars = $args;
$t->end_operation( $id );
}
}, 10, 2 );
add_action( 'add_event', function( $id, $label = '' ) use ( $t ) {
$t->add_event( $id, $label );
}, 10, 1 );
add_action( 'log', function( $data ) {
if ( is_scalar( $data ) )
do_action( 'add_event', $data );
else
do_action( 'add_event', print_r( $data, true ) );
} );
add_action( 'init', function() use ( $t ) {
$t->start_operation( 'hook: init' );
}, 0 );
add_action( 'init', function() use ( $t ) {
$t->end_operation( 'hook: init' );
}, 999999 );
add_action( 'parse_query', function( $wp_query ) use ( $t ) {
$query = is_string( $wp_query->query ) ? $wp_query->query : json_encode( $wp_query->query );
global $wp_the_query;
if ( $wp_the_query == $wp_query ) {
$name = 'Main WP Query';
}
else {
$trace = debug_backtrace();
if ( isset( $trace[6]['function'] ) && isset( $trace[7]['file'] ) )
$name = $trace[6]['function'] . ' - ' . $trace[7]['file'] . '[' . $trace[7]['line'] . ']';
else
$name = 'WP_Query';
}
$t->start_operation( 'wp_query::' . spl_object_hash( $wp_query ), 'WP_Query - ' . $name );
$wp_query->query_vars['suppress_filters'] = false;
}, 1 );
add_action( 'the_posts', function( $posts, $wp_query ) use ( $t ) {
$t->end_operation( 'wp_query::' . spl_object_hash( $wp_query ) );
return $posts;
}, 99, 2 );
add_action( 'shutdown', function() use ( $t ) {
$t->add_event( 'shutdown' );
} );
add_action( 'template_redirect', function() use ( $t ) {
$t->add_event( 'template_redirect' );
}, 1 );
add_action( 'wp_head', function() use ( $t ) {
$t->add_event( 'wp_head' );
}, 1 );
add_action( 'loop_start', function( $wp_query ) use ( $t ) {
$query = is_string( $wp_query->query ) ? $wp_query->query : json_encode( $wp_query->query );
$t->add_event( 'the_loop::' . spl_object_hash( $wp_query ), 'Loop Start' );
}, 1 );
add_action( 'loop_end', function( $wp_query ) use ( $t ) {
$query = is_string( $wp_query->query ) ? $wp_query->query : json_encode( $wp_query->query );
$t->add_event( 'the_loop::' . spl_object_hash( $wp_query ), 'Loop End' );
}, 1 );
add_action( 'get_sidebar', function( $name ) use ( $t ) {
$t->add_event( 'get_sidebar', 'get_sidebar - ' . $name );
}, 1 );
add_action( 'wp_footer', function() use ( $t ) {
$t->add_event( 'wp_footer' );
}, 1 );
// hooks for remote rewuest, (but hacky)
add_filter( 'https_ssl_verify', function( $var ) {
do_action( 'start_operation', 'Remote Request' );
return $var;
} );
add_action( 'http_api_debug', function( $response, $type, $class, $args, $url ) use ( $t ) {
$t->end_operation( 'Remote Request', array( 'url' => $url ) );
}, 10, 5 );
add_action( 'plugins_loaded', function() use ( $t ) {
$t->add_event( 'loaded plugins' );
}, 9999999 );
}
public function get_start_time() {
return $this->start_time;
}
}
class HM_Time_Stack_Operation {
public $start_time;
public $end_time;
public $duration;
public $id;
public $label;
public $is_open;
private $open_operation;
public $children;
public $peak_memory_usage;
public $start_memory_usage;
public $end_memory_usage;
public $start_query_count;
public $end_query_count;
public $query_count;
public $vars = array();
public $time;
public function __construct( $id, $label = '' ) {
$this->children = array();
$this->id = $id;
$this->start_time = hm_time_stack_time();
if ( $id !== 'wp' )
$this->time = round( hm_time_stack_time() - HM_Time_Stack::instance()->get_start_time(), 3 );
$this->label = $label;
$this->is_open = true;
$this->start_memory_usage = memory_get_peak_usage();
global $wpdb;
if ( ! defined( 'SAVEQUERIES' ) )
define( 'SAVEQUERIES', true );
$this->start_query_count = count( $wpdb->queries );
}
public function end() {
$this->end_time = hm_time_stack_time();
$this->end_memory_usage = memory_get_peak_usage();
$this->peak_memory_usage = round( ( $this->end_memory_usage - $this->start_memory_usage ) / 1024 / 1024, 4 );
$this->duration = round( $this->end_time - $this->start_time, 4 );
$this->is_open = false;
global $wpdb;
$this->end_query_count = count( $wpdb->queries );
$this->query_count = $this->end_query_count - $this->start_query_count;
if ( $wpdb->queries )
$this->queries = array_slice( $wpdb->queries, $this->start_query_count );
else
$this->queries = array();
}
public function add_operation( $operation ) {
if ( ! empty( $this->open_operation ) ) {
$this->open_operation->add_operation( $operation );
}
else {
$this->children[] = $operation;
$this->open_operation = $operation;
}
}
public function add_event( $event ) {
if ( ! empty( $this->open_operation ) ) {
$this->open_operation->add_event( $event );
} else {
$this->children[] = $event;
}
}
public function end_operation( $operation, $vars = null ) {
if ( ! empty( $this->open_operation ) ) {
if ( $this->open_operation == $operation ) {
if ( $vars )
$this->open_operation->vars = array_merge( $this->open_operation->vars, $vars );
$this->open_operation->end();
$this->open_operation = null;
}
else {
$this->open_operation->end_operation( $operation, $vars );
}
}
if ( $operation === $this ) {
if ( $vars )
$this->vars = array_merge( $this->vars, $vars );
$this->end();
}
}
public function get_child_operation_by_id( $id ) {
$return = null;
foreach ( $this->children as $child ) {
if ( $operation = $child->get_child_operation_by_id( $id ) ) {
$return = $operation;
break;
}
}
if ( $this->is_open && $this->id == $id )
$return = $this;
return $return;
}
public function archive() {
$archive = (object) array();
$archive->type = get_class($this);
$archive->queries = ! empty( $this->queries ) ? $this->queries : array();
$archive->query_time = $this->get_query_time();
$archive->is_open = $this->is_open;
$archive->duration = round( $this->duration, 4 );
$archive->memory_usage = round( $this->peak_memory_usage, 4 );
$archive->label = $this->label ? $this->label : $this->id;
$archive->vars = $this->vars;
$archive->time = $this->time;
foreach ( $this->children as $operation )
$archive->children[] = $operation->archive();
//special case for "wp"
if ( $this->id === 'wp' ) {
$archive->request = array(
'user_agent',
);
}
return $archive;
}
private function get_query_time() {
$query_time = 0;
if ( !empty( $this->queries ) )
foreach ( (array) $this->queries as $q )
$query_time += $q[1];
}
public function _print() {
$query_time = 0;
if ( !empty( $this->queries ) )
foreach ( (array) $this->queries as $q )
$query_time += $q[1];
?>
<li class="operation">
<span class="title">
operation: <strong><?php echo $this->label ? $this->label : $this->id ?></strong>
<span class="querie-count"><?php echo $this->query_count ?> Queries [<?php echo $query_time ?>]</span>
<span class="memory-usage"><?php echo $this->peak_memory_usage ?>MB</span>
<span class="duration"><?php echo $this->duration ?></span>
</span>
<?php if ( $this->vars ) : ?>
<?php print_r( $this->vars ) ?>
<?php endif; ?>
<?php if ( $this->is_open ) : ?>
Warning: Not Ended;
<?php endif; ?>
<ul>
<?php foreach( $this->children as $operation ) : ?>
<?php $operation->_print(); ?>
<?php endforeach; ?>
</ul>
</li>
<?php
}
}
class HM_Time_Stack_Event extends HM_Time_Stack_Operation {
public $id;
public $time;
function __construct( $id, $label = '' ) {
$this->id = $id;
$this->time = round( hm_time_stack_time() - HM_Time_Stack::instance()->get_start_time(), 3 );
$this->children = array();
$this->label = $label;
$this->peak_memory_usage = round( memory_get_peak_usage() / 1024 / 1024, 3 );
}
function _print() {
?>
<li class="event">
<?php echo $this->label ? $this->label : $this->id ?>
<span class="memory-usage"><?php echo $this->peak_memory_usage ?>MB</span>
<span class="duration"><?php echo $this->time ?> in
</li>
<?php
}
function archive() {
$archive = parent::archive();
unset( $archive->duration );
$archive->time = $this->time;
return $archive;
}
}
function hm_time_stack_time() {
$time = explode( ' ', microtime() );
$time = $time[1] + $time[0];
return $time;
}
HM_Time_Stack::instance();
// show persistant stacks
if ( isset( $_GET['action'] ) && $_GET['action'] == 'hm_get_stacks' ) {
echo $_GET['jsoncallback'] . '(' . json_encode( HM_Time_Stack::get_data( true ) ) . ')';
exit;
}