3 use Benchmark qw/:hireswallclock cmpthese/;
4 use Getopt::Long::Descriptive;
7 $ENV{PERL5LIB} = join ($Config{path_sep}, @INC);
12 ($opts, $usage) = describe_options(
14 [ 'help|h' => 'Print usage message and exit' ],
15 [ 'bench|b:s' => 'Which benchmarks to run (all|xs|pp)', { default => 'all', regex => qr/^(?:all|xs|pp)$/ } ],
16 [ 'iterations|i:i' => 'How many iterations in each bench run (def 1000)', { default => 1000 } ],
17 [ 'reuse|r' => 'Reuse the object between benchmark runs' ],
18 { getopt_conf => [qw/gnu_getopt bundling_override no_ignore_case/] },
28 eval "require $_" or push @missing, $_;
32 die sprintf "Missing modules necessary for benchmark:\n\n%s\n\n",
33 join ("\n", @missing);
37 use Method::Generate::Accessor; # need to pre-load for the XS shut-off to work
39 $usage->die if $opts->{help};
41 my $attrs_to_bench = {
42 plain => q|is => 'rw' |,
43 lazy_default => q|is => 'rw', lazy => 1, default => sub { {} } |,
44 lazy_default_qsub => q|is => 'rw', lazy => 1, default => Sub::Quote::quote_sub q{ {} } |,
49 if ($opts->{bench} =~ /all|pp/) {
51 local $Method::Generate::Accessor::CAN_HAZ_XS = 0;
52 _add_moosey_has (moo => 'Moo');
55 _add_moosey_has (moose => 'Moose');
56 _add_moosey_has (mouse => 'Mousse')
59 if ($opts->{bench} =~ /all|xs/) {
61 local $Method::Generate::Accessor::CAN_HAZ_XS = 1;
62 _add_moosey_has (moo_XS => 'Moo');
64 _add_moosey_has (mouse_XS => 'Mouse')
70 print "Perl $], take $_:\n";
74 print "\n\nBenching new()\n====================\n";
76 cmpthese ( -1, { map {
78 "${type}->new" => sub {
79 $objects->{$type} = $class_types->{$type}->new
80 for (1 .. $opts->{iterations});
82 } keys %$class_types } );
84 for my $attr (keys %$attrs_to_bench) {
85 print "\n\nBenching $attr\n====================\n";
87 cmpthese ( -1, { map {
89 "${type}->$attr" => sub {
90 $objects->{$type} = $class_types->{$type}->new
91 unless $opts->{reuse};
93 for (1 .. $opts->{iterations} ) {
94 my $init = $objects->{$type}->$attr;
95 $objects->{$type}->$attr('foo');
96 my $set = $objects->{$type}->$attr;
105 sub _add_moosey_has {
106 my ($name, $base) = @_;
108 my $class = "Bench::${name}";
110 my $perl = "package $class; use $base;";
112 for my $attr (keys %$attrs_to_bench) {
113 $perl .= "has $attr => ($attrs_to_bench->{$attr});";
115 $class_types->{$name} = $class;
118 $perl .= 'eval { __PACKAGE__->meta->make_immutable };';