Merge "Chinese Conversion Table Update 2017-6"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / SamplingStatsdClientTest.php
1 <?php
2
3 use Liuggio\StatsdClient\Entity\StatsdData;
4 use Liuggio\StatsdClient\Sender\SenderInterface;
5
6 class SamplingStatsdClientTest extends PHPUnit_Framework_TestCase {
7
8 use MediaWikiCoversValidator;
9
10 /**
11 * @dataProvider samplingDataProvider
12 */
13 public function testSampling( $data, $sampleRate, $seed, $expectWrite ) {
14 $sender = $this->getMockBuilder( SenderInterface::class )->getMock();
15 $sender->expects( $this->any() )->method( 'open' )->will( $this->returnValue( true ) );
16 if ( $expectWrite ) {
17 $sender->expects( $this->once() )->method( 'write' )
18 ->with( $this->anything(), $this->equalTo( $data ) );
19 } else {
20 $sender->expects( $this->never() )->method( 'write' );
21 }
22 mt_srand( $seed );
23 $client = new SamplingStatsdClient( $sender );
24 $client->send( $data, $sampleRate );
25 }
26
27 public function samplingDataProvider() {
28 $unsampled = new StatsdData();
29 $unsampled->setKey( 'foo' );
30 $unsampled->setValue( 1 );
31
32 $sampled = new StatsdData();
33 $sampled->setKey( 'foo' );
34 $sampled->setValue( 1 );
35 $sampled->setSampleRate( '0.1' );
36
37 return [
38 // $data, $sampleRate, $seed, $expectWrite
39 [ $unsampled, 1, 0 /*0.44*/, true ],
40 [ $sampled, 1, 0 /*0.44*/, false ],
41 [ $sampled, 1, 4 /*0.03*/, true ],
42 [ $unsampled, 0.1, 0 /*0.44*/, false ],
43 [ $sampled, 0.5, 0 /*0.44*/, false ],
44 [ $sampled, 0.5, 4 /*0.03*/, false ],
45 ];
46 }
47
48 public function testSetSamplingRates() {
49 $matching = new StatsdData();
50 $matching->setKey( 'foo.bar' );
51 $matching->setValue( 1 );
52
53 $nonMatching = new StatsdData();
54 $nonMatching->setKey( 'oof.bar' );
55 $nonMatching->setValue( 1 );
56
57 $sender = $this->getMockBuilder( SenderInterface::class )->getMock();
58 $sender->expects( $this->any() )->method( 'open' )->will( $this->returnValue( true ) );
59 $sender->expects( $this->once() )->method( 'write' )->with( $this->anything(),
60 $this->equalTo( $nonMatching ) );
61
62 $client = new SamplingStatsdClient( $sender );
63 $client->setSamplingRates( [ 'foo.*' => 0.2 ] );
64
65 mt_srand( 0 ); // next random is 0.44
66 $client->send( $matching );
67 mt_srand( 0 );
68 $client->send( $nonMatching );
69 }
70 }