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 XhprofTest
extends PHPUnit_Framework_TestCase
{
30 public function setUp() {
31 if ( !function_exists( 'xhprof_enable' ) ) {
32 $this->markTestSkipped( 'No xhprof support detected.' );
37 * @covers Xhprof::splitKey
38 * @dataProvider provideSplitKey
40 public function testSplitKey( $key, $expect ) {
41 $this->assertSame( $expect, Xhprof
::splitKey( $key ) );
44 public function provideSplitKey() {
46 array( 'main()', array( null, 'main()' ) ),
47 array( 'foo==>bar', array( 'foo', 'bar' ) ),
48 array( 'bar@1==>bar@2', array( 'bar@1', 'bar@2' ) ),
49 array( 'foo==>bar==>baz', array( 'foo', 'bar==>baz' ) ),
50 array( '==>bar', array( '', 'bar' ) ),
51 array( '', array( null, '' ) ),
56 * @covers Xhprof::__construct
57 * @covers Xhprof::stop
58 * @covers Xhprof::getRawData
59 * @dataProvider provideRawData
61 public function testRawData( $flags, $keys ) {
62 $xhprof = new Xhprof( array( 'flags' => $flags ) );
63 $raw = $xhprof->getRawData();
64 $this->assertArrayHasKey( 'main()', $raw );
65 foreach ( $keys as $key ) {
66 $this->assertArrayHasKey( $key, $raw['main()'] );
70 public function provideRawData() {
72 array( 0, array( 'ct', 'wt' ) ),
75 if ( defined( 'XHPROF_FLAGS_CPU' ) && defined( 'XHPROF_FLAGS_CPU' ) ) {
76 $tests[] = array( XHPROF_FLAGS_MEMORY
, array(
77 'ct', 'wt', 'mu', 'pmu',
79 $tests[] = array( XHPROF_FLAGS_CPU
, array(
82 $tests[] = array( XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU
, array(
83 'ct', 'wt', 'mu', 'pmu', 'cpu',
91 * @covers Xhprof::pruneData
93 public function testInclude() {
94 $xhprof = $this->getXhprofFixture( array(
95 'include' => array( 'main()' ),
97 $raw = $xhprof->getRawData();
98 $this->assertArrayHasKey( 'main()', $raw );
99 $this->assertArrayHasKey( 'main()==>foo', $raw );
100 $this->assertArrayHasKey( 'main()==>xhprof_disable', $raw );
101 $this->assertSame( 3, count( $raw ) );
105 * Validate the structure of data returned by
106 * Xhprof::getInclusiveMetrics(). 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.
110 * @covers Xhprof::getInclusiveMetrics
112 public function testInclusiveMetricsStructure() {
113 $metricStruct = array(
121 'total' => 'numeric',
125 'variance' => 'numeric',
126 'percent' => 'numeric',
129 $xhprof = $this->getXhprofFixture();
130 $metrics = $xhprof->getInclusiveMetrics();
132 foreach ( $metrics as $name => $metric ) {
133 $this->assertArrayStructure( $metricStruct, $metric );
135 foreach ( $metricStruct as $key => $type ) {
136 if ( $type === 'array' ) {
137 $this->assertArrayStructure( $statStruct, $metric[$key] );
138 if ( $name === 'main()' ) {
139 $this->assertEquals( 100, $metric[$key]['percent'] );
147 * Validate the structure of data returned by
148 * Xhprof::getCompleteMetrics(). This acts as a guard against unexpected
149 * structural changes to the returned data in lieu of using a more heavy
150 * weight typed response object.
152 * @covers Xhprof::getCompleteMetrics
154 public function testCompleteMetricsStructure() {
155 $metricStruct = array(
162 'subcalls' => 'array',
164 $statsMetrics = array( 'wt', 'cpu', 'mu', 'pmu' );
166 'total' => 'numeric',
170 'variance' => 'numeric',
171 'percent' => 'numeric',
172 'exclusive' => 'numeric',
175 $xhprof = $this->getXhprofFixture();
176 $metrics = $xhprof->getCompleteMetrics();
178 foreach ( $metrics as $name => $metric ) {
179 $this->assertArrayStructure( $metricStruct, $metric, $name );
181 foreach ( $metricStruct as $key => $type ) {
182 if ( in_array( $key, $statsMetrics ) ) {
183 $this->assertArrayStructure(
184 $statStruct, $metric[$key], $key
186 $this->assertLessThanOrEqual(
187 $metric[$key]['total'], $metric[$key]['exclusive']
195 * @covers Xhprof::getCallers
196 * @covers Xhprof::getCallees
199 public function testEdges() {
200 $xhprof = $this->getXhprofFixture();
201 $this->assertSame( array(), $xhprof->getCallers( 'main()' ) );
202 $this->assertSame( array( 'foo', 'xhprof_disable' ),
203 $xhprof->getCallees( 'main()' )
205 $this->assertSame( array( 'main()' ),
206 $xhprof->getCallers( 'foo' )
208 $this->assertSame( array(), $xhprof->getCallees( 'strlen' ) );
212 * @covers Xhprof::getCriticalPath
215 public function testCriticalPath() {
216 $xhprof = $this->getXhprofFixture();
217 $path = $xhprof->getCriticalPath();
220 foreach ( $path as $key => $value ) {
221 list( $func, $call ) = Xhprof
::splitKey( $key );
222 $this->assertSame( $last, $func );
225 $this->assertSame( $last, 'bar@1' );
229 * Get an Xhprof instance that has been primed with a set of known testing
230 * data. Tests for the Xhprof class should laregly be concerned with
231 * evaluating the manipulations of the data collected by xhprof rather
232 * than the data collection process itself.
234 * The returned Xhprof instance primed will be with a data set created by
235 * running this trivial program using the PECL xhprof implementation:
237 * function bar( $x ) {
243 * for ( $idx = 0; $idx < 2; $idx++ ) {
245 * $x = strlen( 'abc' );
248 * xhprof_enable( XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY );
250 * $x = xhprof_disable();
256 protected function getXhprofFixture( array $opts = array() ) {
257 $xhprof = new Xhprof( $opts );
258 $xhprof->loadRawData( array (
259 'foo==>bar' => array (
266 'foo==>strlen' => array (
273 'bar==>bar@1' => array (
280 'main()==>foo' => array (
287 'main()==>xhprof_disable' => array (
306 * Assert that the given array has the described structure.
308 * @param array $struct Array of key => type mappings
309 * @param array $actual Array to check
310 * @param string $label
312 protected function assertArrayStructure( $struct, $actual, $label = null ) {
313 $this->assertInternalType( 'array', $actual, $label );
314 $this->assertCount( count($struct), $actual, $label );
315 foreach ( $struct as $key => $type ) {
316 $this->assertArrayHasKey( $key, $actual );
317 $this->assertInternalType( $type, $actual[$key] );