1
+ <?php
2
+
3
+ final class PinbaTest extends TestCase
4
+ {
5
+ protected function setUp ()
6
+ {
7
+ if (!extension_loaded ('pinba ' ))
8
+ $ this ->markTestSkipped (
9
+ 'The pinba extension is not available. '
10
+ );
11
+
12
+ if (!PinbaClient::isEnabled ())
13
+ $ this ->markTestSkipped (
14
+ 'The pinba is not enabled at php.ini (pinba.enabled=1). '
15
+ );
16
+
17
+ if (!extension_loaded ('runkit ' )) {
18
+ $ this ->markTestSkipped (
19
+ 'The runkit extension is not available. '
20
+ );
21
+ }
22
+
23
+ if (!ini_get ('runkit.internal_override ' ))
24
+ $ this ->markTestSkipped (
25
+ 'The runkit.internal_override is not enabled (enabled it at php.ini). '
26
+ );
27
+
28
+ runkit_function_rename ('pinba_timer_start ' , 'pinba_timer_start_bak ' );
29
+ runkit_function_rename ('pinba_timer_stop ' , 'pinba_timer_stop_bak ' );
30
+
31
+ runkit_function_rename ('pinba_timer_start_callback ' , 'pinba_timer_start ' );
32
+ runkit_function_rename ('pinba_timer_stop_callback ' , 'pinba_timer_stop ' );
33
+ }
34
+
35
+ public static function tearDownAfterClass (){
36
+
37
+ if (
38
+ !extension_loaded ('runkit ' )
39
+ || !ini_get ('runkit.internal_override ' )
40
+ )
41
+ return ;
42
+
43
+ runkit_function_rename ('pinba_timer_start ' , 'pinba_timer_start_callback ' );
44
+ runkit_function_rename ('pinba_timer_stop ' , 'pinba_timer_stop_callback ' );
45
+
46
+ runkit_function_rename ('pinba_timer_start_bak ' , 'pinba_timer_start ' );
47
+ runkit_function_rename ('pinba_timer_stop_bak ' , 'pinba_timer_stop ' );
48
+ }
49
+
50
+ public function testTreeLog ()
51
+ {
52
+ PinbaClient::me ()->setTreeLogEnabled ();
53
+
54
+ $ this ->assertEquals (count (PinbaClient::me ()->getTreeQueue ()), 0 );
55
+
56
+ PinbaClient::me ()->timerStart (
57
+ 'test ' ,
58
+ array ("test " => "main " )
59
+ );
60
+
61
+ $ this ->assertEquals (count (PinbaClient::me ()->getTreeQueue ()), 1 );
62
+
63
+ PinbaClient::me ()->timerStart (
64
+ 'subtest ' ,
65
+ array ("test " => "submain " )
66
+ );
67
+
68
+ $ this ->assertEquals (count (PinbaClient::me ()->getTreeQueue ()), 2 );
69
+
70
+ PinbaClient::me ()->timerStop ('subtest ' );
71
+
72
+ $ this ->assertEquals (count (PinbaClient::me ()->getTreeQueue ()), 1 );
73
+
74
+ PinbaClient::me ()->timerStop ('test ' );
75
+
76
+ $ this ->assertEquals (count (PinbaClient::me ()->getTreeQueue ()), 0 );
77
+
78
+ }
79
+ }
80
+
81
+ final class RunkitCallback
82
+ {
83
+ public static $ queue = array ();
84
+ public static $ log = array ();
85
+
86
+ public static function start ($ tags , $ data = array ())
87
+ {
88
+ self ::$ log [] = $ tags ;
89
+ end (self ::$ log );
90
+
91
+ if (!empty ($ tags ['treeParentId ' ]) && $ tags ['treeParentId ' ] != "root " ) {
92
+ if ($ tags ['treeParentId ' ] != end (self ::$ queue )) {
93
+ throw new Exception ("Error generatin tree " );
94
+ }
95
+ }
96
+
97
+ if (!empty ($ tags ['treeId ' ])) {
98
+ self ::$ queue [] = $ tags ['treeId ' ];
99
+ }
100
+
101
+ return key (self ::$ log );
102
+ }
103
+
104
+ public static function stop ($ id )
105
+ {
106
+ $ current = self ::$ log [$ id ];
107
+ $ tree_id = $ current ['treeId ' ];
108
+
109
+ if (end (self ::$ queue ) != $ tree_id ) {
110
+ throw new Exception ("Error generatin tree " );
111
+ }
112
+
113
+ array_pop (self ::$ queue );
114
+ unset(self ::$ log [$ id ]);
115
+ }
116
+ }
117
+
118
+ function pinba_timer_start_callback ($ tags , $ data = array ()) {
119
+ return RunkitCallback::start ($ tags , $ data );
120
+ }
121
+
122
+ function pinba_timer_stop_callback ($ id ){
123
+ return RunkitCallback::stop ($ id );
124
+ }
125
+ ?>
0 commit comments