summaryrefslogtreecommitdiffstats
path: root/lib/Reply/Plugin/Timer.pm
blob: bef91d757a87df659949712f6e176cb33422ad9f (plain) (blame)
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
package main;
use strict;
use warnings;
# ABSTRACT: time commands

use mop;

use Time::HiRes qw(gettimeofday tv_interval);

=head1 SYNOPSIS

  ; .replyrc
  [Timer]
  mintime = 0.01

=head1 DESCRIPTION

This plugin prints timer info for results that take longer than C<mintime>.
the default C<mintime> is C<< 0.01 >> seconds.

=cut

class Reply::Plugin::Timer extends Reply::Plugin {
    has $mintime = 0.01;

    method execute ($next, @args) {
        my $t0 = [gettimeofday];
        my $ret = $next->(@args);
        my $elapsed = tv_interval($t0);

        if ($elapsed > $mintime) {
            if ($elapsed >= 1) {
                printf "Execution Time: %0.3fs\n", $elapsed
            } else {
                printf "Execution Time: %dms\n", $elapsed * 1000
            }
        }

        return $ret;
    }
}

1;