File Coverage

blib/lib/PerlGuard/Agent/Profile.pm
Criterion Covered Total %
statement 8 68 11.7
branch 0 2 0.0
condition n/a
subroutine 3 24 12.5
pod 0 19 0.0
total 11 113 9.7


line stmt bran cond sub pod time code
1             # This is a single web request, or a single execution of a script
2              
3             package PerlGuard::Agent::Profile;
4 1     1   35 use 5.010001;
  1         4  
5 1     1   7 use Moo;
  1         1  
  1         8  
6 1     1   66237 use Time::HiRes;
  1         7265  
  1         14  
7              
8             has agent => ( is => 'ro', required => 1, weak_ref => 1);
9              
10             has uuid => ( is => 'lazy' );
11             has start_time => ( is => 'ro' );
12             has finish_time => ( is => 'ro' );
13             has start_time_hires => ( is => 'ro' );
14             has finish_time_hires => ( is => 'ro' );
15              
16             has url => ( is => 'rw' );
17             has http_method => ( is => 'rw' );
18             has controller => ( is => 'rw' );
19             has controller_action => ( is => 'rw' );
20              
21             has should_save => ( is => 'rw', default => sub { 1 } );
22              
23             # has user; # A user definable value
24             # has script_name; # Superceeded by grouping_name which is more generic
25             # has hostname;
26             # has server_name;
27              
28             has database_transactions => ( is => 'rw', default => sub {[]});
29             has webservice_transactions => ( is => 'rw', default => sub {[]});;
30              
31             has cross_application_tracing_id => ( is => 'rw', default => sub { undef });
32              
33             sub _build_uuid {
34 0     0     my $self = shift;
35              
36 0           return "$self"; # Switch to an actual UUID later
37             }
38              
39             sub start_recording {
40 0     0 0   my $self = shift;
41              
42 0           $self->{start_time_hires} = [Time::HiRes::gettimeofday()];
43 0           $self->{start_time} = DateTime->now();
44             }
45              
46       0 0   sub pause_recording {
47              
48             }
49              
50             sub finish_recording {
51 0     0 0   my $self = shift;
52              
53 0           $self->{finish_time_hires} = [Time::HiRes::gettimeofday()];
54 0           $self->{finish_time} = DateTime->now();
55             }
56              
57             sub has_finished {
58 0     0 0   my $self = shift;
59              
60 0 0         return 1 if defined $self->{finish_time_hires};
61 0           return 0;
62             }
63              
64             sub save {
65 0     0 0   my $self = shift;
66              
67 0           $self->agent->output->save($self);
68             }
69              
70             sub total_elapsed_time {
71 0     0 0   my $self = shift;
72              
73 0           return Time::HiRes::tv_interval( $self->{start_time_hires}, $self->{finish_time_hires} );
74             }
75              
76             sub total_elapsed_time_in_ms {
77 0     0 0   my $self = shift;
78              
79 0           $self->convert_to_ms($self->total_elapsed_time);
80             }
81              
82             sub database_transaction_count {
83 0     0 0   my $self = shift;
84              
85 0           scalar(@{$self->database_transactions});
  0            
86             }
87              
88             sub webservice_transaction_count {
89 0     0 0   my $self = shift;
90              
91 0           scalar(@{$self->webservice_transactions});
  0            
92             }
93              
94             sub database_elapsed_time {
95 0     0 0   my $self = shift;
96              
97 0           my $total = 0;
98 0           foreach my $database_transaction(@{$self->database_transactions}) {
  0            
99              
100             #warn $database_transaction->{start_time};
101              
102             #warn "start " . join(",", @{$database_transaction->{start_time}});
103             #warn "finish " . join(",", @{$database_transaction->{finish_time}});
104             #warn "interval " . Time::HiRes::tv_interval( $database_transaction->{start_time}, $database_transaction->{finish_time});
105              
106 0           $total += Time::HiRes::tv_interval( $database_transaction->{start_time}, $database_transaction->{finish_time});
107             }
108              
109 0           return $total;
110             }
111              
112             sub webservice_elapsed_time {
113 0     0 0   my $self = shift;
114              
115 0           my $total = 0;
116 0           foreach my $webservice_transaction(@{$self->webservice_transactions}) {
  0            
117 0           $total += Time::HiRes::tv_interval( $webservice_transaction->{start_time}, $webservice_transaction->{finish_time});
118             }
119              
120 0           return $total;
121             }
122              
123             sub database_elapsed_time_in_ms {
124 0     0 0   my $self = shift;
125              
126 0           $self->convert_to_ms($self->database_elapsed_time)
127             }
128              
129             sub webservice_elapsed_time_in_ms {
130 0     0 0   my $self = shift;
131              
132 0           $self->convert_to_ms($self->webservice_elapsed_time)
133             }
134              
135             sub add_database_transaction {
136 0     0 0   my $self = shift;
137 0           my $database_transaction = shift;
138              
139 0           push(@{$self->database_transactions}, $database_transaction);
  0            
140             }
141              
142             sub add_webservice_transaction {
143 0     0 0   my $self = shift;
144 0           my $webservice_transaction = shift;
145              
146 0           push(@{$self->webservice_transactions}, $webservice_transaction);
  0            
147             }
148              
149             sub calculate_time_index_in_ms {
150 0     0 0   my $self = shift;
151 0           my $other_time = shift;
152              
153 0           return( $self->convert_to_ms( Time::HiRes::tv_interval($self->{start_time_hires}, $other_time )));
154             }
155              
156             sub convert_to_ms {
157 0     0 0   my $self = shift;
158 0           my $thing_to_convert = shift;
159              
160 0           return sprintf("%.0f", $thing_to_convert * 1000)
161             }
162              
163             sub do_not_save {
164 0     0 0   my $self = shift;
165              
166 0           $self->should_save(0);
167             }
168              
169             # Putting the application ID in here would really help the server later on but we aren't requiring the user to specify it yet
170             sub generate_new_cross_application_tracing_id {
171 0     0 0   my $self = shift;
172              
173 0           return $self->{uuid} . '@' . join(',', (Time::HiRes::gettimeofday()));
174             }
175              
176             sub DESTROY {
177 0     0     my $self = shift;
178              
179 0           $self->agent->output->flush();
180 0           $self->agent->remove_profile($self->uuid);
181             }
182              
183              
184              
185             1;