Reworked RH perf test to be more targetted
[dbsrgits/DBIx-Class.git] / t / 99rh_perl_perf_bug.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Test::More;
5 use lib qw(t/lib);
6
7 # This is a rather unusual test.
8 # It does not test any aspect of DBIx::Class, but instead tests the
9 # perl installation this is being run under to see if it is:-
10 #  1. Potentially affected by a RH perl build bug
11 #  2. If so we do a performance test for the effect of
12 #     that bug.
13 #
14 # You can skip these tests by setting the DBIC_NO_WARN_BAD_PERL env
15 # variable
16 #
17 # If these tests fail then please read the section titled
18 # Perl Performance Issues on Red Hat Systems in
19 # L<DBIx::Class::Manual::Troubleshooting>
20
21 plan skip_all =>
22   'Skipping RH perl performance bug tests as DBIC_NO_WARN_BAD_PERL set'
23   if ( $ENV{DBIC_NO_WARN_BAD_PERL} );
24
25 eval "use Benchmark ':all'";
26 plan skip_all => 'needs Benchmark for testing' if $@;
27
28 plan tests => 3;
29
30 ok(1, 'Dummy - prevents next test timing out');
31
32 # we do a benchmark test filling an array with blessed/overloaded references,
33 # against an array filled with array refs.
34 # On a sane system the ratio between these operation sets is 1 - 1.5,
35 # whereas a bugged system gives a ratio of around 8
36 # we therefore consider there to be a problem if the ratio is >= 2
37
38 my $results = timethese(
39     -1,    # run for 1 CPU second each
40     {
41         no_bless => sub {
42             my %h;
43             for ( my $i = 0 ; $i < 10000 ; $i++ ) {
44                 $h{$i} = [];
45             }
46         },
47         bless_overload => sub {
48             use overload q(<) => sub { };
49             my %h;
50             for ( my $i = 0 ; $i < 10000 ; $i++ ) {
51                 $h{$i} = bless [] => 'main';
52             }
53         },
54     },
55 );
56
57 my $ratio = $results->{no_bless}->iters / $results->{bless_overload}->iters;
58
59 ok( ( $ratio < 2 ), 'Overload/bless performance acceptable' );
60
61 # We will only check for the difference in bless handling (whether the
62 # bless applies to the reference or the referent) if we have seen a
63 # performance issue...
64
65 SKIP: {
66     skip "Not checking for bless handling as performance is OK", 1
67       if ( $ratio < 2 );
68
69     {
70         package    # don't want this in PAUSE
71           TestRHBug;
72         use overload bool => sub { 0 }
73     }
74
75     sub _has_bug_34925 {
76         my %thing;
77         my $r1 = \%thing;
78         my $r2 = \%thing;
79         bless $r1 => 'TestRHBug';
80         return !!$r2;
81     }
82
83     sub _possibly_has_bad_overload_performance {
84         return $] < 5.008009 && !_has_bug_34925();
85     }
86
87     # If this next one fails then you almost certainly have a RH derived
88     # perl with the performance bug
89     # if this test fails, look at the section titled
90     # "Perl Performance Issues on Red Hat Systems" in
91     # L<DBIx::Class::Manual::Troubleshooting>
92     # Basically you may suffer severe performance issues when running
93     # DBIx::Class (and many other) modules.  Look at getting a fixed
94     # version of the perl interpreter for your system.
95     #
96     ok( !_possibly_has_bad_overload_performance(),
97         'Checking whether bless applies to reference not object' );
98 }