blib/lib/Mail/SpamAssassin/Plugin/RuleTimingRedis.pm

filter: Statementstmt 23.9% 29 / 121 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 38.1% 8 / 21 Podpod 100% 6 / 6 totaltotal 22.2% 43 / 193 CC 19 SCAR 30.4
CC
19
Cov
18%
_get_redis
42.0
ran_rule
42.0
check_end
42.0
CRAP 20.9
↑ Summary
1 package Mail::SpamAssassin::Plugin::RuleTimingRedis;
2  
3 1 ▶use Mail::SpamAssassin::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mail::SpamAssassin::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mail::SpamAssassin::Util qw( untaint_var );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: collect SA rule timings in redis
11 1 our $VERSION = '1.005'; # VERSION
12  
13  
14 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 @ISA = qw(Mail::SpamAssassin::Plugin);
18  
19 1 ▶use Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our $BULK_TIMING_SCRIPT = <<"EOT";
22 for i=1,#KEYS do
23 redis.call('INCR', KEYS[i] .. ".count" )
24 redis.call('INCRBY', KEYS[i] .. ".time", ARGV[i] )
25 end
26 return #KEYS
27 EOT
28 1 our $BULK_MINCR_SCRIPT = <<"EOT";
29 for i=1,#KEYS do
30 redis.call('INCR', KEYS[i] )
31 end
32 return #KEYS
33 EOT
34  
35 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
36 0 my $class = shift;
37 0 my $mailsaobject = shift;
38  
39 0 ▶ $class = ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
40 0 my $self = $class->SUPER::new($mailsaobject);
41 0 bless ($self, $class);
42  
43 $mailsaobject->{conf}->{parser}->register_commands( [
44 {
45 0 setting => 'timing_redis_server',
46 is_admin => 1,
47 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
48 default => '127.0.0.1:6379',
49 }, {
50 setting => 'timing_redis_password',
51 is_admin => 1,
52 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
53 }, {
54 setting => 'timing_redis_exclude_re',
55 is_admin => 1,
56 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
57 default => '^__',
58 }, {
59 setting => 'timing_redis_prefix',
60 is_admin => 1,
61 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
62 default => 'sa-timing.',
63 }, {
64 setting => 'timing_redis_database',
65 is_admin => 1,
66 type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
67 default => 0,
68 }, {
69 setting => 'timing_redis_precision',
70 is_admin => 1,
71 type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
72 default => 1000000, # microseconds (millionths of a second)
73 }, {
74 setting => 'timing_redis_bulk_update',
75 is_admin => 1,
76 type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
77 default => 50,
78 }, {
79 setting => 'timing_redis_debug',
80 is_admin => 1,
81 type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL,
82 default => 0,
83 }, {
84 setting => 'timing_redis_hits_enabled',
85 is_admin => 1,
86 type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL,
87 default => 0,
88 },
89 ] );
90  
91 0 return( $self );
92 }
93  
94 0 ▶sub _get_redis {
Subroutine _get_redis: not called CC=6 SCAR=37.4
95 0 my $self = shift;
96 0 my $conf = $self->{main}->{conf};
97 my ( $server, $debug, $password, $database, $bulk ) =
98 0 @$conf{ 'timing_redis_server','timing_redis_debug', 'timing_redis_password',
99 'timing_redis_database', 'timing_redis_bulk_update' };
100  
101 0 untaint_var( \$server );
102  
103 0 ▶ if( ! defined $self->{'_redis'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_redis"}) 0 0 0
104 0 Mail::SpamAssassin::Plugin::info('initializing connection to redis server...');
105 0 eval {
106 0 ▶ $self->{'_redis'} = Redis->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $password ? : 0 0 0
107 'server' => $server,
108 'debug' => $debug,
109 defined $password ? ( password => $password ) : (),
110 );
111 };
112 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
113 0 die('could not connect to redis: '.$@);
114 }
115 0 ▶ if( $database ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($database) 0 0 0
116 0 Mail::SpamAssassin::Plugin::info("selecting redis database $database...");
117 0 $self->{'_redis'}->select($database);
118 }
119 0 ▶ if( $bulk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bulk) 0 0 0
120 0 Mail::SpamAssassin::Plugin::info("loading redis lua bulk script...");
121 0 $self->{'_timing_script'} = $self->{'_redis'}->script_load($BULK_TIMING_SCRIPT);
122 0 $self->{'_mincr_script'} = $self->{'_redis'}->script_load($BULK_MINCR_SCRIPT);
123 Mail::SpamAssassin::Plugin::dbg("scripts loaded as ".$self->{'_timing_script'}
124 0 .' and '.$self->{'_mincr_script'});
125 }
126 }
127 0 return $self->{'_redis'};
128 }
129  
130 0 ▶sub _flush_queue {
Subroutine _flush_queue: not called CC=2 SCAR=17.9
131 0 my ( $self, $queue ) = @_;
132 0 my $prefix = $self->{main}->{conf}->{'timing_redis_prefix'};
133  
134 0 my $count = scalar @$queue;
135 0 ▶ if( ! $count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($count) 0 0 0
136 0 return;
137 }
138 0 Mail::SpamAssassin::Plugin::dbg("flushing $count timing events to redis...");
139 0 my @args;
140 0 push( @args, map { $prefix.$_->[0] } @$queue );
141 0 push( @args, map { $_->[1] } @$queue );
142  
143 $self->{'_redis'}->evalsha(
144 0 ▶ $self->{'_timing_script'}, $count, @args, sub {});
Subroutine __ANON__: not called CC=1 SCAR=6.9
145  
146 0 @$queue = ();
147  
148 0 return;
149 }
150  
151 0 ▶sub check_start {
Subroutine check_start: not called CC=1 SCAR=6.9
Pod documented
152 0 my ($self, $options) = @_;
153 0 $options->{permsgstatus}->{'rule_timing_queue'} = [];
154 0 return;
155 }
156  
157 0 ▶sub start_rules {
Subroutine start_rules: not called CC=1 SCAR=6.9
Pod documented
158 0 my ($self, $options) = @_;
159 0 $options->{permsgstatus}->{'rule_timing_start'} = Time::HiRes::time();
160 0 return;
161 }
162  
163 0 ▶sub ran_rule {
Subroutine ran_rule: not called CC=6 SCAR=37.4
Pod documented
164 0 my $time = Time::HiRes::time();
165 0 my ($self, $options) = @_;
166 0 my $exclude_re = $self->{main}->{conf}->{'timing_redis_exclude_re'};
167 0 my $bulk = $self->{main}->{conf}->{'timing_redis_bulk_update'};
168 0 my $queue = $options->{permsgstatus}->{'rule_timing_queue'};
169  
170 0 my $permsg = $options->{permsgstatus};
171 0 my $name = $options->{rulename};
172 0 ▶ if( defined $exclude_re
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exclude_re and $exclude_re ne "" and $name =~ /$exclude_re/) 0 0 0
Condition0%0/6
defined $exclude_re and $exclude_re ne "" and $name =~ /$exclude_re/
!ll&&!rl&&r
000
defined $exclude_re and $exclude_re ne ""
!ll&&!rl&&r
000
MC/DC0%0/3
defined $exclude_re and $exclude_re ne "" and $name =~ /$exclude_re/
defined $exclude_re $exclude_re ne "" $name =~ /$exclude_re/
Truth table0%0/4
A and B and C
A= defined $exclude_reB= $exclude_re ne ""C= $name =~ /$exclude_re/
ABCresult
0XX0
10X0
1100
1111
173 && $exclude_re ne ''
174 && $name =~ /$exclude_re/ ) {
175 0 $permsg->{'rule_timing_start'} = Time::HiRes::time();
176 0 return;
177 }
178 0 my $prefix = $self->{main}->{conf}->{'timing_redis_prefix'};
179 0 my $precision = $self->{main}->{conf}->{'timing_redis_precision'};
180  
181 0 my $duration = int(($time - $permsg->{'rule_timing_start'}) * $precision);
182  
183 0 my $redis = $self->_get_redis;
184  
185 0 ▶ if( $bulk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bulk) { } 0 0 0
186 0 push( @$queue, [ $name, $duration ] );
187  
188 0 ▶ if( scalar @$queue >= $bulk ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$queue >= $bulk) 0 0 0
189 0 $self->_flush_queue( $queue );
190 }
191 } else {
192 0 ▶ $redis->incrby($prefix.$name.'.time', $duration, sub {} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
193 0 ▶ $redis->incr($prefix.$name.'.count', sub {} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 }
195  
196 0 $permsg->{'rule_timing_start'} = Time::HiRes::time();
197 0 return;
198 }
199  
200 0 ▶sub check_end {
Subroutine check_end: not called CC=6 SCAR=37.4
Pod documented
201 0 my ($self, $options) = @_;
202 0 my $prefix = $self->{main}->{conf}->{'timing_redis_prefix'};
203 0 my $bulk = $self->{main}->{conf}->{'timing_redis_bulk_update'};
204 0 my $hits_enabled = $self->{main}->{conf}->{'timing_redis_hits_enabled'};
205 0 my $pms = $options->{permsgstatus};
206 0 my $queue = $pms->{'rule_timing_queue'};
207  
208 0 my $redis = $self->_get_redis;
209 0 ▶ if( $bulk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bulk) 0 0 0
210 0 Mail::SpamAssassin::Plugin::dbg("cleaning up redis timing queue (".scalar(@$queue)." left)...");
211 0 $self->_flush_queue( $queue );
212 }
213 0 ▶ if( $hits_enabled && $bulk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hits_enabled and $bulk) 0 0 0
Condition0%0/3
$hits_enabled and $bulk
!ll&&!rl&&r
000
MC/DC0%0/2
$hits_enabled and $bulk
$hits_enabled $bulk
Truth table0%0/3
A and B
A= $hits_enabledB= $bulk
ABresult
0X0
100
111
214 0 my @tests = map { $prefix.$_.'.hits' }
215 split(',', $pms->get_names_of_tests_hit);
216 0 ▶ if( $bulk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bulk) { } 0 0 0
217 $self->{'_redis'}->evalsha(
218 0 ▶ $self->{'_mincr_script'}, scalar(@tests), @tests, sub {});
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 } else {
220 0 foreach my $test ( @tests ) {
221 0 ▶ $self->{'_redis'}->incr($test, sub {} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 }
223 }
224 }
225  
226 0 Mail::SpamAssassin::Plugin::dbg("waiting for redis pipelined responses...");
227 0 $redis->wait_all_responses;
228  
229 0 return;
230 }
231  
232 0 ▶sub finish {
Subroutine finish: not called CC=2 SCAR=17.9
Pod documented
233 0 my $self = shift;
234 0 ▶ if( defined $self->{'redis'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"redis"}) 0 0 0
235 0 $self->{'redis'}->quit;
236 }
237 0 return;
238 }
239  
240 1 1;
241  
242 __END__
↑ Summary