Skip to content

Commit 292d183

Browse files
author
kannan
committed
add support to ignore specified functions during profiling; more tests
git-svn-id: https://svn.php.net/repository/pecl/xhprof/trunk@281203 c90b9560-bf6c-de11-be94-00142212c4b1
1 parent a693abf commit 292d183

File tree

8 files changed

+757
-49
lines changed

8 files changed

+757
-49
lines changed

CHANGELOG

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
Modification History:
22

3+
*** NOTE ***
4+
DO NOT UPDATE THIS FILE. UPDATE package.xml INSTEAD.
5+
This file contains the CHANGELOG for the initial release. For subsequent
6+
releases, the CHANGLELOG is maintained in the package.xml file itself.
7+
Please edit package.xml instead.
8+
************
9+
310
03/02/2008 kannan, checkin xhprof_html/ and xhprof_lib/ directories.
411
cjiang [These contain PHP sources for the UI as well
512
as various supporting libraries to compute

extension/tests/xhprof_005.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Author: Kannan
88
// Some coarse grained sanity tests for the time just
99
// to make sure profiler's timer implementation isn't
1010
// way off.
11-
// The test allows for gives a 25% margin of error.
11+
// The test allows for a 25% margin of error.
1212
//
1313

1414
include_once dirname(__FILE__).'/common.php';

extension/tests/xhprof_006.phpt

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
--TEST--
2+
XHPRrof: Basic Sampling Test
3+
Author: mpal
4+
--FILE--
5+
<?php
6+
7+
include_once dirname(__FILE__).'/common.php';
8+
9+
function individual_strings($limit) {
10+
$sum = 0;
11+
$t1 = microtime(true);
12+
$half = $limit/2;
13+
for ($idx = 0; $idx < $limit; $idx++) {
14+
$query = 'SELECT ';
15+
if ($idx < $half) {
16+
$query .= 'first_half';
17+
} else {
18+
$query .= 'second_half';
19+
}
20+
$query .= ' FROM database';
21+
$sum += strlen($query);
22+
}
23+
$t2 = microtime(true);
24+
$delta = $t2 - $t1;
25+
return $delta;
26+
}
27+
28+
function folded_strings($limit) {
29+
$sum = 0;
30+
$t1 = microtime(true);
31+
$half = $limit/2;
32+
for ($idx = 0; $idx < $limit; $idx++) {
33+
if ($idx < $half) {
34+
$query = 'SELECT first_half FROM database';
35+
} else {
36+
$query = 'SELECT second_half FROM database';
37+
}
38+
$sum += strlen($query);
39+
}
40+
$t2 = microtime(true);
41+
$delta = $t2 - $t1;
42+
return $delta;
43+
}
44+
45+
function foo($x) {
46+
$time_individual = individual_strings($x);
47+
$time_folded = folded_strings($x);
48+
$time_total = $time_individual + $time_folded;
49+
$percent_individual = ($time_individual / $time_total) * 100;
50+
$percent_folded = ($time_folded / $time_total) * 100;
51+
echo "Test Case : Percent of Total Time\n";
52+
echo "Individual :" /* . " $percent_individual" */ . "\n";
53+
echo "Folded :" /* . " $percent_folded" */ . "\n";
54+
return strlen("hello: {$x}");
55+
}
56+
57+
// 1: Sanity test a sampling profile run
58+
echo "Part 1: Sampling Profile\n";
59+
xhprof_sample_enable();
60+
foo(5000);
61+
$output = xhprof_sample_disable();
62+
63+
echo "Part 1: output\n";
64+
echo "In general, sampling output depends upon execution speed.\n";
65+
echo "Currently checking that this runs to completion.\n";
66+
// print_r($output);
67+
echo "\n";
68+
69+
?>
70+
--EXPECT--
71+
Part 1: Sampling Profile
72+
Test Case : Percent of Total Time
73+
Individual :
74+
Folded :
75+
Part 1: output
76+
In general, sampling output depends upon execution speed.
77+
Currently checking that this runs to completion.
78+

extension/tests/xhprof_007.phpt

+274
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
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

Comments
 (0)