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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
use IO::Handle;
use Errno qw( EAGAIN EWOULDBLOCK );
use Metrics::Any '$metrics';
require Metrics::Any::Adapter; # no 'use' yet
sub readall
{
my $ret = $_[0];
$_[0] = "";
return $ret;
}
open my $fh, ">>", \( my $buf = "" );
# fh isn't documented but useful for this unit test
Metrics::Any::Adapter->import( File => fh => $fh );
# Force the adapter to exist
$metrics->adapter;
ok( $metrics, '$metrics is still true' );
# counter
{
$metrics->make_counter( c => name => "counter" );
$metrics->inc_counter( c => );
is( readall( $buf ), "METRIC COUNTER counter +1 => 1\n",
'Counter metric written' );
$metrics->inc_counter_by( c => 3 );
is( readall( $buf ), "METRIC COUNTER counter +3 => 4\n",
'Counter persists total' );
}
# distribution
{
$metrics->make_distribution( d => name => "distribution" );
$metrics->report_distribution( d => 5 );
is( readall( $buf ), "METRIC DISTRIBUTION distribution +5 => 5/1 [avg=5]\n",
'Distribution metric written' );
$metrics->report_distribution( d => 3 );
is( readall( $buf ), "METRIC DISTRIBUTION distribution +3 => 8/2 [avg=4]\n",
'Distribution persists total and count' );
}
# gauge
{
$metrics->make_gauge( g => name => "gauge" );
$metrics->inc_gauge( g => );
is( readall( $buf ), "METRIC GAUGE gauge +1 => 1\n",
'Gauge metric written' );
$metrics->inc_gauge_by( g => 2 );
is( readall( $buf ), "METRIC GAUGE gauge +2 => 3\n",
'Gauge persists total' );
}
# timer
{
$metrics->make_timer( t => name => "timer" );
$metrics->report_timer( t => 0.02 );
is( readall( $buf ), "METRIC TIMER timer +0.02 => 0.02/1 [avg=0.02]\n",
'Timer metric written' );
$metrics->report_timer( t => 0.04 );
is( readall( $buf ), "METRIC TIMER timer +0.04 => 0.06/2 [avg=0.03]\n",
'Timer persists total and count' );
}
ok( $metrics, '$metrics is still true at EOF' );
done_testing;
|