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

filter: Statementstmt 25.6% 28 / 109 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 42.1% 8 / 19 Podpod 100% 6 / 6 totaltotal 24.7% 42 / 170 CC 15 SCAR 28.5
CC
15
Cov
20%
_get_redis
42.0
ran_rule
42.0
new
6.0
CRAP 17.3
↑ 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.004'; # 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_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  
29 sub new {
30 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
31 0 my $mailsaobject = shift;
32  
33 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
34 0 my $self = $class->SUPER::new($mailsaobject);
35 0 bless ($self, $class);
36  
37 $mailsaobject->{conf}->{parser}->register_commands( [
38 {
39 0 setting => 'timing_redis_server',
40 is_admin => 1,
41 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
42 default => '127.0.0.1:6379',
43 }, {
44 setting => 'timing_redis_password',
45 is_admin => 1,
46 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
47 }, {
48 setting => 'timing_redis_exclude_re',
49 is_admin => 1,
50 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
51 default => '^__',
52 }, {
53 setting => 'timing_redis_prefix',
54 is_admin => 1,
55 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
56 default => 'sa-timing.',
57 }, {
58 setting => 'timing_redis_database',
59 is_admin => 1,
60 type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
61 default => 0,
62 }, {
63 setting => 'timing_redis_precision',
64 is_admin => 1,
65 type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
66 default => 1000000, # microseconds (millionths of a second)
67 }, {
68 setting => 'timing_redis_bulk_update',
69 is_admin => 1,
70 type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
71 default => 50,
72 }, {
73 setting => 'timing_redis_debug',
74 is_admin => 1,
75 type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL,
76 default => 0,
77 },
78 ] );
79  
80 0 return( $self );
81 }
82  
83 sub _get_redis {
84 0 ▶ my $self = shift;
Subroutine _get_redis: not called CC=6 SCAR=37.4
85 0 my $conf = $self->{main}->{conf};
86 my ( $server, $debug, $password, $database, $bulk ) =
87 0 @$conf{ 'timing_redis_server','timing_redis_debug', 'timing_redis_password',
88 'timing_redis_database', 'timing_redis_bulk_update' };
89  
90 0 untaint_var( \$server );
91  
92 0 ▶ if( ! defined $self->{'_redis'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_redis"}) 0 0 0
93 0 Mail::SpamAssassin::Plugin::info('initializing connection to redis server...');
94 0 eval {
95 0 ▶ $self->{'_redis'} = Redis->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $password ? : 0 0 0
96 'server' => $server,
97 'debug' => $debug,
98 defined $password ? ( password => $password ) : (),
99 );
100 };
101 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
102 0 die('could not connect to redis: '.$@);
103 }
104 0 ▶ if( $database ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($database) 0 0 0
105 0 Mail::SpamAssassin::Plugin::info("selecting redis database $database...");
106 0 $self->{'_redis'}->select($database);
107 }
108 0 ▶ if( $bulk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bulk) 0 0 0
109 0 Mail::SpamAssassin::Plugin::info("loading redis lua bulk script...");
110 0 $self->{'_script'} = $self->{'_redis'}->script_load($BULK_SCRIPT);
111 0 Mail::SpamAssassin::Plugin::dbg("script loaded as ".$self->{'_script'});
112 }
113 }
114 0 return $self->{'_redis'};
115 }
116  
117 sub _flush_queue {
118 0 ▶ my ( $self, $queue ) = @_;
Subroutine _flush_queue: not called CC=2 SCAR=17.9
119 0 my $prefix = $self->{main}->{conf}->{'timing_redis_prefix'};
120  
121 0 my $count = scalar @$queue;
122 0 ▶ if( ! $count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($count) 0 0 0
123 0 return;
124 }
125 0 Mail::SpamAssassin::Plugin::dbg("flushing $count timing events to redis...");
126 0 my @args;
127 0 push( @args, map { $prefix.$_->[0] } @$queue );
128 0 push( @args, map { $_->[1] } @$queue );
129  
130 $self->{'_redis'}->evalsha(
131 0 ▶ $self->{'_script'}, $count, @args, sub {});
Subroutine __ANON__: not called CC=1 SCAR=6.9
132  
133 0 @$queue = ();
134  
135 0 return;
136 }
137  
138 sub check_start {
139 0 ▶ my ($self, $options) = @_;
Subroutine check_start: not called CC=1 SCAR=6.9
Pod documented
140 0 $options->{permsgstatus}->{'rule_timing_queue'} = [];
141 0 return;
142 }
143  
144 sub start_rules {
145 0 ▶ my ($self, $options) = @_;
Subroutine start_rules: not called CC=1 SCAR=6.9
Pod documented
146 0 $options->{permsgstatus}->{'rule_timing_start'} = Time::HiRes::time();
147 0 return;
148 }
149  
150 sub ran_rule {
151 0 ▶ my $time = Time::HiRes::time();
Subroutine ran_rule: not called CC=6 SCAR=37.4
Pod documented
152 0 my ($self, $options) = @_;
153 0 my $exclude_re = $self->{main}->{conf}->{'timing_redis_exclude_re'};
154 0 my $bulk = $self->{main}->{conf}->{'timing_redis_bulk_update'};
155 0 my $queue = $options->{permsgstatus}->{'rule_timing_queue'};
156  
157 0 my $permsg = $options->{permsgstatus};
158 0 my $name = $options->{rulename};
159 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
160 && $exclude_re ne ''
161 && $name =~ /$exclude_re/ ) {
162 0 $permsg->{'rule_timing_start'} = Time::HiRes::time();
163 0 return;
164 }
165 0 my $prefix = $self->{main}->{conf}->{'timing_redis_prefix'};
166 0 my $precision = $self->{main}->{conf}->{'timing_redis_precision'};
167  
168 0 my $duration = int(($time - $permsg->{'rule_timing_start'}) * $precision);
169  
170 0 my $redis = $self->_get_redis;
171  
172 0 ▶ if( $bulk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bulk) { } 0 0 0
173 0 push( @$queue, [ $name, $duration ] );
174  
175 0 ▶ if( scalar @$queue >= $bulk ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$queue >= $bulk) 0 0 0
176 0 $self->_flush_queue( $queue );
177 }
178 } else {
179 0 ▶ $redis->incrby($prefix.$name.'.time', $duration, sub {} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 0 ▶ $redis->incr($prefix.$name.'.count', sub {} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 }
182  
183 0 $permsg->{'rule_timing_start'} = Time::HiRes::time();
184 0 return;
185 }
186  
187 sub check_end {
188 0 ▶ my ($self, $options) = @_;
Subroutine check_end: not called CC=2 SCAR=17.9
Pod documented
189 0 my $bulk = $self->{main}->{conf}->{'timing_redis_bulk_update'};
190 0 my $queue = $options->{permsgstatus}->{'rule_timing_queue'};
191  
192 0 my $redis = $self->_get_redis;
193 0 ▶ if( $bulk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bulk) 0 0 0
194 0 Mail::SpamAssassin::Plugin::dbg("cleaning up redis timing queue (".scalar(@$queue)." left)...");
195 0 $self->_flush_queue( $queue );
196 }
197 0 Mail::SpamAssassin::Plugin::dbg("waiting for redis pipelined responses...");
198 0 $redis->wait_all_responses;
199  
200 0 return;
201 }
202  
203 sub finish {
204 0 ▶ my $self = shift;
Subroutine finish: not called CC=2 SCAR=17.9
Pod documented
205 0 ▶ if( defined $self->{'redis'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"redis"}) 0 0 0
206 0 $self->{'redis'}->quit;
207 }
208 0 return;
209 }
210  
211 1 1;
212  
213 __END__
214  
215 =pod
216  
217 =encoding UTF-8
218  
219 =head1 NAME
220  
221 Mail::SpamAssassin::Plugin::RuleTimingRedis - collect SA rule timings in redis
222  
223 =head1 VERSION
224  
225 version 1.004
226  
227 =head1 DESCRIPTION
228  
229 RuleTimingRedis is a plugin for spamassassin which gathers and stores performance
230 data of processed spamassassin rules in a redis server.
231  
232 =head1 CONFIGURATION
233  
234 To load the plugin put an loadplugin line into init.pre:
235  
236 loadplugin Mail::SpamAssassin::Plugin::RuleTimingRedis
237  
238 If the RuleTimingRedis.pm is not in perls @INC you need to specify the path:
239  
240 loadplugin Mail::SpamAssassin::Plugin::RuleTimingRedis <path-to>/RuleTimingRedis.pm
241  
242 If your redis server is not listening on 127.0.0.1:6379 configure the address in local.cf:
243  
244 timing_redis_server 192.168.0.10:6379
245  
246 Then restart amavisd.
247  
248 After the first mail was processed the keys for the processed rules should appear in redis:
249  
250 $ redis-cli
251 redis 127.0.0.1:6379> KEYS 'sa-timing.*'
252 1) "sa-timing.__DRUGS_SLEEP3.count"
253 2) "sa-timing.__MAIL_LINK.count"
254 3) "sa-timing.__CGATE_RCVD.count"
255 ...
256  
257 =head1 PARAMETERS
258  
259 The plugin has the following configuration options:
260  
261 =over
262  
263 =item timing_redis_server (default: '127.0.0.1:6379')
264  
265 Address and port of the redis server.
266  
267 =item timing_redis_password (default: no password)
268  
269 Set if you redis server requires a password.
270  
271 =item timing_redis_exclude_re (default: '^__')
272  
273 Regex to exclude rules from timing statistics.
274  
275 The current SpamAssassin ruleset is about ~2k rules.
276 The default will exclude all sub-rules that start with '__' (two underscores).
277  
278 Set to empty string if you really want to measure all rules.
279  
280 =item timing_redis_prefix (default: 'sa-timing.')
281  
282 Prefix to used for the keys in redis.
283  
284 =item timing_redis_database (default: 0)
285  
286 Will call SELECT to switch database after connect if set to a non-zero value.
287  
288 Database 0 is the default in redis.
289  
290 =item timing_redis_precision (default: 1000000)
291  
292 Since redis uses integers the floating point value is multiplied
293 by this factor before storing in redis.
294  
295 =item timing_redis_bulk_update (default: 50)
296  
297 Will queue redis updates up to the configured value and execute them
298 in bulks via a server-side script.
299  
300 Requires a redis server >= 2.6.0 and Redis perl module >= 1.954.
301  
302 Set to 0 to disable bulk.
303  
304 =item timing_redis_debug (default: 0)
305  
306 Turn on/off debug on the Redis connection.
307  
308 =back
309  
310 =head1 AUTHOR
311  
312 Markus Benning <ich@markusbenning.de>
313  
314 =head1 COPYRIGHT AND LICENSE
315  
316 This software is Copyright (c) 2015 by Markus Benning.
317  
318 This is free software, licensed under:
319  
320 The Apache License, Version 2.0, January 2004
321  
322 =cut
↑ Summary