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 * @author Bryan Davis <bd808@wikimedia.org>
25 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
28 class XhprofDataTest
extends PHPUnit_Framework_TestCase
{
31 * @covers XhprofData::splitKey
32 * @dataProvider provideSplitKey
34 public function testSplitKey( $key, $expect ) {
35 $this->assertSame( $expect, XhprofData
::splitKey( $key ) );
38 public function provideSplitKey() {
40 [ 'main()', [ null, 'main()' ] ],
41 [ 'foo==>bar', [ 'foo', 'bar' ] ],
42 [ 'bar@1==>bar@2', [ 'bar@1', 'bar@2' ] ],
43 [ 'foo==>bar==>baz', [ 'foo', 'bar==>baz' ] ],
44 [ '==>bar', [ '', 'bar' ] ],
50 * @covers XhprofData::pruneData
52 public function testInclude() {
53 $xhprofData = $this->getXhprofDataFixture( [
54 'include' => [ 'main()' ],
56 $raw = $xhprofData->getRawData();
57 $this->assertArrayHasKey( 'main()', $raw );
58 $this->assertArrayHasKey( 'main()==>foo', $raw );
59 $this->assertArrayHasKey( 'main()==>xhprof_disable', $raw );
60 $this->assertSame( 3, count( $raw ) );
64 * Validate the structure of data returned by
65 * Xhprof::getInclusiveMetrics(). This acts as a guard against unexpected
66 * structural changes to the returned data in lieu of using a more heavy
67 * weight typed response object.
69 * @covers XhprofData::getInclusiveMetrics
71 public function testInclusiveMetricsStructure() {
84 'variance' => 'numeric',
85 'percent' => 'numeric',
88 $xhprofData = $this->getXhprofDataFixture();
89 $metrics = $xhprofData->getInclusiveMetrics();
91 foreach ( $metrics as $name => $metric ) {
92 $this->assertArrayStructure( $metricStruct, $metric );
94 foreach ( $metricStruct as $key => $type ) {
95 if ( $type === 'array' ) {
96 $this->assertArrayStructure( $statStruct, $metric[$key] );
97 if ( $name === 'main()' ) {
98 $this->assertEquals( 100, $metric[$key]['percent'] );
106 * Validate the structure of data returned by
107 * Xhprof::getCompleteMetrics(). This acts as a guard against unexpected
108 * structural changes to the returned data in lieu of using a more heavy
109 * weight typed response object.
111 * @covers XhprofData::getCompleteMetrics
113 public function testCompleteMetricsStructure() {
121 'subcalls' => 'array',
123 $statsMetrics = [ 'wt', 'cpu', 'mu', 'pmu' ];
125 'total' => 'numeric',
129 'variance' => 'numeric',
130 'percent' => 'numeric',
131 'exclusive' => 'numeric',
134 $xhprofData = $this->getXhprofDataFixture();
135 $metrics = $xhprofData->getCompleteMetrics();
137 foreach ( $metrics as $name => $metric ) {
138 $this->assertArrayStructure( $metricStruct, $metric, $name );
140 foreach ( $metricStruct as $key => $type ) {
141 if ( in_array( $key, $statsMetrics ) ) {
142 $this->assertArrayStructure(
143 $statStruct, $metric[$key], $key
145 $this->assertLessThanOrEqual(
146 $metric[$key]['total'], $metric[$key]['exclusive']
154 * @covers XhprofData::getCallers
155 * @covers XhprofData::getCallees
158 public function testEdges() {
159 $xhprofData = $this->getXhprofDataFixture();
160 $this->assertSame( [], $xhprofData->getCallers( 'main()' ) );
161 $this->assertSame( [ 'foo', 'xhprof_disable' ],
162 $xhprofData->getCallees( 'main()' )
164 $this->assertSame( [ 'main()' ],
165 $xhprofData->getCallers( 'foo' )
167 $this->assertSame( [], $xhprofData->getCallees( 'strlen' ) );
171 * @covers XhprofData::getCriticalPath
174 public function testCriticalPath() {
175 $xhprofData = $this->getXhprofDataFixture();
176 $path = $xhprofData->getCriticalPath();
179 foreach ( $path as $key => $value ) {
180 list( $func, $call ) = XhprofData
::splitKey( $key );
181 $this->assertSame( $last, $func );
184 $this->assertSame( $last, 'bar@1' );
188 * Get an Xhprof instance that has been primed with a set of known testing
189 * data. Tests for the Xhprof class should laregly be concerned with
190 * evaluating the manipulations of the data collected by xhprof rather
191 * than the data collection process itself.
193 * The returned Xhprof instance primed will be with a data set created by
194 * running this trivial program using the PECL xhprof implementation:
196 * function bar( $x ) {
202 * for ( $idx = 0; $idx < 2; $idx++ ) {
204 * $x = strlen( 'abc' );
207 * xhprof_enable( XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY );
209 * $x = xhprof_disable();
215 protected function getXhprofDataFixture( array $opts = [] ) {
216 return new XhprofData( [
245 'main()==>xhprof_disable' => [
263 * Assert that the given array has the described structure.
265 * @param array $struct Array of key => type mappings
266 * @param array $actual Array to check
267 * @param string $label
269 protected function assertArrayStructure( $struct, $actual, $label = null ) {
270 $this->assertInternalType( 'array', $actual, $label );
271 $this->assertCount( count( $struct ), $actual, $label );
272 foreach ( $struct as $key => $type ) {
273 $this->assertArrayHasKey( $key, $actual );
274 $this->assertInternalType( $type, $actual[$key] );