Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / libs / SamplingStatsdClientTest.php
blob9a489303a7a3d9db4b94cd441d927e2b5e845042
1 <?php
3 use Liuggio\StatsdClient\Entity\StatsdData;
5 class SamplingStatsdClientTest extends PHPUnit_Framework_TestCase {
6 /**
7 * @dataProvider samplingDataProvider
8 */
9 public function testSampling( $data, $sampleRate, $seed, $expectWrite ) {
10 $sender = $this->getMock( 'Liuggio\StatsdClient\Sender\SenderInterface' );
11 $sender->expects( $this->any() )->method( 'open' )->will( $this->returnValue( true ) );
12 if ( $expectWrite ) {
13 $sender->expects( $this->once() )->method( 'write' )
14 ->with( $this->anything(), $this->equalTo( $data ) );
15 } else {
16 $sender->expects( $this->never() )->method( 'write' );
18 mt_srand( $seed );
19 $client = new SamplingStatsdClient( $sender );
20 $client->send( $data, $sampleRate );
23 public function samplingDataProvider() {
24 $unsampled = new StatsdData();
25 $unsampled->setKey( 'foo' );
26 $unsampled->setValue( 1 );
28 $sampled = new StatsdData();
29 $sampled->setKey( 'foo' );
30 $sampled->setValue( 1 );
31 $sampled->setSampleRate( '0.1' );
33 return [
34 // $data, $sampleRate, $seed, $expectWrite
35 [ $unsampled, 1, 0 /*0.44*/, true ],
36 [ $sampled, 1, 0 /*0.44*/, false ],
37 [ $sampled, 1, 4 /*0.03*/, true ],
38 [ $unsampled, 0.1, 0 /*0.44*/, false ],
39 [ $sampled, 0.5, 0 /*0.44*/, false ],
40 [ $sampled, 0.5, 4 /*0.03*/, false ],
44 public function testSetSamplingRates() {
45 $matching = new StatsdData();
46 $matching->setKey( 'foo.bar' );
47 $matching->setValue( 1 );
49 $nonMatching = new StatsdData();
50 $nonMatching->setKey( 'oof.bar' );
51 $nonMatching->setValue( 1 );
53 $sender = $this->getMock( 'Liuggio\StatsdClient\Sender\SenderInterface' );
54 $sender->expects( $this->any() )->method( 'open' )->will( $this->returnValue( true ) );
55 $sender->expects( $this->once() )->method( 'write' )->with( $this->anything(),
56 $this->equalTo( $nonMatching ) );
58 $client = new SamplingStatsdClient( $sender );
59 $client->setSamplingRates( [ 'foo.*' => 0.2 ] );
61 mt_srand( 0 ); // next random is 0.44
62 $client->send( $matching );
63 mt_srand( 0 );
64 $client->send( $nonMatching );