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