| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\Stats; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Psr\Log\NullLogger; |
| 7 |
use UDPTransport; |
| 8 |
use Wikimedia\Stats\IBufferingStatsdDataFactory; |
| 9 |
use Wikimedia\Stats\OutputFormats; |
| 10 |
use Wikimedia\Stats\StatsCache; |
| 11 |
use Wikimedia\Stats\StatsFactory; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class StatsEmitterTest extends TestCase { |
| 20 |
|
| 21 |
public function testSendMetrics() { |
| 22 |
|
| 23 |
$statsd = $this->createMock( IBufferingStatsdDataFactory::class ); |
| 24 |
$statsd->expects( $this->exactly( 3 ) )->method( "updateCount" ); |
| 25 |
$statsd->expects( $this->once() )->method( "gauge" ); |
| 26 |
$statsd->expects( $this->once() )->method( "timing" ); |
| 27 |
|
| 28 |
|
| 29 |
$cache = new StatsCache(); |
| 30 |
|
| 31 |
|
| 32 |
$emitter = OutputFormats::getNewEmitter( |
| 33 |
'mediawiki', |
| 34 |
$cache, |
| 35 |
OutputFormats::getNewFormatter( OutputFormats::DOGSTATSD ) |
| 36 |
); |
| 37 |
|
| 38 |
|
| 39 |
$transport = $this->createMock( UDPTransport::class ); |
| 40 |
$transport->expects( $this->once() )->method( "emit" ) |
| 41 |
->with( |
| 42 |
"mediawiki.test.bar:1|c\n" . |
| 43 |
"mediawiki.test.bar:1|c\n" . |
| 44 |
"mediawiki.test.foo:3.14|ms\n" . |
| 45 |
"mediawiki.test.gauge:1|g\n" . |
| 46 |
"mediawiki.test.stats_buffered_total:4|c\n" |
| 47 |
); |
| 48 |
$emitter = $emitter->withTransport( $transport ); |
| 49 |
|
| 50 |
|
| 51 |
$m = new StatsFactory( $cache, $emitter, new NullLogger, 'test' ); |
| 52 |
|
| 53 |
|
| 54 |
$m->withStatsdDataFactory( $statsd ); |
| 55 |
|
| 56 |
|
| 57 |
$m->getCounter( 'bar' )->copyToStatsdAt( 'test.metric' )->increment(); |
| 58 |
|
| 59 |
|
| 60 |
$metric = $m->getCounter( 'bar' ); |
| 61 |
$metric->increment(); |
| 62 |
$metric = $m->getTiming( 'foo' )->copyToStatsdAt( 'test.metric.timing' ); |
| 63 |
$metric->observe( 3.14 ); |
| 64 |
|
| 65 |
|
| 66 |
$metric = @$m->getGauge( 'bar' ); |
| 67 |
$metric->set( 42 ); |
| 68 |
|
| 69 |
$m->getGauge( 'gauge' )->copyToStatsdAt( 'test.metric.gauge' )->set( 1 ); |
| 70 |
|
| 71 |
|
| 72 |
$m->flush(); |
| 73 |
} |
| 74 |
|
| 75 |
} |