Test::Timer(3pm)

๐Ÿ“ฆ
version 2.12 ยท 6 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

๐ŸŽ€ Test::Timer โ€” the manual, but make it make sense ๐Ÿ’…

okay bestie, gather round. This is Test::Timer(3pm), a Perl module from the User Contributed Perl Documentation, and it's literally here to test/assert response times. Timing your code = self care, no cap. Let's get into it. โœจ


โœจ VERSION โœจ

This doc is describing version 2.12 of Test::Timer. Yes, in 2.12. Keep up. ๐Ÿ’–


๐Ÿฆ‹ FEATURES ๐Ÿฆ‹

  • Test subroutines so you can write unit-tests that check your code executes before a specified threshold (we love a deadline queen)
  • Test subroutines so you can write unit-tests that check your code execution exceeds a specified threshold
  • A test subroutine so you can write unit-tests that check your code executes within a specified time frame โ€” the sweet spot, the vibe zone ๐ŸŒธ
  • Measurements are in seconds. That's it. Seconds. (More on this in BUGS, stay tuned ๐Ÿ‘€)
  • It implements a configurable alarm signal handler so your tests don't just... execute forever into the void. Iconic fail-safe energy. ๐Ÿ”ฅ

๐ŸŒˆ SYNOPSIS ๐ŸŒˆ

use Test::Timer;

time_ok( sub { doYourStuffButBeQuickAboutIt(); }, 1, 'threshold of one second');

time_atmost( sub { doYourStuffYouHave10Seconds(); }, 10, 'threshold of 10 seconds');

time_between( sub { doYourStuffYouHave5-10Seconds(); }, 5, 10,
    'lower threshold of 5 seconds and upper threshold of 10 seconds');

# Will succeed
time_nok( sub { sleep(2); }, 1, 'threshold of one second');

time_atleast( sub { sleep(2); }, 2, 'threshold of one second');

# Will fail after 5 (threshold) + 2 seconds (default alarm)
time_ok( sub { while(1) { sleep(1); } }, 5, 'threshold of one second');

$test::Timer::alarm = 6 #default 2 seconds

# Will fail after 5 (threshold) + 6 seconds (specified alarm)
time_ok( sub { while(1) { sleep(1); } }, 5, 'threshold of one second');

๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

Test::Timer implements a set of test primitives so you can test and assert test times from bodies of code. It's giving "I timed you and I have receipts". โฑ๏ธ

The key features are subroutines to assert or test the following:

  • that a given piece of code does not exceed a specified time limit
  • that a given piece of code takes longer than a specified time limit and does not exceed another

๐Ÿซถ EXPORT ๐Ÿซถ

Test::Timer exports:

  • time_ok
  • time_nok
  • time_atleast
  • time_atmost
  • time_between

๐Ÿ‘‘ SUBROUTINES/METHODS ๐Ÿ‘‘

๐ŸŒธ time_ok

Takes the following parameters:

  • a reference to a block of code (anonymous sub) โ€” yes, your lil closure bestie
  • a threshold specified as a integer indicating a number of seconds
  • a string specifying a test name
time_nok( sub { sleep(2); }, 1, 'threshold of one second');

If the execution of the code exceeds the threshold specified, the test fail with the following diagnostic message:

Test ran 2 seconds and exceeded specified threshold of 1 seconds

(Yes. "the test fail". We're keeping it, it's part of the lore. ๐Ÿ˜ญ)


๐Ÿฆ‹ time_nok

This is the inverted variant of time_ok. It passes if the threshold is exceeded, and it fails if the benchmark of the code is within the specified timing threshold. Chaotic? Maybe. Useful? Absolutely. ๐Ÿ’…

The API is the same as for time_ok.

time_nok( sub { sleep(1); }, 2, 'threshold of two seconds');

If the execution of the code executes below the threshold specified, the test fail with the following diagnostic message:

Test ran 1 seconds and did not exceed specified threshold of 2 seconds

โญ time_atmost

This is syntactic sugar for time_ok โ€” same flavour, cuter name. ๐Ÿ“

time_atmost( sub { doYourStuffButBeQuickAboutIt(); }, 1, 'threshold of one second');

If the execution of the code exceeds the threshold specified, the test fail with the following diagnostic message:

Test ran N seconds and exceeded specified threshold of 1 seconds

N will be the actual measured execution time of the specified code. So N is your code's report card, basically. ๐Ÿ“


๐Ÿ’€ time_atleast

time_atleast( sub { doYourStuffAndTakeYourTimeAboutIt(); }, 1, 'threshold of 1 second');

The test succeeds if the code takes at least the number of seconds specified by the timing threshold. Slow and steady wins the race, periodt. ๐Ÿข

If the code executes faster, the test fails with the following diagnostic message:

