Log-Timer

 view release on metacpan or  search on metacpan

lib/Guard/Timer.pm  view on Meta::CPAN

# ABSTRACT: a scope guard that keeps time


our @EXPORT = our @EXPORT_OK = qw/ timer_guard /;

use Carp;
use Time::HiRes qw/ gettimeofday tv_interval /;
use Guard;


sub timer_guard(&;$) { ## no critic(ProhibitSubroutinePrototypes)
    my ($subref, $decimal_points) = @_;
    $decimal_points ||= 3;
    $decimal_points =~ /\A\d+\Z/
        or croak("timer_guard: Number of decimal points isn't an integer");

    my $t0 = [ gettimeofday() ];

    return guard {
        my $duration = sprintf( "%.${decimal_points}f", tv_interval( $t0 ) );
        $subref->($duration);

t/log.t  view on Meta::CPAN

use strict;
use warnings;
use Test::Most;
use Log::Any::Adapter 'Test';
use Time::HiRes 'usleep';
use Log::Timer;

sub assert_log(&$;$) {
    my ($code,$log_re,$name) = @_;
    $name ||= 'event';

    Log::Any::Adapter::Test->clear;

    $code->();

    Log::Any::Adapter::Test->contains_ok(
        $log_re,
        "the $name should be logged as expected",



( run in 0.258 second using v1.01-cache-2.11-cpan-49f99fa48dc )