2015-07-23 20:13:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
2024-02-16 22:07:23 +00:00
|
|
|
namespace Wikimedia\Tests;
|
|
|
|
|
|
2015-07-23 20:13:27 +00:00
|
|
|
use Liuggio\StatsdClient\Entity\StatsdData;
|
2017-04-05 23:39:50 +00:00
|
|
|
use Liuggio\StatsdClient\Sender\SenderInterface;
|
2024-02-16 22:07:23 +00:00
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use SamplingStatsdClient;
|
2015-07-23 20:13:27 +00:00
|
|
|
|
2017-12-29 23:44:13 +00:00
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \SamplingStatsdClient
|
2017-12-29 23:44:13 +00:00
|
|
|
*/
|
2024-02-16 22:07:23 +00:00
|
|
|
class SamplingStatsdClientTest extends TestCase {
|
2017-12-29 23:22:37 +00:00
|
|
|
|
|
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
|
2023-11-21 22:02:59 +00:00
|
|
|
protected function setUp(): void {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
if ( version_compare( PHP_VERSION, '8.2', '>=' ) ) {
|
|
|
|
|
// Tracked on T326386
|
|
|
|
|
$this->markTestSkipped( "PHP 8.2 isn't supported for this test" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-23 20:13:27 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider samplingDataProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testSampling( $data, $sampleRate, $seed, $expectWrite ) {
|
2022-07-14 12:42:07 +00:00
|
|
|
$sender = $this->createMock( SenderInterface::class );
|
2021-04-22 07:56:11 +00:00
|
|
|
$sender->method( 'open' )->willReturn( true );
|
2015-07-23 20:13:27 +00:00
|
|
|
if ( $expectWrite ) {
|
|
|
|
|
$sender->expects( $this->once() )->method( 'write' )
|
2021-04-22 07:56:11 +00:00
|
|
|
->with( $this->anything(), $data );
|
2015-07-23 20:13:27 +00:00
|
|
|
} else {
|
|
|
|
|
$sender->expects( $this->never() )->method( 'write' );
|
|
|
|
|
}
|
2024-04-06 08:00:12 +00:00
|
|
|
|
|
|
|
|
mt_srand( $seed );
|
|
|
|
|
|
2015-07-23 20:13:27 +00:00
|
|
|
$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' );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2015-07-23 20:13:27 +00:00
|
|
|
// $data, $sampleRate, $seed, $expectWrite
|
2024-04-06 08:00:12 +00:00
|
|
|
[ $unsampled, 1, 0 /*0.54*/, true ],
|
|
|
|
|
[ $sampled, 1, 0 /*0.54*/, false ],
|
|
|
|
|
[ $sampled, 1, 7 /*0.07*/, true ],
|
|
|
|
|
[ $unsampled, 0.1, 0 /*0.54*/, false ],
|
|
|
|
|
[ $sampled, 0.5, 0 /*0.54*/, false ],
|
|
|
|
|
[ $sampled, 0.5, 7 /*0.07*/, false ],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-07-23 20:13:27 +00:00
|
|
|
}
|
2016-04-12 22:08:20 +00:00
|
|
|
|
|
|
|
|
public function testSetSamplingRates() {
|
|
|
|
|
$matching = new StatsdData();
|
|
|
|
|
$matching->setKey( 'foo.bar' );
|
|
|
|
|
$matching->setValue( 1 );
|
|
|
|
|
|
|
|
|
|
$nonMatching = new StatsdData();
|
|
|
|
|
$nonMatching->setKey( 'oof.bar' );
|
|
|
|
|
$nonMatching->setValue( 1 );
|
|
|
|
|
|
2022-07-14 12:42:07 +00:00
|
|
|
$sender = $this->createMock( SenderInterface::class );
|
2021-04-22 07:56:11 +00:00
|
|
|
$sender->method( 'open' )->willReturn( true );
|
|
|
|
|
$sender->expects( $this->once() )->method( 'write' )
|
|
|
|
|
->with( $this->anything(), $nonMatching );
|
2016-04-12 22:08:20 +00:00
|
|
|
|
|
|
|
|
$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 );
|
|
|
|
|
}
|
2015-07-23 20:13:27 +00:00
|
|
|
}
|