Enable profiling via xhprof
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / XhprofTest.php
1 <?php
2 /**
3 * @section LICENSE
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22 /**
23 * @uses Xhprof
24 * @uses AutoLoader
25 * @author Bryan Davis <bd808@wikimedia.org>
26 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
27 * @since 1.25
28 */
29 class XhprofTest extends PHPUnit_Framework_TestCase {
30
31 public function setUp() {
32 if ( !function_exists( 'xhprof_enable' ) ) {
33 $this->markTestSkipped( 'No xhprof support detected.' );
34 }
35 }
36
37 /**
38 * @covers Xhprof::splitKey
39 * @dataProvider provideSplitKey
40 */
41 public function testSplitKey( $key, $expect ) {
42 $this->assertSame( $expect, Xhprof::splitKey( $key ) );
43 }
44
45 public function provideSplitKey() {
46 return array(
47 array( 'main()', array( null, 'main()' ) ),
48 array( 'foo==>bar', array( 'foo', 'bar' ) ),
49 array( 'bar@1==>bar@2', array( 'bar@1', 'bar@2' ) ),
50 array( 'foo==>bar==>baz', array( 'foo', 'bar==>baz' ) ),
51 array( '==>bar', array( '', 'bar' ) ),
52 array( '', array( null, '' ) ),
53 );
54 }
55
56 /**
57 * @covers Xhprof::__construct
58 * @covers Xhprof::stop
59 * @covers Xhprof::getRawData
60 * @dataProvider provideRawData
61 */
62 public function testRawData( $flags, $keys ) {
63 $xhprof = new Xhprof( array( 'flags' => $flags ) );
64 $raw = $xhprof->getRawData();
65 $this->assertArrayHasKey( 'main()', $raw );
66 foreach ( $keys as $key ) {
67 $this->assertArrayHasKey( $key, $raw['main()'] );
68 }
69 }
70
71 public function provideRawData() {
72 return array(
73 array( 0, array( 'ct', 'wt' ) ),
74 array( XHPROF_FLAGS_MEMORY, array( 'ct', 'wt', 'mu', 'pmu' ) ),
75 array( XHPROF_FLAGS_CPU, array( 'ct', 'wt', 'cpu' ) ),
76 array( XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU, array(
77 'ct', 'wt', 'mu', 'pmu', 'cpu',
78 ) ),
79 );
80 }
81
82 /**
83 * @covers Xhprof::pruneData
84 */
85 public function testInclude() {
86 $xhprof = $this->getXhprofFixture( array(
87 'include' => array( 'main()' ),
88 ) );
89 $raw = $xhprof->getRawData();
90 $this->assertArrayHasKey( 'main()', $raw );
91 $this->assertArrayHasKey( 'main()==>foo', $raw );
92 $this->assertArrayHasKey( 'main()==>xhprof_disable', $raw );
93 $this->assertSame( 3, count( $raw ) );
94 }
95
96 /**
97 * Validate the structure of data returned by
98 * Xhprof::getInclusiveMetrics(). This acts as a guard against unexpected
99 * structural changes to the returned data in lieu of using a more heavy
100 * weight typed response object.
101 *
102 * @covers Xhprof::getInclusiveMetrics
103 */
104 public function testInclusiveMetricsStructure() {
105 $metricStruct = array(
106 'ct' => 'int',
107 'wt' => 'array',
108 'cpu' => 'array',
109 'mu' => 'array',
110 'pmu' => 'array',
111 );
112 $statStruct = array(
113 'total' => 'numeric',
114 'min' => 'numeric',
115 'mean' => 'numeric',
116 'max' => 'numeric',
117 'variance' => 'numeric',
118 'percent' => 'numeric',
119 );
120
121 $xhprof = $this->getXhprofFixture();
122 $metrics = $xhprof->getInclusiveMetrics();
123
124 foreach ( $metrics as $name => $metric ) {
125 $this->assertArrayStructure( $metricStruct, $metric );
126
127 foreach ( $metricStruct as $key => $type ) {
128 if ( $type === 'array' ) {
129 $this->assertArrayStructure( $statStruct, $metric[$key] );
130 if ( $name === 'main()' ) {
131 $this->assertEquals( 100, $metric[$key]['percent'] );
132 }
133 }
134 }
135 }
136 }
137
138 /**
139 * Validate the structure of data returned by
140 * Xhprof::getCompleteMetrics(). This acts as a guard against unexpected
141 * structural changes to the returned data in lieu of using a more heavy
142 * weight typed response object.
143 *
144 * @covers Xhprof::getCompleteMetrics
145 */
146 public function testCompleteMetricsStructure() {
147 $metricStruct = array(
148 'ct' => 'int',
149 'wt' => 'array',
150 'cpu' => 'array',
151 'mu' => 'array',
152 'pmu' => 'array',
153 'calls' => 'array',
154 'subcalls' => 'array',
155 );
156 $statsMetrics = array( 'wt', 'cpu', 'mu', 'pmu' );
157 $statStruct = array(
158 'total' => 'numeric',
159 'min' => 'numeric',
160 'mean' => 'numeric',
161 'max' => 'numeric',
162 'variance' => 'numeric',
163 'percent' => 'numeric',
164 'exclusive' => 'numeric',
165 );
166
167 $xhprof = $this->getXhprofFixture();
168 $metrics = $xhprof->getCompleteMetrics();
169
170 foreach ( $metrics as $name => $metric ) {
171 $this->assertArrayStructure( $metricStruct, $metric, $name );
172
173 foreach ( $metricStruct as $key => $type ) {
174 if ( in_array( $key, $statsMetrics ) ) {
175 $this->assertArrayStructure(
176 $statStruct, $metric[$key], $key
177 );
178 $this->assertLessThanOrEqual(
179 $metric[$key]['total'], $metric[$key]['exclusive']
180 );
181 }
182 }
183 }
184 }
185
186 /**
187 * @covers Xhprof::getCallers
188 * @covers Xhprof::getCallees
189 * @uses Xhprof
190 */
191 public function testEdges() {
192 $xhprof = $this->getXhprofFixture();
193 $this->assertSame( array(), $xhprof->getCallers( 'main()' ) );
194 $this->assertSame( array( 'foo', 'xhprof_disable' ),
195 $xhprof->getCallees( 'main()' )
196 );
197 $this->assertSame( array( 'main()' ),
198 $xhprof->getCallers( 'foo' )
199 );
200 $this->assertSame( array(), $xhprof->getCallees( 'strlen' ) );
201 }
202
203 /**
204 * @covers Xhprof::getCriticalPath
205 * @uses Xhprof
206 */
207 public function testCriticalPath() {
208 $xhprof = $this->getXhprofFixture();
209 $path = $xhprof->getCriticalPath();
210
211 $last = null;
212 foreach ( $path as $key => $value ) {
213 list( $func, $call ) = Xhprof::splitKey( $key );
214 $this->assertSame( $last, $func );
215 $last = $call;
216 }
217 $this->assertSame( $last, 'bar@1' );
218 }
219
220 /**
221 * Get an Xhprof instance that has been primed with a set of known testing
222 * data. Tests for the Xhprof class should laregly be concerned with
223 * evaluating the manipulations of the data collected by xhprof rather
224 * than the data collection process itself.
225 *
226 * The returned Xhprof instance primed will be with a data set created by
227 * running this trivial program using the PECL xhprof implementation:
228 * @code
229 * function bar( $x ) {
230 * if ( $x > 0 ) {
231 * bar($x - 1);
232 * }
233 * }
234 * function foo() {
235 * for ( $idx = 0; $idx < 2; $idx++ ) {
236 * bar( $idx );
237 * $x = strlen( 'abc' );
238 * }
239 * }
240 * xhprof_enable( XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY );
241 * foo();
242 * $x = xhprof_disable();
243 * var_export( $x );
244 * @endcode
245 *
246 * @return Xhprof
247 */
248 protected function getXhprofFixture( array $opts = array() ) {
249 $xhprof = new Xhprof( $opts );
250 $xhprof->loadRawData( array (
251 'foo==>bar' => array (
252 'ct' => 2,
253 'wt' => 57,
254 'cpu' => 92,
255 'mu' => 1896,
256 'pmu' => 0,
257 ),
258 'foo==>strlen' => array (
259 'ct' => 2,
260 'wt' => 21,
261 'cpu' => 141,
262 'mu' => 752,
263 'pmu' => 0,
264 ),
265 'bar==>bar@1' => array (
266 'ct' => 1,
267 'wt' => 18,
268 'cpu' => 19,
269 'mu' => 752,
270 'pmu' => 0,
271 ),
272 'main()==>foo' => array (
273 'ct' => 1,
274 'wt' => 304,
275 'cpu' => 307,
276 'mu' => 4008,
277 'pmu' => 0,
278 ),
279 'main()==>xhprof_disable' => array (
280 'ct' => 1,
281 'wt' => 8,
282 'cpu' => 10,
283 'mu' => 768,
284 'pmu' => 392,
285 ),
286 'main()' => array (
287 'ct' => 1,
288 'wt' => 353,
289 'cpu' => 351,
290 'mu' => 6112,
291 'pmu' => 1424,
292 ),
293 ) );
294 return $xhprof;
295 }
296
297 /**
298 * Assert that the given array has the described structure.
299 *
300 * @param array $struct Array of key => type mappings
301 * @param array $actual Array to check
302 * @param string $label
303 */
304 protected function assertArrayStructure( $struct, $actual, $label = null ) {
305 $this->assertInternalType( 'array', $actual, $label );
306 $this->assertCount( count($struct), $actual, $label );
307 foreach ( $struct as $key => $type ) {
308 $this->assertArrayHasKey( $key, $actual );
309 $this->assertInternalType( $type, $actual[$key] );
310 }
311 }
312 }