File Coverage

blib/lib/Dumbbench/Instance/Cmd.pm
Criterion Covered Total %
statement 21 70 30.0
branch 0 20 0.0
condition 0 3 0.0
subroutine 7 10 70.0
pod 3 3 100.0
total 31 106 29.2


line stmt bran cond sub pod time code
1             package Dumbbench::Instance::Cmd;
2 3     3   19 use strict;
  3         6  
  3         72  
3 3     3   12 use warnings;
  3         6  
  3         56  
4 3     3   12 use Carp ();
  3         5  
  3         34  
5 3     3   12 use Time::HiRes ();
  3         4  
  3         57  
6              
7 3     3   13 use Dumbbench::Instance;
  3         4  
  3         68  
8 3     3   13 use parent 'Dumbbench::Instance';
  3         4  
  3         23  
9              
10             use Class::XSAccessor {
11 3         24 getters => [qw(
12             command
13             dry_run_command
14             )],
15             accessors => [qw(
16             use_shell
17             )],
18 3     3   197 };
  3         8  
19              
20             =encoding utf8
21              
22             =head1 NAME
23              
24             Dumbbench::Instance::Cmd - Benchmarks an external command
25              
26             =head1 SYNOPSIS
27              
28             use Dumbbench;
29              
30             my $bench = Dumbbench->new(
31             target_rel_precision => 0.005, # seek ~0.5%
32             initial_runs => 20, # the higher the more reliable
33             );
34             $bench->add_instances(
35             Dumbbench::Instance::Cmd->new(name => 'mauve', command => [qw(perl -e 'something')]),
36             # ... more things to benchmark ...
37             );
38             $bench->run();
39             # ...
40              
41             =head1 DESCRIPTION
42              
43             This class inherits from L and implements
44             benchmarking of external commands.
45              
46             =head1 METHODS
47              
48             =head2 new
49              
50             Constructor that takes named arguments.
51              
52             In addition to the properties of the base class, the
53             C constructor requires a C
54             parameter. C can either be string specifying the
55             external command with its options or (preferably) a
56             reference to an array of command-name and options
57             (as with the ordinary C builtin).
58              
59             Optionally, you can provide a C option.
60             It has the same structure and purpose as the C
61             option, but it is used for the dry-runs. If C
62             is not specified, the dry-run will consist of starting
63             another process that immediately exits.
64              
65             =head2 command
66              
67             Returns the command that was set on object construction.
68              
69             =head2 dry_run_command
70              
71             Returns the command that was set for dry-runs on object construction.
72              
73             =cut
74              
75              
76             sub clone {
77 0     0 1   my $self = shift;
78 0           my $clone = $self->SUPER::clone(@_);
79 0 0         if (defined $self->command) {
80 0           $clone->{command} = [@{$self->command}];
  0            
81             }
82 0           return $clone;
83             }
84              
85             sub single_run {
86 0     0 1   my $self = shift;
87              
88 0 0         my @cmd = (ref($self->{command}) ? @{$self->{command}} : ($self->{command}));
  0            
89 0 0         @cmd = ("") if not @cmd;
90             #my $start;
91             #my $tbase = Time::HiRes::time();
92             #while ( ($start = Time::HiRes::time()) <= $tbase+1.e-15 ) {} # wait for clock tick. See discussion in Benchmark.pm comments
93 0           my ($start, $end);
94 0 0         if ($self->use_shell) {
95 0           my $cmd = join ' ', @cmd;
96 0           $start = Time::HiRes::time();
97 0           system($cmd);
98 0           $end = Time::HiRes::time();
99             }
100             else {
101 0           my $cmd = $cmd[0];
102 0           $start = Time::HiRes::time();
103 0           system({$cmd} @cmd);
  0            
104 0           $end = Time::HiRes::time();
105             }
106              
107 0           my $duration = $end-$start;
108 0           return $duration;
109             }
110              
111             sub single_dry_run {
112 0     0 1   my $self = shift;
113              
114 0           my @cmd;
115              
116 0 0         if (defined $self->{dry_run_command}) {
117 0 0         @cmd = (ref($self->{dry_run_command}) ? @{$self->{dry_run_command}} : ($self->{dry_run_command}));
  0            
118             }
119             else {
120 0 0         my @orig_cmd = (ref($self->{command}) ? @{$self->{command}} : ($self->{command}));
  0            
121 0 0 0       if (@orig_cmd and $orig_cmd[0] =~ /(?:^|\b)perl(?:\d+\.\d+\.\d+)?/) {
122 0           @cmd = ($orig_cmd[0], '-e', '1');
123             }
124             }
125 0 0         if (!@cmd) {
126             # FIXME For lack of a better dry run test, we always use perl for now as a fallback
127 0           @cmd = ($^X, qw(-e 1));
128             }
129              
130 0           my ($start, $end);
131 0 0         if ($self->use_shell) {
132 0           my $cmd = join ' ', @cmd;
133 0           my $tbase = Time::HiRes::time();
134 0           while ( ($start = Time::HiRes::time()) <= $tbase+1.e-15 ) {} # wait for clock tick. See discussion in Benchmark.pm comments
135 0           system($cmd);
136 0           $end = Time::HiRes::time();
137             }
138             else {
139 0           my $cmd = $cmd[0];
140 0           my $tbase = Time::HiRes::time();
141 0           while ( ($start = Time::HiRes::time()) <= $tbase+1.e-15 ) {} # wait for clock tick. See discussion in Benchmark.pm comments
142 0           system({$cmd} @cmd);
  0            
143 0           $end = Time::HiRes::time();
144             }
145              
146 0           my $duration = $end-$start;
147 0           return $duration;
148             }
149              
150              
151             1;
152              
153              
154             __END__