MediaWiki  REL1_31
XhprofDataTest.php
Go to the documentation of this file.
1 <?php
27 class 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 
72  public function testInclusiveMetricsStructure() {
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 
114  public function testCompleteMetricsStructure() {
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 ) ) {
143  $this->assertArrayStructure(
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 }
XhprofData\splitKey
static splitKey( $key)
Convert an xhprof data key into an array of ['parent', 'child'] function names.
Definition: XhprofData.php:113
XhprofDataTest
@uses XhprofData @uses AutoLoader
Definition: XhprofDataTest.php:27
XhprofDataTest\testCriticalPath
testCriticalPath()
XhprofData::getCriticalPath @uses XhprofData.
Definition: XhprofDataTest.php:175
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
array
the array() calling protocol came about after MediaWiki 1.4rc1.
$last
$last
Definition: profileinfo.php:408
XhprofDataTest\testInclusiveMetricsStructure
testInclusiveMetricsStructure()
Validate the structure of data returned by Xhprof::getInclusiveMetrics().
Definition: XhprofDataTest.php:72
XhprofDataTest\provideSplitKey
provideSplitKey()
Definition: XhprofDataTest.php:39
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
XhprofDataTest\getXhprofDataFixture
getXhprofDataFixture(array $opts=[])
Get an Xhprof instance that has been primed with a set of known testing data.
Definition: XhprofDataTest.php:216
XhprofDataTest\testInclude
testInclude()
XhprofData::pruneData.
Definition: XhprofDataTest.php:53
list
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
XhprofDataTest\assertArrayStructure
assertArrayStructure( $struct, $actual, $label=null)
Assert that the given array has the described structure.
Definition: XhprofDataTest.php:270
$value
$value
Definition: styleTest.css.php:45
XhprofDataTest\testCompleteMetricsStructure
testCompleteMetricsStructure()
Validate the structure of data returned by Xhprof::getCompleteMetrics().
Definition: XhprofDataTest.php:114
XhprofDataTest\testEdges
testEdges()
XhprofData::getCallers XhprofData::getCallees @uses XhprofData.
Definition: XhprofDataTest.php:159
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
XhprofData
Convenience class for working with XHProf profiling data https://github.com/phacility/xhprof.
Definition: XhprofData.php:31
$path
$path
Definition: NoLocalSettings.php:25
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
XhprofDataTest\testSplitKey
testSplitKey( $key, $expect)
XhprofData::splitKey provideSplitKey.
Definition: XhprofDataTest.php:35
$type
$type
Definition: testCompression.php:48