35d16463299806971c861b38ac1da19083be85e0
[catagits/Catalyst-Runtime.git] / t / unit_stats.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 6;
7 use Time::HiRes qw/gettimeofday/;
8
9 my @fudge_t = ( 0, 0 );
10 BEGIN {
11     no warnings;
12     *Time::HiRes::gettimeofday = sub () { return @fudge_t };
13 }
14
15 BEGIN { use_ok("Catalyst::Stats") };
16
17
18 my $stats = Catalyst::Stats->new;
19 is (ref($stats), "Catalyst::Stats", "new");
20
21 my @expected; # level, string, time
22
23 $fudge_t[0] = 1;
24 ok($stats->profile("single comment arg"), "profile");
25 push(@expected, [ 0, "- single comment arg", 1, 0 ]);
26
27 $fudge_t[0] = 3;
28 $stats->profile(comment => "hash comment arg");
29 push(@expected, [ 0, "- hash comment arg", 2, 0 ]);
30
31 $fudge_t[0] = 10;
32 $stats->profile(begin => "block", comment => "start block");
33 push(@expected, [ 0, "block - start block", 4, 1 ]);
34
35
36 $fudge_t[0] = 11;
37 $stats->profile("inside block");
38 push(@expected, [ 1, "- inside block", 1, 0 ]);
39
40 $fudge_t[1] = 100000;
41 my $uid = $stats->profile(begin => "nested block", uid => "boo");
42 push(@expected, [ 1, "nested block", 0.7, 1 ]);
43 is ($uid, "boo", "set UID");
44
45 $stats->enable(0);
46 $fudge_t[1] = 150000;
47 $stats->profile("this shouldn't appear");
48 $stats->enable(1);
49
50 $fudge_t[1] = 200000;
51 $stats->profile(begin => "double nested block 1");
52 push(@expected, [ 2, "double nested block 1", 0.2, 1 ]);
53
54 $stats->profile(comment => "attach to uid", parent => $uid);
55
56 $fudge_t[1] = 250000;
57 $stats->profile(begin => "badly nested block 1");
58 push(@expected, [ 3, "badly nested block 1", 0.35, 1 ]);
59
60 $fudge_t[1] = 300000;
61 $stats->profile(comment => "interleave 1");
62 push(@expected, [ 4, "- interleave 1", 0.05, 0 ]);
63
64 $fudge_t[1] = 400000; # end double nested block time
65 $stats->profile(end => "double nested block 1");
66
67 $fudge_t[1] = 500000;
68 $stats->profile(comment => "interleave 2");
69 push(@expected, [ 4, "- interleave 2", 0.2, 0 ]);
70
71 $fudge_t[1] = 600000; # end badly nested block time
72 $stats->profile(end => "badly nested block 1");
73
74 $fudge_t[1] = 800000; # end nested block time
75 $stats->profile(end => "nested block");
76
77 $fudge_t[0] = 14; # end block time
78 $fudge_t[1] = 0;
79 $stats->profile(end => "block", comment => "end block");
80
81 push(@expected, [ 2, "- attach to uid", 0.1, 0 ]);
82
83
84 my @report = $stats->report;
85 is_deeply(\@report, \@expected, "report");
86
87 is ($stats->elapsed, 14, "elapsed");
88