]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/libs/SamplingStatsdClientTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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          * @dataProvider samplingDataProvider
9          */
10         public function testSampling( $data, $sampleRate, $seed, $expectWrite ) {
11                 $sender = $this->getMockBuilder( SenderInterface::class )->getMock();
12                 $sender->expects( $this->any() )->method( 'open' )->will( $this->returnValue( true ) );
13                 if ( $expectWrite ) {
14                         $sender->expects( $this->once() )->method( 'write' )
15                                 ->with( $this->anything(), $this->equalTo( $data ) );
16                 } else {
17                         $sender->expects( $this->never() )->method( 'write' );
18                 }
19                 mt_srand( $seed );
20                 $client = new SamplingStatsdClient( $sender );
21                 $client->send( $data, $sampleRate );
22         }
23
24         public function samplingDataProvider() {
25                 $unsampled = new StatsdData();
26                 $unsampled->setKey( 'foo' );
27                 $unsampled->setValue( 1 );
28
29                 $sampled = new StatsdData();
30                 $sampled->setKey( 'foo' );
31                 $sampled->setValue( 1 );
32                 $sampled->setSampleRate( '0.1' );
33
34                 return [
35                         // $data, $sampleRate, $seed, $expectWrite
36                         [ $unsampled, 1, 0 /*0.44*/, true ],
37                         [ $sampled, 1, 0 /*0.44*/, false ],
38                         [ $sampled, 1, 4 /*0.03*/, true ],
39                         [ $unsampled, 0.1, 0 /*0.44*/, false ],
40                         [ $sampled, 0.5, 0 /*0.44*/, false ],
41                         [ $sampled, 0.5, 4 /*0.03*/, false ],
42                 ];
43         }
44
45         public function testSetSamplingRates() {
46                 $matching = new StatsdData();
47                 $matching->setKey( 'foo.bar' );
48                 $matching->setValue( 1 );
49
50                 $nonMatching = new StatsdData();
51                 $nonMatching->setKey( 'oof.bar' );
52                 $nonMatching->setValue( 1 );
53
54                 $sender = $this->getMockBuilder( SenderInterface::class )->getMock();
55                 $sender->expects( $this->any() )->method( 'open' )->will( $this->returnValue( true ) );
56                 $sender->expects( $this->once() )->method( 'write' )->with( $this->anything(),
57                         $this->equalTo( $nonMatching ) );
58
59                 $client = new SamplingStatsdClient( $sender );
60                 $client->setSamplingRates( [ 'foo.*' => 0.2 ] );
61
62                 mt_srand( 0 ); // next random is 0.44
63                 $client->send( $matching );
64                 mt_srand( 0 );
65                 $client->send( $nonMatching );
66         }
67 }