Dumbbench/Instance/Cmd.pm

filter: Statementstmt 30.9% 22 / 71 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 70.0% 7 / 10 Podpod 100% 3 / 3 totaltotal 28.3% 32 / 113 CC 14 SCAR 39.5
CC
14
Cov
22%
single_dry_run
110.0
single_run
20.0
clone
6.0
CRAP 52.1
↑ Summary Benchmark/Dumb.pm »
1 package Dumbbench::Instance::Cmd;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Dumbbench::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use parent 'Dumbbench::Instance';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Class::XSAccessor {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 getters => [qw(
12 command
13 dry_run_command
14 )],
15 accessors => [qw(
16 use_shell
17 )],
18 3 };
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<Dumbbench::Instance> 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<Dumbbench::Instance::Cmd> constructor requires a C<command>
54 parameter. C<command> 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<system> builtin).
58  
59 Optionally, you can provide a C<dry_run_command> option.
60 It has the same structure and purpose as the C<command>
61 option, but it is used for the dry-runs. If C<dry_run_command>
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 0 ▶sub clone {
Subroutine clone: not called CC=2 SCAR=17.9
Pod documented
77 0 my $self = shift;
78 0 my $clone = $self->SUPER::clone(@_);
79 0 ▶ if (defined $self->command) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->command) 0 0 0
80 0 $clone->{command} = [@{$self->command}];
81 }
82 0 return $clone;
83 }
84  
85 0 ▶sub single_run {
Subroutine single_run: not called CC=4 SCAR=30.0
Pod documented
86 0 my $self = shift;
87  
88 0 ▶ my @cmd = (ref($self->{command}) ? @{$self->{command}} : ($self->{command}));
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'command'} ? : 0 0 0
89 0 ▶ @cmd = ("") if not @cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless @cmd 0 0 0
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 ▶ if ($self->use_shell) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_shell) { } 0 0 0
95 0 my $cmd = join ' ', @cmd;
96 0 $start = clock_gettime(CLOCK_MONOTONIC);
97 0 system($cmd);
98 0 $end = clock_gettime(CLOCK_MONOTONIC);
99 }
100 else {
101 0 my $cmd = $cmd[0];
102 0 $start = clock_gettime(CLOCK_MONOTONIC);
103 0 system({$cmd} @cmd);
104 0 $end = clock_gettime(CLOCK_MONOTONIC);
105 }
106  
107 0 my $duration = $end-$start;
108 0 return $duration;
109 }
110  
111 0 ▶sub single_dry_run {
Subroutine single_dry_run: not called CC=10 SCAR=47.0
Pod documented
112 0 my $self = shift;
113  
114 0 my @cmd;
115  
116 0 ▶ if (defined $self->{dry_run_command}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'dry_run_command'}) { } 0 0 0
117 0 ▶ @cmd = (ref($self->{dry_run_command}) ? @{$self->{dry_run_command}} : ($self->{dry_run_command}));
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'dry_run_command'} ? : 0 0 0
118 }
119 else {
120 0 ▶ my @orig_cmd = (ref($self->{command}) ? @{$self->{command}} : ($self->{command}));
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'command'} ? : 0 0 0
121 0 ▶ if (@orig_cmd and $orig_cmd[0] =~ /(?:^|\b)perl(?:\d+\.\d+\.\d+)?/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@orig_cmd and $orig_cmd[0] =~ /(?:^|\b)perl(?:\d+\.\d+\.\d+)?/) 0 0 0
Condition0%0/3
@orig_cmd and $orig_cmd[0] =~ /(?:^|\b)perl(?:\d+\.\d+\.\d+)?/
!ll&&!rl&&r
000
MC/DC0%0/2
@orig_cmd and $orig_cmd[0] =~ /(?:^|\b)perl(?:\d+\.\d+\.\d+)?/
@orig_cmd $orig_cmd[0] =~ /(?:^|\b)perl(?:\d+\.\d+\.\d+)?/
Truth table0%0/3
A and B
A= @orig_cmdB= $orig_cmd[0] =~ /(?:^|\b)perl(?:\d+\.\d+\.\d+)?/
ABresult
0X0
100
111
122 0 @cmd = ($orig_cmd[0], '-e', '1');
123 }
124 }
125 0 ▶ if (!@cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@cmd) 0 0 0
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 ▶ if ($self->use_shell) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_shell) { } 0 0 0
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
Branch: 1 branch
BranchTrueFalseTotal
if (($start = &Time::HiRes::time()) <= $tbase + "1e-15") 0 0 0
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
Branch: 1 branch
BranchTrueFalseTotal
if (($start = &Time::HiRes::time()) <= $tbase + "1e-15") 0 0 0
142 0 system({$cmd} @cmd);
143 0 $end = Time::HiRes::time();
144 }
145  
146 0 my $duration = $end-$start;
147 0 return $duration;
148 }
149  
150  
151 3 1;
152  
153  
154 __END__
↑ Summary Benchmark/Dumb.pm »