4 use Test::More tests => 13;
8 my @fudge_t = ( 0, 0 );
11 *Time::HiRes::gettimeofday = sub () { return @fudge_t };
12 my $original_tv_interval = \&Time::HiRes::tv_interval;
13 *Time::HiRes::tv_interval = sub ($;@) {
14 return $original_tv_interval->($_[0], $_[1] || [Time::HiRes::gettimeofday()]);
18 BEGIN { use_ok("Catalyst::Stats") };
21 my $stats = Catalyst::Stats->new;
22 is (ref($stats), "Catalyst::Stats", "new");
24 is_deeply([ $stats->created ], [0, 0], "created time");
26 my @expected; # level, string, time
29 ok($stats->profile("single comment arg"), "profile");
30 push(@expected, [ 0, "- single comment arg", 1, 0 ]);
33 $stats->profile(comment => "hash comment arg");
34 push(@expected, [ 0, "- hash comment arg", 2, 0 ]);
37 $stats->profile(begin => "block", comment => "start block");
38 push(@expected, [ 0, "block - start block", 4, 1 ]);
42 $stats->profile("inside block");
43 push(@expected, [ 1, "- inside block", 1, 0 ]);
46 my $uid = $stats->profile(begin => "nested block", uid => "boo");
47 push(@expected, [ 1, "nested block", 0.7, 1 ]);
48 is ($uid, "boo", "set UID");
52 $stats->profile("this shouldn't appear");
56 $stats->profile(begin => "double nested block 1");
57 push(@expected, [ 2, "double nested block 1", 0.2, 1 ]);
59 $stats->profile(comment => "attach to uid", parent => $uid);
62 $stats->profile(begin => "badly nested block 1");
63 push(@expected, [ 3, "badly nested block 1", 0.35, 1 ]);
66 $stats->profile(comment => "interleave 1");
67 push(@expected, [ 4, "- interleave 1", 0.05, 0 ]);
69 $fudge_t[1] = 400000; # end double nested block time
70 $stats->profile(end => "double nested block 1");
73 $stats->profile(comment => "interleave 2");
74 push(@expected, [ 4, "- interleave 2", 0.2, 0 ]);
77 $stats->profile(begin => "begin with no end");
78 push(@expected, [ 4, "begin with no end", 0.05, 1 ]);
80 $fudge_t[1] = 600000; # end badly nested block time
81 $stats->profile(end => "badly nested block 1");
83 $fudge_t[1] = 800000; # end nested block time
84 $stats->profile(end => "nested block");
86 $fudge_t[0] = 14; # end block time
88 $stats->profile(end => "block", comment => "end block");
90 push(@expected, [ 2, "- attach to uid", 0.1, 0 ]);
93 my @report = $stats->report;
94 is_deeply(\@report, \@expected, "report");
96 # print scalar($stats->report);
98 is ($stats->elapsed, 14, "elapsed");
101 # COMPATABILITY METHODS
105 my $stats = Catalyst::Stats->new;
106 my $root = $stats->{tree};
107 my $uid = $root->getUID;
109 my $visitor = Tree::Simple::Visitor::FindByUID->new;
110 $visitor->includeTrunk(1); # needed for this test
111 $visitor->searchForUID($uid);
112 $stats->accept($visitor);
113 is( $visitor->getResult, $root, '[COMPAT] accept()' );
119 my $stats = Catalyst::Stats->new;
120 my $node = Tree::Simple->new(
128 $stats->addChild( $node );
130 my $actual = $stats->{ tree }->{ _children }->[ 0 ];
131 is( $actual, $node, '[COMPAT] addChild()' );
132 is( $actual->getNodeValue->{ elapsed }, 10, '[COMPAT] addChild(), data munged' );
137 my $stats = Catalyst::Stats->new;
144 $stats->setNodeValue( $stat );
146 is_deeply( $stats->{tree}->getNodeValue, { action => 'test', elapsed => 10, comment => '' } , '[COMPAT] setNodeValue(), data munged' );
151 my $stats = Catalyst::Stats->new;
152 my $expected = $stats->{tree}->getNodeValue->{t};
153 is_deeply( $stats->getNodeValue, $expected, '[COMPAT] getNodeValue()' );
158 my $stats = Catalyst::Stats->new;
159 $stats->{tree}->addChild( Tree::Simple->new( { foo => 'bar' } ) );
161 $stats->traverse( sub { push @value, shift->getNodeValue->{ foo }; } );
163 is_deeply( \@value, [ 'bar' ], '[COMPAT] traverse()' );