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