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.
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.
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
24 * @copyright © 2014 Wikimedia Foundation and contributors
27 class XhprofDataTest
extends PHPUnit\Framework\TestCase
{
29 use MediaWikiCoversValidator
;
32 * @covers XhprofData::splitKey
33 * @dataProvider provideSplitKey
35 public function testSplitKey( $key, $expect ) {
36 $this->assertSame( $expect, XhprofData
::splitKey( $key ) );
39 public function provideSplitKey() {
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' ] ],
51 * @covers XhprofData::pruneData
53 public function testInclude() {
54 $xhprofData = $this->getXhprofDataFixture( [
55 'include' => [ 'main()' ],
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 ) );
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.
70 * @covers XhprofData::getInclusiveMetrics
72 public function testInclusiveMetricsStructure() {
85 'variance' => 'numeric',
86 'percent' => 'numeric',
89 $xhprofData = $this->getXhprofDataFixture();
90 $metrics = $xhprofData->getInclusiveMetrics();
92 foreach ( $metrics as $name => $metric ) {
93 $this->assertArrayStructure( $metricStruct, $metric );
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'] );
107 * Validate the structure of data returned by
108 * Xhprof::getCompleteMetrics(). This acts as a guard against unexpected
109 * structural changes to the returned data in lieu of using a more heavy
110 * weight typed response object.
112 * @covers XhprofData::getCompleteMetrics
114 public function testCompleteMetricsStructure() {
122 'subcalls' => 'array',
124 $statsMetrics = [ 'wt', 'cpu', 'mu', 'pmu' ];
126 'total' => 'numeric',
130 'variance' => 'numeric',
131 'percent' => 'numeric',
132 'exclusive' => 'numeric',
135 $xhprofData = $this->getXhprofDataFixture();
136 $metrics = $xhprofData->getCompleteMetrics();
138 foreach ( $metrics as $name => $metric ) {
139 $this->assertArrayStructure( $metricStruct, $metric, $name );
141 foreach ( $metricStruct as $key => $type ) {
142 if ( in_array( $key, $statsMetrics ) ) {
143 $this->assertArrayStructure(
144 $statStruct, $metric[$key], $key
146 $this->assertLessThanOrEqual(
147 $metric[$key]['total'], $metric[$key]['exclusive']
155 * @covers XhprofData::getCallers
156 * @covers XhprofData::getCallees
159 public function testEdges() {
160 $xhprofData = $this->getXhprofDataFixture();
161 $this->assertSame( [], $xhprofData->getCallers( 'main()' ) );
162 $this->assertSame( [ 'foo', 'xhprof_disable' ],
163 $xhprofData->getCallees( 'main()' )
165 $this->assertSame( [ 'main()' ],
166 $xhprofData->getCallers( 'foo' )
168 $this->assertSame( [], $xhprofData->getCallees( 'strlen' ) );
172 * @covers XhprofData::getCriticalPath
175 public function testCriticalPath() {
176 $xhprofData = $this->getXhprofDataFixture();
177 $path = $xhprofData->getCriticalPath();
180 foreach ( $path as $key => $value ) {
181 list( $func, $call ) = XhprofData
::splitKey( $key );
182 $this->assertSame( $last, $func );
185 $this->assertSame( $last, 'bar@1' );
189 * Get an Xhprof instance that has been primed with a set of known testing
190 * data. Tests for the Xhprof class should laregly be concerned with
191 * evaluating the manipulations of the data collected by xhprof rather
192 * than the data collection process itself.
194 * The returned Xhprof instance primed will be with a data set created by
195 * running this trivial program using the PECL xhprof implementation:
197 * function bar( $x ) {
203 * for ( $idx = 0; $idx < 2; $idx++ ) {
205 * $x = strlen( 'abc' );
208 * xhprof_enable( XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY );
210 * $x = xhprof_disable();
216 protected function getXhprofDataFixture( array $opts = [] ) {
217 return new XhprofData( [
246 'main()==>xhprof_disable' => [
264 * Assert that the given array has the described structure.
266 * @param array $struct Array of key => type mappings
267 * @param array $actual Array to check
268 * @param string $label
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] );