File: unit_stats.t

package info (click to toggle)
libcatalyst-perl 5.90132-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: perl: 11,061; makefile: 7
file content (166 lines) | stat: -rw-r--r-- 4,487 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# HARNESS-NO-PRELOAD
use strict;
use warnings;

use Test::More tests => 13;
use Time::HiRes ();
use Tree::Simple;

my @fudge_t = ( 0, 0 );
BEGIN {
    no warnings;
    *Time::HiRes::gettimeofday = sub () { return @fudge_t };
    my $original_tv_interval = \&Time::HiRes::tv_interval;
    *Time::HiRes::tv_interval = sub ($;@) {
        return $original_tv_interval->($_[0], $_[1] || [Time::HiRes::gettimeofday()]);
    };
}

BEGIN { use_ok("Catalyst::Stats") };

{
    my $stats = Catalyst::Stats->new;
    is (ref($stats), "Catalyst::Stats", "new");

    is_deeply([ $stats->created ], [0, 0], "created time");

    my @expected; # level, string, time

    $fudge_t[0] = 1;
    ok($stats->profile("single comment arg"), "profile");
    push(@expected, [ 0, "- single comment arg", 1, 0 ]);

    $fudge_t[0] = 3;
    $stats->profile(comment => "hash comment arg");
    push(@expected, [ 0, "- hash comment arg", 2, 0 ]);

    $fudge_t[0] = 10;
    $stats->profile(begin => "block", comment => "start block");
    push(@expected, [ 0, "block - start block", 4, 1 ]);


    $fudge_t[0] = 11;
    $stats->profile("inside block");
    push(@expected, [ 1, "- inside block", 1, 0 ]);

    $fudge_t[1] = 100000;
    my $uid = $stats->profile(begin => "nested block", uid => "boo");
    push(@expected, [ 1, "nested block", 0.7, 1 ]);
    is ($uid, "boo", "set UID");

    $stats->enable(0);
    $fudge_t[1] = 150000;
    $stats->profile("this shouldn't appear");
    $stats->enable(1);

    $fudge_t[1] = 200000;
    $stats->profile(begin => "double nested block 1");
    push(@expected, [ 2, "double nested block 1", 0.2, 1 ]);

    $stats->profile(comment => "attach to uid", parent => $uid);

    $fudge_t[1] = 250000;
    $stats->profile(begin => "badly nested block 1");
    push(@expected, [ 3, "badly nested block 1", 0.35, 1 ]);

    $fudge_t[1] = 300000;
    $stats->profile(comment => "interleave 1");
    push(@expected, [ 4, "- interleave 1", 0.05, 0 ]);

    $fudge_t[1] = 400000; # end double nested block time
    $stats->profile(end => "double nested block 1");

    $fudge_t[1] = 500000;
    $stats->profile(comment => "interleave 2");
    push(@expected, [ 4, "- interleave 2", 0.2, 0 ]);

    $fudge_t[1] = 550000;
    $stats->profile(begin => "begin with no end");
    push(@expected, [ 4, "begin with no end", 0.05, 1 ]);

    $fudge_t[1] = 600000; # end badly nested block time
    $stats->profile(end => "badly nested block 1");

    $fudge_t[1] = 800000; # end nested block time
    $stats->profile(end => "nested block");

    $fudge_t[0] = 14; # end block time
    $fudge_t[1] = 0;
    $stats->profile(end => "block", comment => "end block");

    push(@expected, [ 2, "- attach to uid", 0.1, 0 ]);


    my @report = $stats->report;
    is_deeply(\@report, \@expected, "report");

    # print scalar($stats->report);

    is ($stats->elapsed, 14, "elapsed");
}

# COMPATABILITY METHODS

# accept
{
    my $stats = Catalyst::Stats->new;
    my $root = $stats->{tree};
    my $uid = $root->getUID;

    my $visitor = Tree::Simple::Visitor::FindByUID->new;
    $visitor->includeTrunk(1); # needed for this test
    $visitor->searchForUID($uid);
    $stats->accept($visitor);
    is( $visitor->getResult, $root, '[COMPAT] accept()' );

}

# addChild
{
    my $stats = Catalyst::Stats->new;
    my $node = Tree::Simple->new(
        {
            action  => 'test',
            elapsed => '10s',
            comment => "",
        }
    );

    $stats->addChild( $node );

    my $actual = $stats->{ tree }->{ _children }->[ 0 ];
    is( $actual, $node, '[COMPAT] addChild()' );
    is( $actual->getNodeValue->{ elapsed }, 10, '[COMPAT] addChild(), data munged' );
}

# setNodeValue
{
    my $stats = Catalyst::Stats->new;
    my $stat = {
        action  => 'test',
        elapsed => '10s',
        comment => "",
    };

    $stats->setNodeValue( $stat );

    is_deeply( $stats->{tree}->getNodeValue, { action => 'test', elapsed => 10, comment => '' }   , '[COMPAT] setNodeValue(), data munged' );
}

# getNodeValue
{
    my $stats = Catalyst::Stats->new;
    my $expected = $stats->{tree}->getNodeValue->{t};
    is_deeply( $stats->getNodeValue, $expected, '[COMPAT] getNodeValue()' );
}

# traverse
{
    my $stats = Catalyst::Stats->new;
    $stats->{tree}->addChild( Tree::Simple->new( { foo => 'bar' } ) );
    my @value;
    $stats->traverse( sub { push @value, shift->getNodeValue->{ foo }; } );

    is_deeply( \@value, [ 'bar' ], '[COMPAT] traverse()' );
}