|
| 1 | +--TEST-- |
| 2 | +XHPRrof: Test excluding call_user_func and similar functions |
| 3 | +Author: mpal |
| 4 | +--FILE-- |
| 5 | +<?php |
| 6 | + |
| 7 | +include_once dirname(__FILE__).'/common.php'; |
| 8 | + |
| 9 | +$xhprof_ignored_functions = array( 'ignored_functions' => |
| 10 | + array('call_user_func', |
| 11 | + 'call_user_func_array', |
| 12 | + 'my_call_user_func_safe', |
| 13 | + 'my_call_user_func_array_safe')); |
| 14 | +function bar() { |
| 15 | + return 1; |
| 16 | +} |
| 17 | + |
| 18 | +function foo($x) { |
| 19 | + $sum = 0; |
| 20 | + for ($idx = 0; $idx < 2; $idx++) { |
| 21 | + $sum += bar(); |
| 22 | + } |
| 23 | + echo "hello: {$x}\n" ; |
| 24 | + return strlen("hello: {$x}"); |
| 25 | +} |
| 26 | + |
| 27 | +function foo_array($x1, $x2 = 'test') { |
| 28 | + $sum = 0; |
| 29 | + $x = array($x1, $x2); |
| 30 | + foreach ($x as $idx) { |
| 31 | + $sum += bar(); |
| 32 | + } |
| 33 | + echo "hello: " . $x[0] . $x[1] . "\n"; |
| 34 | + return strlen("hello: {$x[0]} {$x[1]}"); |
| 35 | +} |
| 36 | + |
| 37 | +function my_call_user_func_safe($function, $args = 'my_safe') { |
| 38 | + if (!is_callable($function, true)) { |
| 39 | + throw new Exception('my_call_user_func_safe() invoked without ' . |
| 40 | + 'a valid callable.'); |
| 41 | + } |
| 42 | + |
| 43 | + call_user_func($function, array($args)); |
| 44 | +} |
| 45 | + |
| 46 | +function my_call_user_func_array_safe($function, $args = array()) { |
| 47 | + if (!is_callable($function, true)) { |
| 48 | + throw new Exception('my_call_user_func_array_safe() invoked without ' . |
| 49 | + 'a valid callable.'); |
| 50 | + } |
| 51 | + |
| 52 | + call_user_func_array($function, $args); |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +class test_call_user_func { |
| 57 | + function test_call_user_func($test_func = 'foo', |
| 58 | + $arg1 = 'user_func test') { |
| 59 | + call_user_func($test_func, $arg1); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function test_call_user_func_array($test_func = 'foo_array', |
| 64 | + $arg1 = array(0 => 'user_func_array', |
| 65 | + 'test')) { |
| 66 | + call_user_func_array($test_func, $arg1); |
| 67 | +} |
| 68 | + |
| 69 | +function test_my_call_user_func_safe($test_func = 'foo', |
| 70 | + $arg1 = 'my_user_func_safe test') { |
| 71 | + my_call_user_func_safe($test_func, $arg1); |
| 72 | +} |
| 73 | + |
| 74 | +function test_my_call_user_func_array_safe( |
| 75 | + $test_func = 'foo_array', |
| 76 | + $arg1 = array('my_user_func_array_safe', |
| 77 | + 'test')) { |
| 78 | + my_call_user_func_array_safe($test_func, $arg1); |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +// 1: Sanity test a simple profile run |
| 83 | +echo "Part 1: Default Flags\n"; |
| 84 | +xhprof_enable(0, $xhprof_ignored_functions); |
| 85 | +foo("this is a test"); |
| 86 | +$array_arg = array(); |
| 87 | +$array_arg[] = 'calling '; |
| 88 | +$array_arg[] = 'foo_array'; |
| 89 | +foo_array($array_arg); |
| 90 | + |
| 91 | +$output = xhprof_disable(); |
| 92 | +echo "Part 1 output:\n"; |
| 93 | +print_canonical($output); |
| 94 | +echo "\n"; |
| 95 | + |
| 96 | +// 2a: Sanity test ignoring call_user_func |
| 97 | +echo "Part 2a: Ignore call_user_func\n"; |
| 98 | +xhprof_enable(0, $xhprof_ignored_functions); |
| 99 | +$indirect_foo = new test_call_user_func('foo'); |
| 100 | +$output = xhprof_disable(); |
| 101 | +echo "Part 2a output:\n"; |
| 102 | +print_canonical($output); |
| 103 | +echo "\n"; |
| 104 | + |
| 105 | +// 2b: Confirm that profiling without parameters still works |
| 106 | +echo "Part 2b: Standard profile without parameters\n"; |
| 107 | +xhprof_enable(); |
| 108 | +$indirect_foo = new test_call_user_func('foo'); |
| 109 | +$output = xhprof_disable(); |
| 110 | +echo "Part 2b output:\n"; |
| 111 | +print_canonical($output); |
| 112 | +echo "\n"; |
| 113 | + |
| 114 | +// 2c: Confirm that empty array of ignored functions works |
| 115 | +echo "Part 2c: Standard profile with empty array of ignored functions\n"; |
| 116 | +xhprof_enable(0, array()); |
| 117 | +$indirect_foo = new test_call_user_func('foo'); |
| 118 | +$output = xhprof_disable(); |
| 119 | +echo "Part 2c output:\n"; |
| 120 | +print_canonical($output); |
| 121 | +echo "\n"; |
| 122 | + |
| 123 | +// 3: Sanity test ignoring call_user_func_array |
| 124 | +echo "Part 3: Ignore call_user_func_array\n"; |
| 125 | +xhprof_enable(XHPROF_FLAGS_CPU, $xhprof_ignored_functions); |
| 126 | +test_call_user_func_array('foo_array', $array_arg); |
| 127 | +$output = xhprof_disable(); |
| 128 | +echo "Part 3 output:\n"; |
| 129 | +print_canonical($output); |
| 130 | +echo "\n"; |
| 131 | + |
| 132 | +// 4: Sanity test ignoring my_call_user_func_safe |
| 133 | +echo "Part 4: Ignore my_call_user_func_safe\n"; |
| 134 | +xhprof_enable(0, $xhprof_ignored_functions); |
| 135 | +test_my_call_user_func_safe('foo'); |
| 136 | +$output = xhprof_disable(); |
| 137 | +echo "Part 4 output:\n"; |
| 138 | +print_canonical($output); |
| 139 | +echo "\n"; |
| 140 | + |
| 141 | +// 5a: Sanity test ignoring my_call_user_func_array_safe and strlen |
| 142 | +echo "Part 5a: Ignore my_call_user_func_array_safe and strlen\n"; |
| 143 | +$tmp1 = $xhprof_ignored_functions['ignored_functions']; |
| 144 | +$tmp1[] = 'strlen'; |
| 145 | +$ignore_strlen_also = array('ignored_functions' => $tmp1); |
| 146 | +xhprof_enable(XHPROF_FLAGS_MEMORY, $ignore_strlen_also); |
| 147 | +test_my_call_user_func_array_safe('foo_array'); |
| 148 | +$output = xhprof_disable(); |
| 149 | +echo "Part 5a output:\n"; |
| 150 | +print_canonical($output); |
| 151 | +echo "\n"; |
| 152 | + |
| 153 | +// 5b: Sanity test to not ignore call_user_func variants |
| 154 | +echo "Part 5b: Profile call_user_func_array and my_call_user_func_array_safe\n"; |
| 155 | +xhprof_enable(XHPROF_FLAGS_MEMORY, array()); |
| 156 | +test_my_call_user_func_array_safe('foo_array'); |
| 157 | +$output = xhprof_disable(); |
| 158 | +echo "Part 5b output:\n"; |
| 159 | +print_canonical($output); |
| 160 | +echo "\n"; |
| 161 | + |
| 162 | +// 5c: Sanity test to only ignore my_call_user_func_array_safe |
| 163 | +echo "Part 5c: Only ignore call_user_func_array\n"; |
| 164 | +$xhprof_ignored_functions = array('ignored_functions' => |
| 165 | + 'my_call_user_func_array_safe'); |
| 166 | +xhprof_enable(XHPROF_FLAGS_MEMORY, $xhprof_ignored_functions); |
| 167 | +test_my_call_user_func_array_safe('foo_array'); |
| 168 | +$output = xhprof_disable(); |
| 169 | +echo "Part 5c output:\n"; |
| 170 | +print_canonical($output); |
| 171 | +echo "\n"; |
| 172 | + |
| 173 | +?> |
| 174 | +--EXPECT-- |
| 175 | +Part 1: Default Flags |
| 176 | +hello: this is a test |
| 177 | +hello: Arraytest |
| 178 | +Part 1 output: |
| 179 | +foo==>bar : ct= 2; wt=*; |
| 180 | +foo==>strlen : ct= 1; wt=*; |
| 181 | +foo_array==>bar : ct= 2; wt=*; |
| 182 | +foo_array==>strlen : ct= 1; wt=*; |
| 183 | +main() : ct= 1; wt=*; |
| 184 | +main()==>foo : ct= 1; wt=*; |
| 185 | +main()==>foo_array : ct= 1; wt=*; |
| 186 | +main()==>xhprof_disable : ct= 1; wt=*; |
| 187 | + |
| 188 | +Part 2a: Ignore call_user_func |
| 189 | +hello: user_func test |
| 190 | +Part 2a output: |
| 191 | +foo==>bar : ct= 2; wt=*; |
| 192 | +foo==>strlen : ct= 1; wt=*; |
| 193 | +main() : ct= 1; wt=*; |
| 194 | +main()==>test_call_user_func::test_call_user_func: ct= 1; wt=*; |
| 195 | +main()==>xhprof_disable : ct= 1; wt=*; |
| 196 | +test_call_user_func::test_call_user_func==>foo: ct= 1; wt=*; |
| 197 | + |
| 198 | +Part 2b: Standard profile without parameters |
| 199 | +hello: user_func test |
| 200 | +Part 2b output: |
| 201 | +call_user_func==>foo : ct= 1; wt=*; |
| 202 | +foo==>bar : ct= 2; wt=*; |
| 203 | +foo==>strlen : ct= 1; wt=*; |
| 204 | +main() : ct= 1; wt=*; |
| 205 | +main()==>test_call_user_func::test_call_user_func: ct= 1; wt=*; |
| 206 | +main()==>xhprof_disable : ct= 1; wt=*; |
| 207 | +test_call_user_func::test_call_user_func==>call_user_func: ct= 1; wt=*; |
| 208 | + |
| 209 | +Part 2c: Standard profile with empty array of ignored functions |
| 210 | +hello: user_func test |
| 211 | +Part 2c output: |
| 212 | +call_user_func==>foo : ct= 1; wt=*; |
| 213 | +foo==>bar : ct= 2; wt=*; |
| 214 | +foo==>strlen : ct= 1; wt=*; |
| 215 | +main() : ct= 1; wt=*; |
| 216 | +main()==>test_call_user_func::test_call_user_func: ct= 1; wt=*; |
| 217 | +main()==>xhprof_disable : ct= 1; wt=*; |
| 218 | +test_call_user_func::test_call_user_func==>call_user_func: ct= 1; wt=*; |
| 219 | + |
| 220 | +Part 3: Ignore call_user_func_array |
| 221 | +hello: calling foo_array |
| 222 | +Part 3 output: |
| 223 | +foo_array==>bar : cpu=*; ct= 2; wt=*; |
| 224 | +foo_array==>strlen : cpu=*; ct= 1; wt=*; |
| 225 | +main() : cpu=*; ct= 1; wt=*; |
| 226 | +main()==>test_call_user_func_array : cpu=*; ct= 1; wt=*; |
| 227 | +main()==>xhprof_disable : cpu=*; ct= 1; wt=*; |
| 228 | +test_call_user_func_array==>foo_array : cpu=*; ct= 1; wt=*; |
| 229 | + |
| 230 | +Part 4: Ignore my_call_user_func_safe |
| 231 | +hello: Array |
| 232 | +Part 4 output: |
| 233 | +foo==>bar : ct= 2; wt=*; |
| 234 | +foo==>strlen : ct= 1; wt=*; |
| 235 | +main() : ct= 1; wt=*; |
| 236 | +main()==>test_my_call_user_func_safe : ct= 1; wt=*; |
| 237 | +main()==>xhprof_disable : ct= 1; wt=*; |
| 238 | +test_my_call_user_func_safe==>foo : ct= 1; wt=*; |
| 239 | +test_my_call_user_func_safe==>is_callable: ct= 1; wt=*; |
| 240 | + |
| 241 | +Part 5a: Ignore my_call_user_func_array_safe and strlen |
| 242 | +hello: my_user_func_array_safetest |
| 243 | +Part 5a output: |
| 244 | +foo_array==>bar : ct= 2; mu=*; pmu=*; wt=*; |
| 245 | +main() : ct= 1; mu=*; pmu=*; wt=*; |
| 246 | +main()==>test_my_call_user_func_array_safe: ct= 1; mu=*; pmu=*; wt=*; |
| 247 | +main()==>xhprof_disable : ct= 1; mu=*; pmu=*; wt=*; |
| 248 | +test_my_call_user_func_array_safe==>foo_array: ct= 1; mu=*; pmu=*; wt=*; |
| 249 | +test_my_call_user_func_array_safe==>is_callable: ct= 1; mu=*; pmu=*; wt=*; |
| 250 | + |
| 251 | +Part 5b: Profile call_user_func_array and my_call_user_func_array_safe |
| 252 | +hello: my_user_func_array_safetest |
| 253 | +Part 5b output: |
| 254 | +call_user_func_array==>foo_array : ct= 1; mu=*; pmu=*; wt=*; |
| 255 | +foo_array==>bar : ct= 2; mu=*; pmu=*; wt=*; |
| 256 | +foo_array==>strlen : ct= 1; mu=*; pmu=*; wt=*; |
| 257 | +main() : ct= 1; mu=*; pmu=*; wt=*; |
| 258 | +main()==>test_my_call_user_func_array_safe: ct= 1; mu=*; pmu=*; wt=*; |
| 259 | +main()==>xhprof_disable : ct= 1; mu=*; pmu=*; wt=*; |
| 260 | +my_call_user_func_array_safe==>call_user_func_array: ct= 1; mu=*; pmu=*; wt=*; |
| 261 | +my_call_user_func_array_safe==>is_callable: ct= 1; mu=*; pmu=*; wt=*; |
| 262 | +test_my_call_user_func_array_safe==>my_call_user_func_array_safe: ct= 1; mu=*; pmu=*; wt=*; |
| 263 | + |
| 264 | +Part 5c: Only ignore call_user_func_array |
| 265 | +hello: my_user_func_array_safetest |
| 266 | +Part 5c output: |
| 267 | +call_user_func_array==>foo_array : ct= 1; mu=*; pmu=*; wt=*; |
| 268 | +foo_array==>bar : ct= 2; mu=*; pmu=*; wt=*; |
| 269 | +foo_array==>strlen : ct= 1; mu=*; pmu=*; wt=*; |
| 270 | +main() : ct= 1; mu=*; pmu=*; wt=*; |
| 271 | +main()==>test_my_call_user_func_array_safe: ct= 1; mu=*; pmu=*; wt=*; |
| 272 | +main()==>xhprof_disable : ct= 1; mu=*; pmu=*; wt=*; |
| 273 | +test_my_call_user_func_array_safe==>call_user_func_array: ct= 1; mu=*; pmu=*; wt=*; |
| 274 | +test_my_call_user_func_array_safe==>is_callable: ct= 1; mu=*; pmu=*; wt=*; |
0 commit comments