Metric...r::Test(3pm)
π Metrics::Any::Adapter::Test β the girlies' guide π
okay so bestie, this is the Metrics::Any::Adapter::Test page from the User Contributed Perl Documentation, and it's literally the cutest little adapter for unit testing your metrics. no cap. π
β¨ NAME β¨
Metrics::Any::Adapter::Test β a metrics reporting adapter for unit testing π§ͺπ
πΈ SYNOPSIS πΈ
use Test2::V0;
use Metrics::Any::Adapter 'Test';
{
Metrics::Any::Adapter::Test->clear;
# perform some work in the code under test
is( Metrics::Any::Adapter::Test->metrics,
"an_expected_metric = 1\n",
'Metrics were reported while doing something'
);
}
like look at her go π
π¦ DESCRIPTION π¦
so this Metrics::Any adapter type is the one that stores all the reported metrics locally, right, and then lets you peek at them with the metrics method. it's giving "test your code's metrics" energy, and honestly? iconic. you use it in a unit test to check that the code under test is reporting the correct metrics. periodt. β¨
she also supports timer metrics by storing them as distributions. by default, distributions store only a summary β that's the count and the total duration, nothing extra. but if you want the full values stored too, you just set use_full_distributions. easy. π
and for predictable output of timer metrics in unit tests, a unit test may wish to use the override_timer_duration method. (yes bestie, we'll get to her.) π
this adapter type also supports batch mode reporting. the callbacks get invoked at the beginning of the metrics method. iykyk. π«
π METHODS π
π metrics
$result = Metrics::Any::Adapter::Test->metrics
this class method returns a string describing all of the stored metric values. each one is reported on a line formatted like this:
name = value
every line, including the very last one, is terminated by a linefeed. the metrics are sorted alphabetically. and any multi-part metric names get joined with underscores (_). slay. π
metrics that have additional labels get formatted with the extra label names and label values, in declared order, sitting after the name and before the = symbol:
name l1:v1 l2:v2 = value
so cute. so organized. π
π use_full_distributions
Metrics::Any::Adapter::Test->use_full_distributions; # enables the option
Metrics::Any::Adapter::Test->use_full_distributions( $enable );
since version 0.08. β
if enabled, this option stores the full value of every reported observation into distributions, rathr than just the count-and-total summary. (yes, "rathr" β that's what the original says, and we do not edit the queen's typo, we respect it. π)
full value distributions get formatted as a sequence of lines containing the count of observations at that particular value, in square brackets, followed by the summary count:
name[v1] = c1
name[v2] = c2
...
name_count = c
and because we're not trying to be too sensitive to numerical rounding errors, values are stored to only 3 decimal places. lowkey thoughtful of her. π
π clear
Metrics::Any::Adapter::Test->clear
this class method removes all of the stored values of reported metrics. fresh start, clean slate, new me. β¨
π override_timer_duration
Metrics::Any::Adapter::Test->override_timer_duration( $duration )
this class method sets a duration value, and any subsequent call to inc_timer will use that instead of the value the caller actually passed in. this ensures reliably predictable output in unit tests. no more flaky tests bestie, we love to see it. π«Ά
β οΈ heads up: any value you set here will be cleared by clear. so don't come crying to me when your override disappears, i warned you. π
π AUTHOR π
Paul Evans <leonerd@leonerd.org.uk> π
and that's the whole page, bestie!! go forth and test your metrics πβ¨π¦
this was brought to you by perl v5.36.0, dated 2023-09-29 β mwah, take care of her, she's a classic ππ
π
π¦ where this page comes from
- debian trixie: libmetrics-any-perl 0.10-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmetrics-any-perl 0.10-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmetrics-any-perl 0.10-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmetrics-any-perl 0.10-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms