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