MediaWiki REL1_31
XhprofDataTest.php
Go to the documentation of this file.
1<?php
27class XhprofDataTest extends PHPUnit\Framework\TestCase {
28
29 use MediaWikiCoversValidator;
30
35 public function testSplitKey( $key, $expect ) {
36 $this->assertSame( $expect, XhprofData::splitKey( $key ) );
37 }
38
39 public function provideSplitKey() {
40 return [
41 [ 'main()', [ null, 'main()' ] ],
42 [ 'foo==>bar', [ 'foo', 'bar' ] ],
43 [ 'bar@1==>bar@2', [ 'bar@1', 'bar@2' ] ],
44 [ 'foo==>bar==>baz', [ 'foo', 'bar==>baz' ] ],
45 [ '==>bar', [ '', 'bar' ] ],
46 [ '', [ null, '' ] ],
47 ];
48 }
49
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->assertSame( 3, count( $raw ) );
62 }
63
73 $metricStruct = [
74 'ct' => 'int',
75 'wt' => 'array',
76 'cpu' => 'array',
77 'mu' => 'array',
78 'pmu' => 'array',
79 ];
80 $statStruct = [
81 'total' => 'numeric',
82 'min' => 'numeric',
83 'mean' => 'numeric',
84 'max' => 'numeric',
85 'variance' => 'numeric',
86 'percent' => 'numeric',
87 ];
88
89 $xhprofData = $this->getXhprofDataFixture();
90 $metrics = $xhprofData->getInclusiveMetrics();
91
92 foreach ( $metrics as $name => $metric ) {
93 $this->assertArrayStructure( $metricStruct, $metric );
94
95 foreach ( $metricStruct as $key => $type ) {
96 if ( $type === 'array' ) {
97 $this->assertArrayStructure( $statStruct, $metric[$key] );
98 if ( $name === 'main()' ) {
99 $this->assertEquals( 100, $metric[$key]['percent'] );
100 }
101 }
102 }
103 }
104 }
105
115 $metricStruct = [
116 'ct' => 'int',
117 'wt' => 'array',
118 'cpu' => 'array',
119 'mu' => 'array',
120 'pmu' => 'array',
121 'calls' => 'array',
122 'subcalls' => 'array',
123 ];
124 $statsMetrics = [ 'wt', 'cpu', 'mu', 'pmu' ];
125 $statStruct = [
126 'total' => 'numeric',
127 'min' => 'numeric',
128 'mean' => 'numeric',
129 'max' => 'numeric',
130 'variance' => 'numeric',
131 'percent' => 'numeric',
132 'exclusive' => 'numeric',
133 ];
134
135 $xhprofData = $this->getXhprofDataFixture();
136 $metrics = $xhprofData->getCompleteMetrics();
137
138 foreach ( $metrics as $name => $metric ) {
139 $this->assertArrayStructure( $metricStruct, $metric, $name );
140
141 foreach ( $metricStruct as $key => $type ) {
142 if ( in_array( $key, $statsMetrics ) ) {
144 $statStruct, $metric[$key], $key
145 );
146 $this->assertLessThanOrEqual(
147 $metric[$key]['total'], $metric[$key]['exclusive']
148 );
149 }
150 }
151 }
152 }
153
159 public function testEdges() {
160 $xhprofData = $this->getXhprofDataFixture();
161 $this->assertSame( [], $xhprofData->getCallers( 'main()' ) );
162 $this->assertSame( [ 'foo', 'xhprof_disable' ],
163 $xhprofData->getCallees( 'main()' )
164 );
165 $this->assertSame( [ 'main()' ],
166 $xhprofData->getCallers( 'foo' )
167 );
168 $this->assertSame( [], $xhprofData->getCallees( 'strlen' ) );
169 }
170
175 public function testCriticalPath() {
176 $xhprofData = $this->getXhprofDataFixture();
177 $path = $xhprofData->getCriticalPath();
178
179 $last = null;
180 foreach ( $path as $key => $value ) {
181 list( $func, $call ) = XhprofData::splitKey( $key );
182 $this->assertSame( $last, $func );
183 $last = $call;
184 }
185 $this->assertSame( $last, 'bar@1' );
186 }
187
216 protected function getXhprofDataFixture( array $opts = [] ) {
217 return new XhprofData( [
218 'foo==>bar' => [
219 'ct' => 2,
220 'wt' => 57,
221 'cpu' => 92,
222 'mu' => 1896,
223 'pmu' => 0,
224 ],
225 'foo==>strlen' => [
226 'ct' => 2,
227 'wt' => 21,
228 'cpu' => 141,
229 'mu' => 752,
230 'pmu' => 0,
231 ],
232 'bar==>bar@1' => [
233 'ct' => 1,
234 'wt' => 18,
235 'cpu' => 19,
236 'mu' => 752,
237 'pmu' => 0,
238 ],
239 'main()==>foo' => [
240 'ct' => 1,
241 'wt' => 304,
242 'cpu' => 307,
243 'mu' => 4008,
244 'pmu' => 0,
245 ],
246 'main()==>xhprof_disable' => [
247 'ct' => 1,
248 'wt' => 8,
249 'cpu' => 10,
250 'mu' => 768,
251 'pmu' => 392,
252 ],
253 'main()' => [
254 'ct' => 1,
255 'wt' => 353,
256 'cpu' => 351,
257 'mu' => 6112,
258 'pmu' => 1424,
259 ],
260 ], $opts );
261 }
262
270 protected function assertArrayStructure( $struct, $actual, $label = null ) {
271 $this->assertInternalType( 'array', $actual, $label );
272 $this->assertCount( count( $struct ), $actual, $label );
273 foreach ( $struct as $key => $type ) {
274 $this->assertArrayHasKey( $key, $actual );
275 $this->assertInternalType( $type, $actual[$key] );
276 }
277 }
278}
@uses XhprofData @uses AutoLoader
testEdges()
XhprofData::getCallers XhprofData::getCallees @uses XhprofData.
getXhprofDataFixture(array $opts=[])
Get an Xhprof instance that has been primed with a set of known testing data.
testCriticalPath()
XhprofData::getCriticalPath @uses XhprofData.
testSplitKey( $key, $expect)
XhprofData::splitKey provideSplitKey.
testInclude()
XhprofData::pruneData.
assertArrayStructure( $struct, $actual, $label=null)
Assert that the given array has the described structure.
testInclusiveMetricsStructure()
Validate the structure of data returned by Xhprof::getInclusiveMetrics().
testCompleteMetricsStructure()
Validate the structure of data returned by Xhprof::getCompleteMetrics().
Convenience class for working with XHProf profiling data https://github.com/phacility/xhprof.
static splitKey( $key)
Convert an xhprof data key into an array of ['parent', 'child'] function names.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
$last