Bench.pm

filter: Statementstmt 100% 46 / 46 Branchbran 66.6% 4 / 6 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 94.3% 67 / 71 CC 11 SCAR 12.7
CC
11
Cov
95%
run
6.0
add_items
4.0
report
3.1
CRAP 3.5
↑ Summary Bench/Report/Text.pm »
1 package Tool::Bench;
2 {
3 2 $Tool::Bench::VERSION = '0.003';
4 }
5 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use List::Util qw{shuffle};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Tool Bench is a generic framework for running benchmarks.
10  
11 =head1 NAME
12  
13 Tool::Bench - Tool Bench is a generic framework for running benchmarks.
14  
15 =head1 SYNOPSIS
16  
17 Ok so I'm sure your asking your self, yet another benchmarking suit? Sure there
18 are many others but this one is not specific to Perl. Think of Tool::Bench more
19 as a jazzy version of the unix 'time' command it just happens to be written in
20 perl. With 'time' you have a very simple wrap a clock around this command for
21 one run. Tool::Bench goes a bit further by wrapping a clock around the
22 execution of an number of CodeRef, run as many times as you want. Then because
23 all these times are stored you can build reports from the results of all these
24 runs.
25  
26 That said Tool::Bench is designed to just be the clock engine, you
27 have to draw the line somewhere. So here's a quick example of usage.
28  
29 use Tool::Bench;
30 my $bench = Tool::Bench->new;
31  
32 # simplest case: add a code ref with a name
33 $bench->add_items( simple => sub{...} );
34  
35 # slightly more complex item: now with events
36 $bench->add_items( complex => { startup => sub{...},
37 code => sub{...},
38 teardown => sub{...},
39 },
40 );
41  
42 # add items can takes a hash so you can add more then one item
43 $bench->add_items( name1 => sub{...},
44 name2 => { startup => sub{...},
45 code => sub{...},
46 },
47 name3 => sub{...},
48 );
49  
50 # now that your all set up, you'll want to run them
51 # lets say that you want to run each item 3 times
52 $bench->run(3);
53  
54 # now you've got a bunch of data stored off... lets do something with it.
55  
56 $bench->report(format => 'Text');
57
58 =head1 ATTRIBUTES
59  
60 =head2 items
61  
62 This is the store for all the items to be bench marked. When called directly
63 you will get an arrayref of Item objects.
64  
65 =cut
66  
67 has items =>
68 is => 'rw',
69 isa => 'ArrayRef[Tool::Bench::Item]',
70 lazy => 1,
71 1 ▶ default => sub{[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
72 2 ;
73  
74 =head1 METHODS
75  
76 =head2 items_count
77  
78 Returns the count of the number of items currently stored.
79  
80 =cut
81  
82 4 ▶sub items_count { scalar( @{ shift->items } ) };
Subroutine items_count: called CC=1 SCAR=0.0
Pod documented
83  
84 =head2 add_items
85  
86 $bench->add_items( $name => $coderef );
87 $bench->add_items( $name => { startup => $coderef,
88 code => $coderef,
89 teardown => $coderef,
90 #verify => $coderef, # currently not implimented
91 }
92 ...
93 );
94  
95 This method will take your input and build new Item objects and store them
96 in the items stack. See L<Tool::Bench::Item> for more info on the events.
97  
98 Returns items_count.
99 =cut
100  
101 3 ▶sub add_items {
Subroutine add_items: called CC=4 SCAR=13.9
Pod documented
102 3 require Tool::Bench::Item;
103 3 my $self = shift;
104 3 my %items = @_;
105 3 for my $name ( keys %items ) {
106 4 my $ref = ref($items{$name});
107 my $new = $ref eq 'CODE' ? {code => $items{$name}}
108 4 ▶ : $ref eq 'HASH' ? $items{$name}
Branch: 2 branches
BranchTrueFalseTotal
$ref eq 'CODE' ? : 3 1 4
$ref eq 'HASH' ? : 1 0 1
109 : {};
110  
111 4 push @{$self->items}, Tool::Bench::Item->new( name => $name, %$new );
112 }
113 3 return $self->items_count;
114 }
115  
116 =head2 run
117  
118  
119 When you are done adding all your items, you'll want to run them. Run takes an
120 int that refers to the number of times that you want to run each item, the
121 default is 1.
122  
123 $bench->run; # fire off the run method of all known items in shuffled order
124 $bench->run(3); # run all items 3 times, random order non-sequential runs
125  
126 Returns the number of times that it ran each item.
127  
128 =cut
129  
130 2 ▶sub run {
Subroutine run: called CC=6 SCAR=17.9
Pod documented
131 2 my $self = shift;
132 2 ▶ my $times = shift || 1;
Condition100%2/2
shift() || 1
l!l
11
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
133 2 my $count = 0;
134 2 $_->pre_run->() for @{ $self->items }; # pre run even tripping
135 2 foreach my $i (1..int($times)) {
136 4 foreach my $item ( shuffle( @{ $self->items } ) ) {
137 16 $item->run;
138 16 $count++;
139 }
140 }
141 2 $_->post_run->() for @{ $self->items }; # post run even tripping
142 2 $count; # seems completely pointless but should return something at least marginally useful
143 }
144  
145  
146 #---------------------------------------------------------------------------
147 # REPORTING
148 #---------------------------------------------------------------------------
149  
150 =head2 report
151  
152 Lastly, once you've run the items, you'll likely want to mine them and build
153 a report. The report method by default will return a Text report, though you
154 can ask for other formats. The 'format' value is expected to be the last part
155 of the class to generate the report.
156  
157 $bench->report(format => 'Text'); # uses Tool::Bench::Report::Text
158 $bench->report(format => 'JSON'); # uses Tool::Bench::Report::JSON
159  
160 By using class names you can build your own report simply, see
161 L<Tool::Bench::Report::Text> for more info on how to build report types.
162  
163 =cut
164  
165 2 ▶sub report {
Subroutine report: called CC=3 SCAR=11.3
Pod documented
166 2 my ($self, %args) = @_;
167 2 ▶ my $type = $args{format} || 'Text';
Condition50%1/2
$args{'format'} || 'Text'
l!l
20
MC/DC0%0/1
$args{'format'} || 'Text'
$args{'format'}
Truth table50%1/2
A
A= $args{'format'}
Aresult
11
00
168 2 my $class = qq{Tool::Bench::Report::$type};
169 2 ▶ eval qq{require $class} or die $@; #TODO this is messy
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class" or die $@ 2 0 2
170 2 $class->new->report(
171 items => $self->items,
172 %args,
173 );
174 }
175  
176 2 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
177 2 1;
↑ Summary Bench/Report/Text.pm »