enabling immutable finishing porting Log and stats
[catagits/Catalyst-Runtime.git] / t / unit_stats.t
CommitLineData
dc5f035e 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 6;
7use Time::HiRes qw/gettimeofday/;
8
9my @fudge_t = ( 0, 0 );
10BEGIN {
11 no warnings;
12 *Time::HiRes::gettimeofday = sub () { return @fudge_t };
13}
14
15BEGIN { use_ok("Catalyst::Stats") };
16
17
18my $stats = Catalyst::Stats->new;
19is (ref($stats), "Catalyst::Stats", "new");
20
21my @expected; # level, string, time
22
23$fudge_t[0] = 1;
24ok($stats->profile("single comment arg"), "profile");
25push(@expected, [ 0, "- single comment arg", 1, 0 ]);
26
27$fudge_t[0] = 3;
28$stats->profile(comment => "hash comment arg");
29push(@expected, [ 0, "- hash comment arg", 2, 0 ]);
30
31$fudge_t[0] = 10;
32$stats->profile(begin => "block", comment => "start block");
33push(@expected, [ 0, "block - start block", 4, 1 ]);
34
35
36$fudge_t[0] = 11;
37$stats->profile("inside block");
38push(@expected, [ 1, "- inside block", 1, 0 ]);
39
40$fudge_t[1] = 100000;
41my $uid = $stats->profile(begin => "nested block", uid => "boo");
42push(@expected, [ 1, "nested block", 0.7, 1 ]);
43is ($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");
52push(@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");
58push(@expected, [ 3, "badly nested block 1", 0.35, 1 ]);
59
60$fudge_t[1] = 300000;
61$stats->profile(comment => "interleave 1");
62push(@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");
69push(@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
81push(@expected, [ 2, "- attach to uid", 0.1, 0 ]);
82
dc5f035e 83my @report = $stats->report;
84is_deeply(\@report, \@expected, "report");
85
86is ($stats->elapsed, 14, "elapsed");
87