| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace Wikimedia\Tests; |
| 22 |
|
| 23 |
use LogicException; |
| 24 |
use MediaWikiCoversValidator; |
| 25 |
use Monolog\Test\TestCase; |
| 26 |
use XhprofData; |
| 27 |
|
| 28 |
|
| 29 |
s |
| 30 |
|
| 31 |
|
| 32 |
class XhprofDataTest extends TestCase { |
| 33 |
use MediaWikiCoversValidator; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function testSplitKey( $key, $expect ) { |
| 39 |
$this->assertSame( $expect, XhprofData::splitKey( $key ) ); |
| 40 |
} |
| 41 |
|
| 42 |
public static function provideSplitKey() { |
| 43 |
return [ |
| 44 |
[ 'main()', [ null, 'main()' ] ], |
| 45 |
[ 'foo==>bar', [ 'foo', 'bar' ] ], |
| 46 |
[ 'bar@1==>bar@2', [ 'bar@1', 'bar@2' ] ], |
| 47 |
[ 'foo==>bar==>baz', [ 'foo', 'bar==>baz' ] ], |
| 48 |
[ '==>bar', [ '', 'bar' ] ], |
| 49 |
[ '', [ null, '' ] ], |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 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->assertCount( 3, $raw ); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function testInclusiveMetricsStructure() { |
| 71 |
$metricStruct = [ |
| 72 |
'ct' => 'int', |
| 73 |
'wt' => 'array', |
| 74 |
'cpu' => 'array', |
| 75 |
'mu' => 'array', |
| 76 |
'pmu' => 'array', |
| 77 |
]; |
| 78 |
$statStruct = [ |
| 79 |
'total' => 'numeric', |
| 80 |
'min' => 'numeric', |
| 81 |
'mean' => 'numeric', |
| 82 |
'max' => 'numeric', |
| 83 |
'variance' => 'numeric', |
| 84 |
'percent' => 'numeric', |
| 85 |
]; |
| 86 |
|
| 87 |
$xhprofData = $this->getXhprofDataFixture(); |
| 88 |
$metrics = $xhprofData->getInclusiveMetrics(); |
| 89 |
|
| 90 |
foreach ( $metrics as $name => $metric ) { |
| 91 |
$this->assertArrayStructure( $metricStruct, $metric ); |
| 92 |
|
| 93 |
foreach ( $metricStruct as $key => $type ) { |
| 94 |
if ( $type === 'array' ) { |
| 95 |
$this->assertArrayStructure( $statStruct, $metric[$key] ); |
| 96 |
if ( $name === 'main()' ) { |
| 97 |
$this->assertEquals( 100, $metric[$key]['percent'] ); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function testCompleteMetricsStructure() { |
| 111 |
$metricStruct = [ |
| 112 |
'ct' => 'int', |
| 113 |
'wt' => 'array', |
| 114 |
'cpu' => 'array', |
| 115 |
'mu' => 'array', |
| 116 |
'pmu' => 'array', |
| 117 |
'calls' => 'array', |
| 118 |
'subcalls' => 'array', |
| 119 |
]; |
| 120 |
$statsMetrics = [ 'wt', 'cpu', 'mu', 'pmu' ]; |
| 121 |
$statStruct = [ |
| 122 |
'total' => 'numeric', |
| 123 |
'min' => 'numeric', |
| 124 |
'mean' => 'numeric', |
| 125 |
'max' => 'numeric', |
| 126 |
'variance' => 'numeric', |
| 127 |
'percent' => 'numeric', |
| 128 |
'exclusive' => 'numeric', |
| 129 |
]; |
| 130 |
|
| 131 |
$xhprofData = $this->getXhprofDataFixture(); |
| 132 |
$metrics = $xhprofData->getCompleteMetrics(); |
| 133 |
|
| 134 |
foreach ( $metrics as $name => $metric ) { |
| 135 |
$this->assertArrayStructure( $metricStruct, $metric, $name ); |
| 136 |
|
| 137 |
foreach ( $metricStruct as $key => $type ) { |
| 138 |
if ( in_array( $key, $statsMetrics ) ) { |
| 139 |
$this->assertArrayStructure( |
| 140 |
$statStruct, $metric[$key], $key |
| 141 |
); |
| 142 |
$this->assertLessThanOrEqual( |
| 143 |
$metric[$key]['total'], $metric[$key]['exclusive'] |
| 144 |
); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public function testEdges() { |
| 151 |
$xhprofData = $this->getXhprofDataFixture(); |
| 152 |
$this->assertSame( [], $xhprofData->getCallers( 'main()' ) ); |
| 153 |
$this->assertSame( [ 'foo', 'xhprof_disable' ], |
| 154 |
$xhprofData->getCallees( 'main()' ) |
| 155 |
); |
| 156 |
$this->assertSame( [ 'main()' ], |
| 157 |
$xhprofData->getCallers( 'foo' ) |
| 158 |
); |
| 159 |
$this->assertSame( [], $xhprofData->getCallees( 'strlen' ) ); |
| 160 |
} |
| 161 |
|
| 162 |
public function testCriticalPath() { |
| 163 |
$xhprofData = $this->getXhprofDataFixture(); |
| 164 |
$path = $xhprofData->getCriticalPath(); |
| 165 |
|
| 166 |
$last = null; |
| 167 |
foreach ( $path as $key => $value ) { |
| 168 |
[ $func, $call ] = XhprofData::splitKey( $key ); |
| 169 |
$this->assertSame( $last, $func ); |
| 170 |
$last = $call; |
| 171 |
} |
| 172 |
$this->assertSame( 'bar@1', $last ); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
protected function getXhprofDataFixture( array $opts = [] ) { |
| 205 |
return new XhprofData( [ |
| 206 |
'foo==>bar' => [ |
| 207 |
'ct' => 2, |
| 208 |
'wt' => 57, |
| 209 |
'cpu' => 92, |
| 210 |
'mu' => 1896, |
| 211 |
'pmu' => 0, |
| 212 |
], |
| 213 |
'foo==>strlen' => [ |
| 214 |
'ct' => 2, |
| 215 |
'wt' => 21, |
| 216 |
'cpu' => 141, |
| 217 |
'mu' => 752, |
| 218 |
'pmu' => 0, |
| 219 |
], |
| 220 |
'bar==>bar@1' => [ |
| 221 |
'ct' => 1, |
| 222 |
'wt' => 18, |
| 223 |
'cpu' => 19, |
| 224 |
'mu' => 752, |
| 225 |
'pmu' => 0, |
| 226 |
], |
| 227 |
'main()==>foo' => [ |
| 228 |
'ct' => 1, |
| 229 |
'wt' => 304, |
| 230 |
'cpu' => 307, |
| 231 |
'mu' => 4008, |
| 232 |
'pmu' => 0, |
| 233 |
], |
| 234 |
'main()==>xhprof_disable' => [ |
| 235 |
'ct' => 1, |
| 236 |
'wt' => 8, |
| 237 |
'cpu' => 10, |
| 238 |
'mu' => 768, |
| 239 |
'pmu' => 392, |
| 240 |
], |
| 241 |
'main()' => [ |
| 242 |
'ct' => 1, |
| 243 |
'wt' => 353, |
| 244 |
'cpu' => 351, |
| 245 |
'mu' => 6112, |
| 246 |
'pmu' => 1424, |
| 247 |
], |
| 248 |
], $opts ); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
protected function assertArrayStructure( $struct, $actual, $label = '' ) { |
| 259 |
$this->assertIsArray( $actual, $label ); |
| 260 |
$this->assertSameSize( $struct, $actual, $label ); |
| 261 |
foreach ( $struct as $key => $type ) { |
| 262 |
$this->assertArrayHasKey( $key, $actual ); |
| 263 |
switch ( $type ) { |
| 264 |
case 'int': |
| 265 |
$this->assertIsInt( $actual[$key] ); |
| 266 |
break; |
| 267 |
case 'array': |
| 268 |
$this->assertIsArray( $actual[$key] ); |
| 269 |
break; |
| 270 |
case 'numeric': |
| 271 |
$this->assertIsNumeric( $actual[$key] ); |
| 272 |
break; |
| 273 |
default: |
| 274 |
throw new LogicException( "Missing handler for type '$type'" ); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
} |