Add another MOOSE_TEST_MD option, MooseX
[gitmo/Moose.git] / benchmarks / immutable.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Scalar::Util 'blessed';
7 use Benchmark qw[cmpthese];
8
9 use Moose::Util::TypeConstraints;
10
11 {
12     package Foo;
13     use Moose;
14     Foo->meta->make_immutable(debug => 0);
15 }
16
17 coerce 'Foo'
18     => from 'ArrayRef'
19     => via { Foo->new(@{$_}) };
20
21 {
22     package Foo::Normal;
23     use Moose;
24
25     has 'default'         => (is => 'rw', default => 10);
26     has 'default_sub'     => (is => 'rw', default => sub { [] });
27     has 'lazy'            => (is => 'rw', default => 10, lazy => 1);
28     has 'required'        => (is => 'rw', required => 1);
29     has 'weak_ref'        => (is => 'rw', weak_ref => 1);
30     has 'type_constraint' => (is => 'rw', isa => 'Foo');
31     has 'coercion'        => (is => 'rw', isa => 'Foo', coerce => 1);
32
33     package Bar::Normal;
34     use Moose;
35
36     extends 'Foo::Normal';
37
38     has 'default_w_type_constraint' => (
39         is      => 'rw',
40         isa     => 'Int',
41         default => 10,
42     );
43 }
44
45 {
46     package Foo::Immutable;
47     use Moose;
48
49     has 'default'         => (is => 'rw', default => 10);
50     has 'default_sub'     => (is => 'rw', default => sub { [] });
51     has 'lazy'            => (is => 'rw', default => 10, lazy => 1);
52     has 'required'        => (is => 'rw', required => 1);
53     has 'weak_ref'        => (is => 'rw', weak_ref => 1);
54     has 'type_constraint' => (is => 'rw', isa => 'Foo');
55     has 'coercion'        => (is => 'rw', isa => 'Foo', coerce => 1);
56
57     #sub BUILD {
58     #    # ...
59     #}
60
61     Foo::Immutable->meta->make_immutable(debug => 0);
62
63     package Bar::Immutable;
64     use Moose;
65
66     extends 'Foo::Immutable';
67
68     has 'default_w_type_constraint' => (
69         is      => 'rw',
70         isa     => 'Int',
71         default => 10,
72     );
73
74     Bar::Immutable->meta->make_immutable(debug => 0);
75 }
76
77 #__END__
78
79 my $foo = Foo->new;
80
81 cmpthese(10_000,
82     {
83         'normal' => sub {
84             Foo::Normal->new(
85                 required        => 'BAR',
86                 type_constraint => $foo,
87                 coercion        => [],
88                 weak_ref        => {},
89             );
90         },
91         'immutable' => sub {
92             Foo::Immutable->new(
93                 required        => 'BAR',
94                 type_constraint => $foo,
95                 coercion        => [],
96                 weak_ref        => {},
97             );
98         },
99     }
100 );