ed834795c633e523468bb1a369b4a830004af295
[gitmo/Moo.git] / benchmark / object_factory
1 use strictures 1;
2
3 use Benchmark qw/:hireswallclock cmpthese/;
4 use Getopt::Long::Descriptive;
5
6 use Config;
7 $ENV{PERL5LIB} = join ($Config{path_sep}, @INC);
8
9
10 my ($opts, $usage);
11 BEGIN {
12   ($opts, $usage) = describe_options(
13     '%c: %o' =>
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/] },
19   );
20
21   my @missing;
22   for (qw/
23     Moose
24     Moo
25     Mouse
26     Mousse
27   /) {
28     eval "require $_" or push @missing, $_;
29   }
30
31   if (@missing) {
32     die sprintf "Missing modules necessary for benchmark:\n\n%s\n\n",
33       join ("\n", @missing);
34   }
35 }
36
37 use Method::Generate::Accessor; # need to pre-load for the XS shut-off to work
38
39 $usage->die if $opts->{help};
40
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{ {} } |,
45 };
46
47 my $class_types;
48
49 if ($opts->{bench} =~ /all|pp/) {
50   {
51     local $Method::Generate::Accessor::CAN_HAZ_XS = 0;
52     _add_moosey_has (moo => 'Moo');
53   }
54
55   _add_moosey_has (moose => 'Moose');
56   _add_moosey_has (mouse => 'Mousse')
57 }
58
59 if ($opts->{bench} =~ /all|xs/) {
60   {
61     local $Method::Generate::Accessor::CAN_HAZ_XS = 1;
62     _add_moosey_has (moo_XS => 'Moo');
63   }
64   _add_moosey_has (mouse_XS => 'Mouse')
65 }
66
67
68 # Actual Benchmarking
69 for (1, 2) {
70   print "Perl $], take $_:\n";
71
72   my $objects;
73
74   print "\n\nBenching new()\n====================\n";
75
76   cmpthese ( -1, { map {
77     my $type = $_;
78     "${type}->new" => sub {
79       $objects->{$type} = $class_types->{$type}->new
80         for (1 .. $opts->{iterations});
81     }
82   } keys %$class_types } );
83
84   for my $attr (keys %$attrs_to_bench) {
85     print "\n\nBenching $attr\n====================\n";
86
87     cmpthese ( -1, { map {
88       my $type = $_;
89       "${type}->$attr" => sub {
90         $objects->{$type} = $class_types->{$type}->new
91           unless $opts->{reuse};
92
93         for (1 .. $opts->{iterations} ) {
94           my $init = $objects->{$type}->$attr;
95           $objects->{$type}->$attr('foo');
96           my $set = $objects->{$type}->$attr;
97         }
98       }
99     } keys %$objects } );
100   }
101 }
102
103 exit 0; # the end
104
105 sub _add_moosey_has {
106   my ($name, $base) = @_;
107
108   my $class = "Bench::${name}";
109
110   my $perl = "package $class; use $base;";
111
112   for my $attr (keys %$attrs_to_bench) {
113     $perl .= "has $attr => ($attrs_to_bench->{$attr});";
114
115     $class_types->{$name} = $class;
116   }
117
118   $perl .= 'eval { __PACKAGE__->meta->make_immutable };';
119
120   eval $perl;
121   die $@ if $@;
122 }