Test ran 1 seconds and did not exceed specified threshold of 2 seconds
โš ๏ธ ok this is important bestie, listen up: please be aware that Test::Timer breaks the execution with an alarm specified to trigger after the specified threshold + 2 seconds (default). So if you expect your execution to run longer, set the alarm accordingly. Do not just let it vibe and hope for the best. ๐Ÿ™ˆ
$Test::Timer::alarm = $my_alarm_in_seconds;

See also diagnostics. (That's the DIAGNOSTICS section further down. We'll get there. ๐Ÿซถ)


๐ŸŒˆ time_between

This method is a more extensive variant of time_atmost and time_ok. You can specify a lower and an upper threshold, and the code has to execute within this interval in order for the test to succeed. It's giving "goldilocks but make it unit tests". ๐Ÿงš

time_between( sub { sleep(2); }, 5, 10,
    'lower threshold of 5 seconds and upper threshold of 10 seconds');

If the code executes faster than the lower threshold, or exceeds the upper threshold, the test fails with the following diagnostic message:

Test ran 2 seconds and did not execute within specified interval 5 - 10 seconds

Or:

Test ran 12 seconds and did not execute within specified interval 5 - 10 seconds

Either way, you get told. Loudly. ๐Ÿ’–


๐Ÿ”ฎ PRIVATE FUNCTIONS ๐Ÿ”ฎ

๐ŸŽ€ _runtest

This is a method to handle the result from _benchmark. It initiates the benchmark calling benchmark, and based on whether it is within the provided interval, true (1) is returned and if not false (0). Binary queen. ๐Ÿ’…

๐ŸŒธ _benchmark

This is the method doing the actual benchmark. If a better method is located, this is the place to do the handy work.

Currently Benchmark is used. An alternative could be Devel::Timer, but the author does not know this module very well and Benchmark is core, so this is used for now. (Honesty? We love to see it. ๐Ÿ˜Œ)

