Debian Codegraph @

tests/phpunit/unit/includes/libs/Stats/StatsEmitterTest.php

raw symbols SCIP
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 * @covers \Wikimedia\Stats\Emitters\UDPEmitter
15 * @covers \Wikimedia\Stats\StatsCache
16 * @covers \Wikimedia\Stats\Formatters\DogStatsdFormatter
17 * @covers \Wikimedia\Stats\OutputFormats
18 */
19 class StatsEmitterTest extends TestCase {
20
21 public function testSendMetrics() {
22 // set up a mock statsd data factory
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 // initialize cache
29 $cache = new StatsCache();
30
31 // emitter
32 $emitter = OutputFormats::getNewEmitter(
33 'mediawiki',
34 $cache,
35 OutputFormats::getNewFormatter( OutputFormats::DOGSTATSD )
36 );
37
38 // transport
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 // initialize metrics factory
51 $m = new StatsFactory( $cache, $emitter, new NullLogger, 'test' );
52
53 // inject statsd factory
54 $m->withStatsdDataFactory( $statsd );
55
56 // populate metric with statsd copy
57 $m->getCounter( 'bar' )->copyToStatsdAt( 'test.metric' )->increment();
58
59 // fetch same metric from cache and use it
60 $metric = $m->getCounter( 'bar' );
61 $metric->increment();
62 $metric = $m->getTiming( 'foo' )->copyToStatsdAt( 'test.metric.timing' );
63 $metric->observe( 3.14 );
64
65 // name collision: gauge should not appear in output nor throw exception
66 $metric = @$m->getGauge( 'bar' );
67 $metric->set( 42 );
68
69 $m->getGauge( 'gauge' )->copyToStatsdAt( 'test.metric.gauge' )->set( 1 );
70
71 // send metrics
72 $m->flush();
73 }
74
75 }