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