version 0.37_04
[gitmo/Mouse.git] / benchmarks / modifiers.pl
CommitLineData
3fac36be 1#!perl -w
2use strict;
3use Benchmark qw(:all);
4
5use Config;
6
7use Moose ();
8use Mouse ();
9use Class::Method::Modifiers ();
10
11printf "Perl %vd on $Config{archname}\n", $^V;
12my @mods = qw(Moose Mouse Class::Method::Modifiers);
13
14if(eval{ require Class::Method::Modifiers::Fast }){
15 push @mods, 'Class::Method::Modifiers::Fast';
16}
17
18foreach my $class(@mods){
19 print "$class ", $class->VERSION, "\n";
20}
21print "\n";
22
23{
24 package Base;
25 sub f{ 42 }
26 sub g{ 42 }
27 sub h{ 42 }
28}
29
30my $i = 0;
31sub around{
32 my $next = shift;
33 $i++;
34 goto &{$next};
35}
36{
37 package CMM;
38 use parent -norequire => qw(Base);
39 use Class::Method::Modifiers;
40
41 before f => sub{ $i++ };
42 around g => \&main::around;
43 after h => sub{ $i++ };
44}
45{
46 package MooseClass;
47 use parent -norequire => qw(Base);
48 use Moose;
49
50 before f => sub{ $i++ };
51 around g => \&main::around;
52 after h => sub{ $i++ };
53}
54{
55 package MouseClass;
56 use parent -norequire => qw(Base);
57 use Moose;
58
59 before f => sub{ $i++ };
60 around g => \&main::around;
61 after h => sub{ $i++ };
62}
63
64print "Calling methods with before modifiers:\n";
65cmpthese -1 => {
66 CMM => sub{
67 my $old = $i;
68 CMM->f();
69 $i == ($old+1) or die $i;
70 },
71 Moose => sub{
72 my $old = $i;
73 MooseClass->f();
74 $i == ($old+1) or die $i;
75 },
76 Mouse => sub{
77 my $old = $i;
78 MouseClass->f();
79 $i == ($old+1) or die $i;
80 },
81};
82
83print "\n", "Calling methods with around modifiers:\n";
84cmpthese -1 => {
85 CMM => sub{
86 my $old = $i;
87 CMM->g();
88 $i == ($old+1) or die $i;
89 },
90 Moose => sub{
91 my $old = $i;
92 MooseClass->g();
93 $i == ($old+1) or die $i;
94 },
95 Mouse => sub{
96 my $old = $i;
97 MouseClass->g();
98 $i == ($old+1) or die $i;
99 },
100};
101
102print "\n", "Calling methods with after modifiers:\n";
103cmpthese -1 => {
104 CMM => sub{
105 my $old = $i;
106 CMM->h();
107 $i == ($old+1) or die $i;
108 },
109 Moose => sub{
110 my $old = $i;
111 MooseClass->h();
112 $i == ($old+1) or die $i;
113 },
114 Mouse => sub{
115 my $old = $i;
116 MouseClass->h();
117 $i == ($old+1) or die $i;
118 },
119};