blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/Runtime.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 78 CC 11 SCAR 45.8
CC
11
Cov
0%
init
132.0
BEGIN
2.0
BEGIN
2.0
CRAP 97.3
1 package Dwarf::Plugin::Runtime;
2 use Dwarf::Pragma;
3 use Dwarf::Util qw/add_method/;
4 use Carp;
5 use Time::HiRes;
6  
7 sub init {
8 my ($class, $c, $conf) = @_;
9 $conf ||= {};
10  
11 # コマンドラインプログラムの時の振る舞い
12 if (defined $conf->{cli} and $conf->{cli} == 0) {
13 if ($c->is_cli) {
14 return;
15 }
16 }
17  
18 # ランタイム出力を回避(Plugin::MultiConfig と連動)
19 if (my $ignore = $conf->{ignore}) {
20 if ($c->can('config_name')) {
21 if ($c->config_name =~ /$ignore/) {
22 return;
23 }
24 }
25 }
26  
27 $c->{'dwarf.runtime'} = 1;
28 add_method($c, runtime => sub {
29 my $self = shift;
30 if (@_ == 1) {
31 $self->{'dwarf.runtime'} = $_[0];
32 }
33 return $self->{'dwarf.runtime'};
34 });
35  
36 my $start;
37  
38 $c->add_trigger('BEFORE_DISPATCH' => sub {
39 my $self = shift;
40 $start = [Time::HiRes::gettimeofday];
41 });
42  
43 $c->add_trigger('AFTER_DISPATCH' => sub {
44 my $self = shift;
45 return unless defined $self->handler;
46 return unless $self->runtime;
47  
48 my $run_time = Time::HiRes::tv_interval($start);
49 my $message = "[RUN TIME] " . $self->handler_class . ": " . $run_time;
50  
51 if ($self->can('log')) {
52 if ($self->can('now')) {
53 $message .= sprintf " (%s %s)", $self->now->ymd('/'), $self->now->hms;
54 }
55
56 $self->log->log(level => 'info', message => $message);
57 } else {
58 print STDERR $message . "\n";
59 }
60 });
61 }
62  
63 1;