add an easy benchmark harness
[gitmo/Class-MOP.git] / bench / lib / Bench / Run.pm
1 #!/usr/bin/perl
2
3 package Bench::Run;
4 use Moose;
5
6 use Benchmark qw/:hireswallclock :all/;
7
8 has classes => (
9     isa => "ArrayRef",
10     is  => "rw",
11     auto_deref => 1,
12 );
13
14 has benchmarks => (
15     isa => "ArrayRef",
16     is  => "rw",
17     auto_deref => 1,
18 );
19
20 has min_time => (
21     isa => "Num",
22     is  => "rw",
23     default => 5,
24 );
25
26 sub run {
27     my $self = shift;
28
29     foreach my $bench ( $self->benchmarks ) {
30         my $bench_class = $bench->{class};
31         my @bench_args  = ( (ref($bench->{args}) eq "ARRAY") ? @{ $bench->{args} } : %{ $bench->{args} } );
32
33         eval "require $bench_class";
34         die $@ if $@;
35
36         my %res;
37
38         foreach my $class ( $self->classes ) {
39             eval "require $class";
40             die $@ if $@;
41
42             my $b = $bench_class->new( @bench_args, class => $class );
43             $res{$class} = countit( $self->min_time, $b->code );
44         }
45
46         print "$bench_class:\n";
47         cmpthese( \%res );
48         print "\n";
49     }
50 }
51
52 __PACKAGE__;
53
54 __END__