Separate MediaWiki unit and integration tests
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / libs / XhprofDataTest.php
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 /**
22 * @copyright © 2014 Wikimedia Foundation and contributors
23 * @since 1.25
24 */
25 class XhprofDataTest extends PHPUnit\Framework\TestCase {
26
27 use MediaWikiCoversValidator;
28
29 /**
30 * @covers XhprofData::splitKey
31 * @dataProvider provideSplitKey
32 */
33 public function testSplitKey( $key, $expect ) {
34 $this->assertSame( $expect, XhprofData::splitKey( $key ) );
35 }
36
37 public function provideSplitKey() {
38 return [
39 [ 'main()', [ null, 'main()' ] ],
40 [ 'foo==>bar', [ 'foo', 'bar' ] ],
41 [ 'bar@1==>bar@2', [ 'bar@1', 'bar@2' ] ],
42 [ 'foo==>bar==>baz', [ 'foo', 'bar==>baz' ] ],
43 [ '==>bar', [ '', 'bar' ] ],
44 [ '', [ null, '' ] ],
45 ];
46 }
47
48 /**
49 * @covers XhprofData::pruneData
50 */
51 public function testInclude() {
52 $xhprofData = $this->getXhprofDataFixture( [
53 'include' => [ 'main()' ],
54 ] );
55 $raw = $xhprofData->getRawData();
56 $this->assertArrayHasKey( 'main()', $raw );
57 $this->assertArrayHasKey( 'main()==>foo', $raw );
58 $this->assertArrayHasKey( 'main()==>xhprof_disable', $raw );
59 $this->assertSame( 3, count( $raw ) );
60 }
61
62 /**
63 * Validate the structure of data returned by
64 * Xhprof::getInclusiveMetrics(). This acts as a guard against unexpected
65 * structural changes to the returned data in lieu of using a more heavy
66 * weight typed response object.
67 *
68 * @covers XhprofData::getInclusiveMetrics
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 * @covers XhprofData::getCompleteMetrics
111 */
112 public function testCompleteMetricsStructure() {
113 $metricStruct = [
114 'ct' => 'int',
115 'wt' => 'array',
116 'cpu' => 'array',
117 'mu' => 'array',
118 'pmu' => 'array',
119 'calls' => 'array',
120 'subcalls' => 'array',
121 ];
122 $statsMetrics = [ 'wt', 'cpu', 'mu', 'pmu' ];
123 $statStruct = [
124 'total' => 'numeric',
125 'min' => 'numeric',
126 'mean' => 'numeric',
127 'max' => 'numeric',
128 'variance' => 'numeric',
129 'percent' => 'numeric',
130 'exclusive' => 'numeric',
131 ];
132
133 $xhprofData = $this->getXhprofDataFixture();
134 $metrics = $xhprofData->getCompleteMetrics();
135
136 foreach ( $metrics as $name => $metric ) {
137 $this->assertArrayStructure( $metricStruct, $metric, $name );
138
139 foreach ( $metricStruct as $key => $type ) {
140 if ( in_array( $key, $statsMetrics ) ) {
141 $this->assertArrayStructure(
142 $statStruct, $metric[$key], $key
143 );
144 $this->assertLessThanOrEqual(
145 $metric[$key]['total'], $metric[$key]['exclusive']
146 );
147 }
148 }
149 }
150 }
151
152 /**
153 * @covers XhprofData::getCallers
154 * @covers XhprofData::getCallees
155 */
156 public function testEdges() {
157 $xhprofData = $this->getXhprofDataFixture();
158 $this->assertSame( [], $xhprofData->getCallers( 'main()' ) );
159 $this->assertSame( [ 'foo', 'xhprof_disable' ],
160 $xhprofData->getCallees( 'main()' )
161 );
162 $this->assertSame( [ 'main()' ],
163 $xhprofData->getCallers( 'foo' )
164 );
165 $this->assertSame( [], $xhprofData->getCallees( 'strlen' ) );
166 }
167
168 /**
169 * @covers XhprofData::getCriticalPath
170 */
171 public function testCriticalPath() {
172 $xhprofData = $this->getXhprofDataFixture();
173 $path = $xhprofData->getCriticalPath();
174
175 $last = null;
176 foreach ( $path as $key => $value ) {
177 list( $func, $call ) = XhprofData::splitKey( $key );
178 $this->assertSame( $last, $func );
179 $last = $call;
180 }
181 $this->assertSame( $last, 'bar@1' );
182 }
183
184 /**
185 * Get an Xhprof instance that has been primed with a set of known testing
186 * data. Tests for the Xhprof class should laregly be concerned with
187 * evaluating the manipulations of the data collected by xhprof rather
188 * than the data collection process itself.
189 *
190 * The returned Xhprof instance primed will be with a data set created by
191 * running this trivial program using the PECL xhprof implementation:
192 * @code
193 * function bar( $x ) {
194 * if ( $x > 0 ) {
195 * bar($x - 1);
196 * }
197 * }
198 * function foo() {
199 * for ( $idx = 0; $idx < 2; $idx++ ) {
200 * bar( $idx );
201 * $x = strlen( 'abc' );
202 * }
203 * }
204 * xhprof_enable( XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY );
205 * foo();
206 * $x = xhprof_disable();
207 * var_export( $x );
208 * @endcode
209 *
210 * @return Xhprof
211 */
212 protected function getXhprofDataFixture( array $opts = [] ) {
213 return new XhprofData( [
214 'foo==>bar' => [
215 'ct' => 2,
216 'wt' => 57,
217 'cpu' => 92,
218 'mu' => 1896,
219 'pmu' => 0,
220 ],
221 'foo==>strlen' => [
222 'ct' => 2,
223 'wt' => 21,
224 'cpu' => 141,
225 'mu' => 752,
226 'pmu' => 0,
227 ],
228 'bar==>bar@1' => [
229 'ct' => 1,
230 'wt' => 18,
231 'cpu' => 19,
232 'mu' => 752,
233 'pmu' => 0,
234 ],
235 'main()==>foo' => [
236 'ct' => 1,
237 'wt' => 304,
238 'cpu' => 307,
239 'mu' => 4008,
240 'pmu' => 0,
241 ],
242 'main()==>xhprof_disable' => [
243 'ct' => 1,
244 'wt' => 8,
245 'cpu' => 10,
246 'mu' => 768,
247 'pmu' => 392,
248 ],
249 'main()' => [
250 'ct' => 1,
251 'wt' => 353,
252 'cpu' => 351,
253 'mu' => 6112,
254 'pmu' => 1424,
255 ],
256 ], $opts );
257 }
258
259 /**
260 * Assert that the given array has the described structure.
261 *
262 * @param array $struct Array of key => type mappings
263 * @param array $actual Array to check
264 * @param string $label
265 */
266 protected function assertArrayStructure( $struct, $actual, $label = null ) {
267 $this->assertInternalType( 'array', $actual, $label );
268 $this->assertCount( count( $struct ), $actual, $label );
269 foreach ( $struct as $key => $type ) {
270 $this->assertArrayHasKey( $key, $actual );
271 $this->assertInternalType( $type, $actual[$key] );
272 }
273 }
274 }