Debian Codegraph @

tests/phpunit/unit/includes/libs/XhprofDataTest.php

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