The method takes two parameters:

  • a code block via a code reference
  • a threshold (the upper threshold, since this is added to the default alarm

๐Ÿ’œ import

Test::Builder required import to do some import hokus-pokus for the test methods exported from Test::Timer. Please refer to the documentation in Test::Builder. Yes, hokus-pokus is the technical term. ๐Ÿช„


๐Ÿšจ DIAGNOSTICS ๐Ÿšจ

All tests either fail or succeed, but a few exceptions are implemented. These are listed below:

  • Test did not exceed specified threshold โ€” this message is diagnosis for time_atleast and time_nok tests, which do not exceed their specified threshold
  • Test exceeded specified threshold โ€” this message is a diagnostic for time_atmost and time_ok, if the specified threshold is surpassed.

This is the key point of the module: either your code is too slow and you should address this, or your threshold is too low โ€” in which case you can set it a bit higher and run the test again. Pick your fighter. ๐ŸฅŠ

  • Test did not execute within specified interval โ€” this is the diagnostic from time_between. It is the diagnosis if the execution of the code is not between the specified lower and upper thresholds
  • Insufficient parameters โ€” this is the message if a specified test is not provided with the sufficient number of parameters. Consult this documentation and correct accordingly. (aka: read the docs, bestie ๐Ÿ“–)
  • Execution exceeded threshold and timed out โ€” the exception is thrown if the execution of tested code exceeds even the alarm, which is default 2 seconds, but can be set by the user, or is equal to the upper threshold + 2 seconds

The exception results in a diagnostic for the failing test. This is a fail-safe to avoid that code runs forever. If you get this diagnose, either your code is too slow and you should address this, or it might be error prone. If this is not the case, adjust the alarm setting to suit your situation. ๐Ÿ›Ÿ


โš™๏ธ CONFIGURATION AND ENVIRONMENT โš™๏ธ

This module requires no special configuration or environment. Clean, minimal, unbothered. โœจ

Tests are sensitive and be configured using environment and configuration files โ€” please see the section on test and quality. (That's TEST AND QUALITY, right below-ish. We got you. ๐Ÿซถ)


๐Ÿงฉ DEPENDENCIES ๐Ÿงฉ

  • Carp
  • Benchmark
  • Error
  • Test::Builder
  • Test::Builder::Module

The whole squad. ๐Ÿ‘‘


๐Ÿšซ INCOMPATIBILITIES ๐Ÿšซ

This module holds no known incompatibilities. Everyone gets along. Peace and love. ๐ŸŒˆ


๐Ÿ› BUGS AND LIMITATIONS ๐Ÿ›

This module holds no known bugs. Slay. ๐Ÿ’…

The current implementations only use seconds and resolutions should be higher, so the current implementation is limited to seconds as the highest resolution. That's the tea. โ˜•

On occasion, failing tests with CPAN-testers have been observed. This seem to be related to the test-suite being not taking into account that some smoke-testers do not prioritize resources for the test run and that additional processes/jobs are running. The test-suite have been adjusted to accommodate this, but these issues might reoccur. So if you see it flake: it's not you, it's the smoke-testers. ๐Ÿ˜ค


๐Ÿ“Š TEST AND QUALITY ๐Ÿ“Š

Coverage report for the release described in this documentation (see VERSION):

---------------------------- ------ ------ ------ ------ ------ ------ ------
File                           stmt   bran   cond    sub    pod   time  total
---------------------------- ------ ------ ------ ------ ------ ------ ------
blib/lib/Test/Timer.pm        100.0  100.0  100.0  100.0  100.0   99.9  100.0
...Timer/TimeoutException.pm  100.0    n/a    n/a  100.0  100.0    0.0  100.0
Total                         100.0  100.0  100.0  100.0  100.0  100.0  100.0
---------------------------- ------ ------ ------ ------ ------ ------ ------

One hundred percent across the board, we are OBSESSED. ๐Ÿ”ฅ๐Ÿ‘‘

The Test::Perl::Critic test runs with severity 5 (gentle) for now, please refer to t/critic.t and t/perlcriticrc.

Set TEST_POD to enable Test::Pod test in t/pod.t and Test::Pod::Coverage test in t/pod-coverage.t.

Set TEST_CRITIC to enable Test::Perl::Critic test in t/critic.t

๐ŸŒŸ CONTINUOUS INTEGRATION ๐ŸŒŸ

This distribution uses Travis for continuous integration testing, the Travis reports are public available. Public. Everyone can see. Behave. ๐Ÿ™ˆ


๐Ÿ”— SEE ALSO ๐Ÿ”—

  • Test::Benchmark

๐Ÿž ISSUE REPORTING ๐Ÿž

Please report any bugs or feature requests using GitHub


๐Ÿซ‚ SUPPORT ๐Ÿซ‚

You can find (this) documentation for this module with the perldoc command.

perldoc Test::Timer

You can also look for information at:


๐Ÿ› ๏ธ DEVELOPMENT ๐Ÿ› ๏ธ

Contributing bestie behaviour. We love a collaborator. ๐Ÿ’–


โœ๏ธ AUTHOR โœ๏ธ

  • Jonas Bromso (jonasbn) <jonasbn at cpan.org>

๐Ÿ’– ACKNOWLEDGEMENTS ๐Ÿ’–

okay so before we get to the legal bits, we gotta do the shoutouts, because this module did NOT get here alone and the people below are literally the wind beneath our wings fr fr ๐Ÿซถ

  • Mohammad S Anwar (MANWAR) โ€” POD corrections PRs #23 โœจ
  • Erik Johansen (UNIEJO) โ€” suggestion for clearing alarm ๐Ÿšจ
  • Gregor Herrmann (GREGOA) from the Debian Perl Group โ€” PR #16 fixes to spelling mistakes ๐Ÿ“
  • Nigel Horne (NHORNE) โ€” issue #15 suggestion for better assertion in time_atleast
  • Nigel Horne (NHORNE) โ€” issue #10/#12 suggestion for improvement to diagnostics ๐Ÿ”
  • p-alik โ€” PR #4 eliminating warnings during test ๐Ÿคซ
  • Kent Fredric (KENTNL) โ€” PR #7 addressing file permissions ๐Ÿ”
  • Nick Morrott โ€” PR #5 corrections to POD ๐Ÿ“–
  • Bartosz Jakubski โ€” reporting issue #3 ๐Ÿ›
  • Gabor Szabo (SZABGAB) โ€” suggestion for specification of interval thresholds even though this was obsoleted by the later introduced time_between
  • Paul Leonerd Evans (PEVANS) โ€” suggestions for time_atleast and time_atmost and the handling of $SIG{ALRM}. Also bug report for addressing issue with Debian packaging resulting in release 0.10 ๐Ÿ“ฆ
  • brian d foy (BDFOY) โ€” for patch to _runtest ๐Ÿ”ง

like... the community really showed up for this one and we are SO obsessed with all of them ๐Ÿ’œ


okay bestie here's the part where we get โœจofficialโœจ, don't skip it, it's giving very legal and very important ๐Ÿ’…

Test::Timer and related modules are (C) by Jonas Bromso (jonasbn) 2007-2021

Test::Timer and related modules are released under the Artistic License 2.0

Used distributions are under copyright of there respective authors and designated licenses

Image used on website https://jonasbn.github.io/perl-test-timer/ is under copyright by Veri Ivanova https://unsplash.com/photos/p3Pj7jOYvnM

ngl, if you're doing anything with this beyond vibing in your own terminal, go actually read that license. it's there for a reason and it's lowkey iconic of you to check ๐ŸŽ€

and that's a wrap on Test::Timer(3pm) bestie!! ๐ŸŒˆ this page is from perl v5.36.0 and it's dated 2022-10-13 โ€” go forth, time your tests, be the main character of your own test suite, periodt ๐Ÿ‘‘โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from