merged PR
[catagits/Catalyst-Runtime.git] / t / unit_stats.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 13;
5 use Time::HiRes qw/gettimeofday/;
6 use Tree::Simple;
7
8 my @fudge_t = ( 0, 0 );
9 BEGIN {
10     no warnings;
11     *Time::HiRes::gettimeofday = sub () { return @fudge_t };
12 }
13
14 BEGIN { use_ok("Catalyst::Stats") };
15
16 {
17     my $stats = Catalyst::Stats->new;
18     is (ref($stats), "Catalyst::Stats", "new");
19
20     is_deeply([ $stats->created ], [0, 0], "created time");
21
22     my @expected; # level, string, time
23
24     $fudge_t[0] = 1;
25     ok($stats->profile("single comment arg"), "profile");
26     push(@expected, [ 0, "- single comment arg", 1, 0 ]);
27
28     $fudge_t[0] = 3;
29     $stats->profile(comment => "hash comment arg");
30     push(@expected, [ 0, "- hash comment arg", 2, 0 ]);
31
32     $fudge_t[0] = 10;
33     $stats->profile(begin => "block", comment => "start block");
34     push(@expected, [ 0, "block - start block", 4, 1 ]);
35
36
37     $fudge_t[0] = 11;
38     $stats->profile("inside block");
39     push(@expected, [ 1, "- inside block", 1, 0 ]);
40
41     $fudge_t[1] = 100000;
42     my $uid = $stats->profile(begin => "nested block", uid => "boo");
43     push(@expected, [ 1, "nested block", 0.7, 1 ]);
44     is ($uid, "boo", "set UID");
45
46     $stats->enable(0);
47     $fudge_t[1] = 150000;
48     $stats->profile("this shouldn't appear");
49     $stats->enable(1);
50
51     $fudge_t[1] = 200000;
52     $stats->profile(begin => "double nested block 1");
53     push(@expected, [ 2, "double nested block 1", 0.2, 1 ]);
54
55     $stats->profile(comment => "attach to uid", parent => $uid);
56
57     $fudge_t[1] = 250000;
58     $stats->profile(begin => "badly nested block 1");
59     push(@expected, [ 3, "badly nested block 1", 0.35, 1 ]);
60
61     $fudge_t[1] = 300000;
62     $stats->profile(comment => "interleave 1");
63     push(@expected, [ 4, "- interleave 1", 0.05, 0 ]);
64
65     $fudge_t[1] = 400000; # end double nested block time
66     $stats->profile(end => "double nested block 1");
67
68     $fudge_t[1] = 500000;
69     $stats->profile(comment => "interleave 2");
70     push(@expected, [ 4, "- interleave 2", 0.2, 0 ]);
71
72     $fudge_t[1] = 550000;
73     $stats->profile(begin => "begin with no end");
74     push(@expected, [ 4, "begin with no end", 0.05, 1 ]);
75
76     $fudge_t[1] = 600000; # end badly nested block time
77     $stats->profile(end => "badly nested block 1");
78
79     $fudge_t[1] = 800000; # end nested block time
80     $stats->profile(end => "nested block");
81
82     $fudge_t[0] = 14; # end block time
83     $fudge_t[1] = 0;
84     $stats->profile(end => "block", comment => "end block");
85
86     push(@expected, [ 2, "- attach to uid", 0.1, 0 ]);
87
88
89     my @report = $stats->report;
90     is_deeply(\@report, \@expected, "report");
91
92     # print scalar($stats->report);
93
94     is ($stats->elapsed, 14, "elapsed");
95 }
96
97 # COMPATABILITY METHODS
98
99 # accept
100 {
101     my $stats = Catalyst::Stats->new;
102     my $root = $stats->{tree};
103     my $uid = $root->getUID;
104
105     my $visitor = Tree::Simple::Visitor::FindByUID->new;
106     $visitor->includeTrunk(1); # needed for this test
107     $visitor->searchForUID($uid);
108     $stats->accept($visitor);
109     is( $visitor->getResult, $root, '[COMPAT] accept()' );
110
111 }
112
113 # addChild
114 {
115     my $stats = Catalyst::Stats->new;
116     my $node = Tree::Simple->new(
117         {
118             action  => 'test',
119             elapsed => '10s',
120             comment => "",
121         }
122     );
123
124     $stats->addChild( $node );
125
126     my $actual = $stats->{ tree }->{ _children }->[ 0 ];
127     is( $actual, $node, '[COMPAT] addChild()' );
128     is( $actual->getNodeValue->{ elapsed }, 10, '[COMPAT] addChild(), data munged' );
129 }
130
131 # setNodeValue
132 {
133     my $stats = Catalyst::Stats->new;
134     my $stat = {
135         action  => 'test',
136         elapsed => '10s',
137         comment => "",
138     };
139
140     $stats->setNodeValue( $stat );
141
142     is_deeply( $stats->{tree}->getNodeValue, { action => 'test', elapsed => 10, comment => '' }   , '[COMPAT] setNodeValue(), data munged' );
143 }
144
145 # getNodeValue
146 {
147     my $stats = Catalyst::Stats->new;
148     my $expected = $stats->{tree}->getNodeValue->{t};
149     is_deeply( $stats->getNodeValue, $expected, '[COMPAT] getNodeValue()' );
150 }
151
152 # traverse
153 {
154     my $stats = Catalyst::Stats->new;
155     $stats->{tree}->addChild( Tree::Simple->new( { foo => 'bar' } ) );
156     my @value;
157     $stats->traverse( sub { push @value, shift->getNodeValue->{ foo }; } );
158
159     is_deeply( \@value, [ 'bar' ], '[COMPAT] traverse()' );
160 }
161