-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathprofiler.php
More file actions
1299 lines (1158 loc) · 33.2 KB
/
profiler.php
File metadata and controls
1299 lines (1158 loc) · 33.2 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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Php-Profiler
*
* The php-profiler is used to analyze your application in order to determine where you could use
* the most optimization.
*
* Copyright (C) 2012 Jim Rubenstein <jrubenstein@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
* @link http://github.com/jimrubenstein/php-profiler
* @author Jim Rubenstein <jrubenstein@gmail.com>
* @version 1.0
* @package php-profiler
*/
/**
* Static Profiler Class
*
* The profiler class is where all interaction with the php-profiler takes place. You use it to create
* step nodes and render the output.
*
* @package php-profiler
*/
class Profiler
{
/**
* Used to insure that the {@link init} method is only called once.
*
* @see Profiler::init()
* @var bool
*/
protected static $init = false;
/**
* Used to identify when the profiler has been enabled.
*
* If <em>false</em> no profiling data is stored, in order to reduce the overhead of running the profiler
*
* @var bool
*/
protected static $enabled = false;
/**
* Tracks the current step node
*
* @var ProfilerNode
*/
protected static $currentNode = null;
/**
* Tracks the current tree depth
*
* @var int
*/
protected static $depthCount = 0;
/**
* List of all top-level step nodes
*
* @var array of {@link ProfilerNode}s
*/
protected static $topNodes = array();
/**
* Time the profiler was included
*
* This is used to calculate time-from-start values for all methods
* as well as total running time.
*
* @var float
*/
protected static $globalStart = 0;
/**
* Time the profiler 'ends'
*
* This is populated just before rendering output (see {@link Profiler::render()})
*
* @var float
*/
protected static $globalEnd = 0;
/**
* Total time script took to run
*
* @var float
*/
protected static $globalDuration = 0;
/**
* Global tracker for step times
*
* Keeps track of how long each node took to execute. This is used to determine
* what is a "trivial" node, and what is not.
*
* @see Profiler::calculateThreshold()
* @see Profiler::isTrivial()
* @see ProfilerNode::$selfDuration
*
* @var array of floats
*/
protected static $childDurations = array();
/**
* Percentile boundary for trivial execution times
*
* @see Profiler::calculateThreshold()
*
* @var float
*/
protected static $trivialThreshold = .75;
/**
* Execution time cut off value for trivial/non-trivial nodes
*
* @see Profiler::calculateThreshold()
* @see Profilier::isTrivial()
*
* @var float
*/
protected static $trivialThresholdMS = 0;
/**
* Total amount of time used in SQL queries
*
* @var float
*/
protected static $totalQueryTime = 0;
/**
* Used to identify when some methods are accessed internally
* versus when they're used externally (as an api or so)
*
* @var string
*/
protected static $profilerKey = null;
/**
* A lightweight shell node used to return when the profiler is disabled.
*
* @var ProfilerGhostNode
*/
protected static $ghostNode;
/**
* Create a constructor that basically says "don't construct me!"
*/
public function __construct()
{
throw new Exception("The Profiler class is a static class. Do not instantiate it, access all member methods statically.");
}
/**
* Initialize the profiler
*
* Set the {@link profiler::$globalStart} time, random {@link profiler::$profilerKey}, and instantiate a {@link profiler::$ghostNode}
*
* @return null doesn't return anything.
*/
public static function init()
{
if (self::$init) return;
self::$globalStart = microtime(true);
self::$profilerKey = md5(rand(1,1000) . 'louddoor!' . time());
self::$ghostNode = new ProfilerGhostNode;
self::$init = true;
}
/**
* Check to see if the profiler is enabled
*
* @see profiler::enabled
*
* @return bool true if profiler is enabled, false if disabled
*/
public static function isEnabled()
{
return self::$enabled;
}
/**
* Enable the profiler
*
* @see profiler::enabled
*
* @return null doesn't return anything
*/
public static function enable()
{
self::$enabled = true;
}
/**
* Disable the profiler
*
* @see profiler::enabled
*
* @return null doesn't return anything.
*/
public static function disable()
{
if (self::$currentNode == null && count(self::$topNodes) == 0)
{
self::$enabled = false;
}
else
{
throw new exception("Can not disable profiling once it has begun.");
}
}
/**
* Start a new step
*
* This is the most-called method of the profiler. It initializes and returns a new step node.
*
* @param string $nodeName name/identifier for your step. is used later in the output to identify this step
*
* @return ProfilerNode|ProfilerGhostNode returns an instance of a {@link ProfilerNode} if the profiler is enabled, or a {@link ProfilerGhostNode} if it's disabled
*/
public static function start($nodeName)
{
if (!self::isEnabled()) return self::$ghostNode;
$newNode = new ProfilerNode($nodeName, ++self::$depthCount, self::$currentNode, self::$profilerKey);
if (self::$currentNode)
{
self::$currentNode->addChild($newNode);
}
else
{
self::$topNodes []= $newNode;
}
self::$currentNode = $newNode;
return self::$currentNode;
}
/**
* End a step
*
* End a step by name, or end all steps in the current tree.
*
* @param string $nodeName ends the first-found step with this name. (Note: a warning is generated if it's not the current step, because this is probably unintentional!)
* @param bool $nuke denotes whether you are intentionally attempting to terminate the entire step-stack. If true, the warning mentioned is not generated.
*
* @return bool|ProfilerNode|ProfilerGhostNode returns null if you ended the top-level step node, or the parent to the ended node, or a ghost node if the profiler is disabled.
*/
public static function end($nodeName, $nuke = false)
{
if (!self::isEnabled()) return self::$ghostNode;
if (self::$currentNode == null)
{
return;
}
while (self::$currentNode && self::$currentNode->getName() != $nodeName)
{
if (!$nuke)
{
trigger_error("Ending profile node '" . self::$currentNode->getName() . "' out of order (Requested end: '{$nodeName}')", E_USER_WARNING);
}
self::$currentNode = self::$currentNode->end(self::$profilerKey);
self::$depthCount --;
}
if (self::$currentNode && self::$currentNode->getName() == $nodeName)
{
self::$currentNode = self::$currentNode->end(self::$profilerKey);
self::$depthCount --;
}
return self::$currentNode;
}
/**
* Start a new sql query
*
* This method is used to tell the profiler to track an sql query. These are treated differently than step nodes
*
* @param string $query the query that you are running (used in the output of the profiler so you can view the query run)
*
* @return ProfilerSQLNode|ProfilerGhostNode returns an instance of the {@link ProfilerGhostNode} if profiler is enabled, or {@link ProfilerGhostNode} if disabled
*/
public static function sqlStart($query)
{
if (!self::isEnabled()) return self::$ghostNode;
if (false == self::$currentNode)
{
self::start("Profiler Default Top Level");
}
$sqlProfile = new ProfilerSQLNode($query, self::$currentNode);
self::$currentNode->sqlStart($sqlProfile);
return $sqlProfile;
}
/**
* Increment the total query time
*
* This method is used by the {@link ProfilerGhostNode} to increment the total query time for the page execution.
* This method should <b>never</b> be called in userland. There is zero need to.
*
* @param float $time amount of time the query took to execute in microseconds.
*
* @return float current amount of time (in microseconds) used to execute sql queries.
*/
public static function addQueryDuration($time)
{
return self::$totalQueryTime += $time;
}
/**
* Get the total amount of query time
*
* @return float total time used to execute sql queries (milliseconds, 1 significant digit)
*/
public static function getTotalQueryTime()
{
return round(self::$totalQueryTime * 1000, 1);
}
/**
* Get the global start time
*
* @return float start time of the script from unix epoch (milliseconds, 1 significant digit)
*/
public static function getGlobalStart()
{
return round(self::$globalStart * 1000, 1);
}
/**
* Get the global script duration
*
* @return float duration of the script (in milliseconds, 1 significant digit)
*/
public static function getGlobalDuration()
{
return round(self::$globalDuration * 1000, 1);
}
/**
* Get the global memory usage in KB
*
* @param string unit a metric prefix to force the unit of bytes used (B, K, M, G)
*
*/
public static function getMemUsage($unit = '')
{
$usage = memory_get_usage();
if ($usage < 1e3 || $unit == 'B')
{
$unit = '';
}
elseif ($usage < 9e5 || $unit == 'K')
{
$usage = round($usage / 1e3, 2);
$unit = 'K';
}
elseif ($usage < 9e8 || $unit == 'M')
{
$usage = round($usage / 1e6, 2);
$unit = 'M';
}
elseif ($usage < 9e11 || $unit = 'G')
{
$usage = round($usage / 1e9, 2);
$unit = 'G';
}
else
{
$usage = round($usage / 1e12, 2);
$unit = 'T';
}
return array(
'num' => $usage,
'unit' => $unit,
);
}
/**
* Render the profiler output
*
* @param int $show_depth the depth of the step tree to traverse when rendering the profiler output. -1 to render the entire tree
*/
public static function render($show_depth = -1)
{
if (!self::isEnabled()) return self::$ghostNode;
self::end("___GLOBAL_END_PROFILER___", true);
self::$globalEnd = microtime(true);
self::$globalDuration = self::$globalEnd - self::$globalStart;
self::calculateThreshold();
require_once dirname(__FILE__) . '/profiler_tpl.tpl.php';
}
/**
* Add node duration to the {@link profiler::$childDurations} variable
*
* @see profiler::$childDurations
*
* @param float $time duration of the child node in microseconds
*/
public static function addDuration($time)
{
self::$childDurations []= $time;
}
/**
* Set the Percentile Boundary Threshold
*
* This is used to set the percentile boundary for when a node is considered trivial or not.
* By default, .75 is used. This translates to the fastest 25% of nodes being regarded "trivial".
* This is a sliding scale, so you will always see some output, regardless of how fast your application runs.
*
* @see profiler::$trivialThreshold
*
* @param float $threshold the threshold to use as the percentile boundary
*/
public function setTrivialThreshold($threshold)
{
self::$trivialThreshold = $threshold;
}
/**
* Calculate the time cut-off for a trivial step
*
* Utilizes the {@link profiler::$trivialThreshold} value to determine how fast a step must be to be regarded "trivial"
*
* @uses profiler::$trivialThresdhold
* @see profiler::$trivialThresholdMS
*/
protected static function calculateThreshold()
{
if (count(self::$childDurations))
{
foreach (self::$childDurations as &$childDuration)
{
$childDuration = round($childDuration * 1000, 1);
}
sort(self::$childDurations);
self::$trivialThresholdMS = self::$childDurations[ floor(count(self::$childDurations) * self::$trivialThreshold) ];
}
}
/**
* Determines if a node is trivial
*
* @uses profiler::$trivialThresholdMS
*
* @return bool true if a node is trivial, false if not
*/
public static function isTrivial($node)
{
$node_duration = $node->getSelfDuration();
return $node_duration < self::$trivialThresholdMS;
}
}
/**
* @internal
* Initialize the profiler as soon as it's available, so we can get an accurate start-time and duration.
*/
profiler::init();
/**
* Class which represents the profiler steps
*
* @package php-profiler
*/
class ProfilerNode
{
/**
* Name of the step
* @var string
*/
protected $name;
/**
* Tree depth of this step
* @var int
*/
protected $depth = 0;
/**
* Time the step started
*
* Stored as microseconds from the unix epoc.
* @var float
*/
protected $started = null;
/**
* Time the step ended
*
* Stored as microseconds from the unix epoc.
* @var float
*/
protected $ended = null;
/**
* Total time the step ran INCLUDING it's children
*
* @var float
*/
protected $totalDuration = null;
/**
* Total time the step ran WITHOUT it's children
*
* @var float
*/
protected $selfDuration = null;
/**
* Total time children steps spent running
*
* @var float
*/
protected $childDuration = 0;
/**
* The parent step to this node
*
* @var ProfilerNode
*/
protected $parentNode = null;
/**
* List of this step's direct children
*
* @var array contains {@link ProfilerNode}s
*/
protected $childNodes = array();
/**
* Number of queries run at this step
*
* @var int
*/
protected $sqlQueryCount = 0;
/**
* List of this step's SQL queries
*
* @var array contains {@link ProfilerSQLNode}s
*/
protected $sqlQueries = array();
/**
* Total time spent performing SQL queries.
*
* Stored in microseconds
*
* @var float
*/
protected $totalSQLQueryDuration = 0;
/**
* Local reference to profiler key generated at initialization
*
* @see Profiler::$profilerKey
* @see Profiler::init
* @var string
*/
protected $profilerKey = null;
/**
* Constructor for {@link ProfilerNode}
*
* Initializes this step on instanciation
* @see ProfilerNode::$name
* @see ProfilerNode::$depth
* @see ProfilerNode::$parentNode
* @see ProfilerNode::$profilerKey
*
* @param string $name name of this step
* @param int $depth tree depth of this step
* @param ProfilerNode $parentNode reference to this step's parent. null if top-level.
* @param string $profilerKey api key to identify an internal api call from an external one.
*/
public function __construct($name, $depth, $parentNode, $profilerKey)
{
$this->started = microtime(true);
$this->name = $name;
$this->depth = $depth;
$this->parentNode = $parentNode;
$this->profilerKey = $profilerKey;
}
/**
* End the timer for this step
*
* Call this after the code that is being profiled by this step has completed executing
*
* @param string $profilerKey this is for internal use only! don't ever pass anything! {@link profiler::$profilerKey}
*
* @return bool|ProfilerNode returns parent node, or null if there is no parent
*/
public function end($profilerKey = null)
{
if (!$profilerKey || $profilerKey != $this->profilerKey)
{
profiler::end($this->name);
return $this->parentNode;
}
if (null == $this->ended)
{
$this->ended = microtime(true);
$this->totalDuration = $this->ended - $this->started;
$this->selfDuration = $this->totalDuration - $this->childDuration;
if ($this->parentNode)
{
$this->parentNode->increaseChildDuration($this->totalDuration);
profiler::addDuration( $this->selfDuration );
}
}
return $this->parentNode;
}
/**
* Add {@link ProfilerSQLNode} to this step
*
* This method is called by the {@link Profiler::sqlStart} method
*
* @access protected
* @param ProfilerSQLNode $sqlProfile an instance of the {@link ProfilerSQLNode} to add to this step
* @return ProfilerSQLNode reference to the {@link ProfilerSQLNode} object for the query initiated
*/
public function sqlStart($sqlProfile)
{
$this->sqlQueries []= $sqlProfile;
$this->sqlQueryCount ++;
return $sqlProfile;
}
/**
* Return the name of this step
*
* @return string name of this step
*/
public function getName()
{
return $this->name;
}
/**
* Return tree depth of this step
*
* @return int tree depth of this step
*/
public function getDepth()
{
return $this->depth;
}
/**
* Return this step's parent node
*
* @return bool|ProfilerNode returns {@link ProfilerNode} object for the parent node to this step, or null if there is no parent
*/
public function getParent()
{
return $this->parentNode;
}
/**
* Increase the total time child steps have taken
*
* Stored in microseconds
*
* @see ProfilerNode::childDuration
* @param float $time amount of time to add to the total child duration, in microseconds
*
* @return float return number total time child steps have taken, in microseconds
*/
public function increaseChildDuration($time)
{
$this->childDuration += $time;
return $this->childDuration;
}
/**
* Add child {@link ProfilerNode} to this node
*
* @param ProfilerNode $childNode the profiler node to add
*
* @return ProfilerNode return a reference to this profiler node (for chaining)
*/
public function addChild($childNode)
{
$this->childNodes []= $childNode;
return $this;
}
/**
* Determine if this node has child steps or not
*
* @return bool true if this node has child steps, false otherwise
*/
public function hasChildren()
{
return count($this->childNodes) > 0? true : false;
}
/**
* Get the children steps for this step
*
* @return array list of {@link ProfilerNodes} that are the child of this node
*/
public function getChildren()
{
return $this->childNodes;
}
/**
* Determine if this node has trivial children
*
* Traverse the tree of child steps until a non-trivial node is found. This is used at render time.
*
* @see ProfilerRenderer::renderNode()
* @see Profiler::isTrivial()
* @return bool false if all children are trivial, true if there's at least one non-trivial
*/
public function hasNonTrivialChildren()
{
if ($this->hasChildren())
{
foreach ($this->getChildren() as $child)
{
if (!profiler::isTrivial($child))
{
return true;
}
if ($child->hasNonTrivialChildren())
{
return true;
}
}
}
return false;
}
/**
* Determine if SQL queries were executed at this step
*
* @return bool true if there are queries, false if not
*/
public function hasSQLQueries()
{
return $this->sqlQueryCount > 0? true : false;
}
/**
* Get all the SQL queries executed at this step
*
* @return array list of {@link ProfilerSQLNode}s
*/
public function getSQLQueries()
{
return $this->sqlQueries;
}
/**
* Return number of queries run at this step
*
* @return int number of queries run at this step
*/
public function getSQLQueryCount()
{
return $this->sqlQueryCount;
}
/**
* Increment the total sql duration at this step
*
* @see ProfilerSQLNode::end()
*
* @param float $time amount of time to increment the SQL duration by, in microseconds
* @return ProfilerNode return instance of this step, for chaining
*/
public function addQueryDuration($time)
{
$this->totalSQLQueryDuration += $time;
}
/**
* Get the total duration for SQL queries executed at this step in milliseconds
*
* @return float duration of query time at this step, in milliseconds, 1 significant digit
*/
public function getTotalSQLQueryDuration()
{
return round($this->totalSQLQueryDuration * 1000, 1);
}
/**
* Get the start time of this step in milliseconds
*
* @return float start time of this step, in milliseconds, from unix epoch. (1 significant digit)
*/
public function getStart()
{
return round($this->started * 1000, 1);
}
/**
* Get the end time of this step in milliseconds
*
* @return float end time of this step, in milliseconds, from unix epoch. (1 significant digit)
*/
public function getEnd()
{
return round($this->ended * 1000, 1);
}
/**
* Get the total time spent executing this node, including children
*
* @return float duration of this step, in milliseconds. (1 significant digit)
*/
public function getTotalDuration()
{
return round($this->totalDuration * 1000, 1);
}
/**
* Get the duration of execution for this step, excluding child nodes.
*
* @return float duration of this step, excluding child nodes. (1 significant digit)
*/
public function getSelfDuration()
{
return round($this->selfDuration * 1000, 1);
}
}
/**
* Class representing each SQL query run
*
* @package php-profiler
*/
class ProfilerSQLNode
{
/**
* The query that this object tracks
*
* @var string
*/
protected $query;
/**
* Reference to the step this SQL query runs in
*
* @var ProfilerNode
*/
protected $profileNode;
/**
* Start time of this query (in microseconds)
*
* @var float
*/
protected $started = null;
/**
* End time of this query (in microseconds)
*
* @var float
*/
protected $ended = null;
/**
* Duration for this query (in microseconds)
*
* @var float
*/
protected $duration = null;
/**
* Call stack backtrace of all methods/functions executed up until this SQL query is run
*
* @var array
*/
protected $callstack = array();
/**
* Constructor for the {@link ProfilerSQLNode}
*
* initializes the timers and call stack for this sql query
*
* @param string $query the sql query for this node
* @param bool|ProfilerNode $profileNode reference to the step that this query is running withing
*/
public function __construct($query, $profileNode = null)
{
$this->started = microtime(true);
$this->query = $query;
$this->profileNode = $profileNode;
$this->callstack = debug_backtrace();
array_shift($this->callstack);
array_shift($this->callstack);
}
/**
* End the timers for this sql node
*
* Call this method when the sql query has finished running
*
* @return ProfilerSQLNode return a reference to this query, for chaining.
*/
public function end()
{
if (null == $this->ended)
{
$this->ended = microtime(true);
$this->duration = $this->ended - $this->started;
$this->profileNode->addQueryDuration($this->duration);
profiler::addQueryDuration($this->duration);
}
return $this;
}
/**
* Get the query for this SQLNode
*
* Query is parsed so extraneous spaces are removed where required
*
* @return string query for this sqlnode
*/
public function getQuery()
{
return preg_replace('#^\s+#m', "\n", $this->query);
}
/**
* Get the type of query this is
*
* Parse the query and try to figure out what kind of query it is
*
* @return string 'reader' if this is a select query, 'writer' if this is a typical writer query, or 'special' if it's another kind
*/
public function getQueryType()
{
list($start_clause) = preg_split("#\s+#", $this->getQuery());
$start_clause = strtolower($start_clause);
switch ($start_clause)
{
case 'select':