3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-01-23 10:34:52 +01:00

LagChecker: convert to ms and be more concise

This commit is contained in:
Pragmatic Software 2020-01-03 21:18:51 -08:00
parent 202ae50c36
commit cdb7ac1901

View File

@ -14,7 +14,6 @@ use warnings;
use strict; use strict;
use feature 'unicode_strings'; use feature 'unicode_strings';
use feature 'switch'; use feature 'switch';
use Time::HiRes qw(gettimeofday tv_interval); use Time::HiRes qw(gettimeofday tv_interval);
@ -22,12 +21,8 @@ use Time::Duration;
use Carp (); use Carp ();
sub new { sub new {
if (ref($_[1]) eq 'HASH') { Carp::croak("Options to LagChecker should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH';
Carp::croak("Options to LagChecker should be key/value pairs, not hash reference");
}
my ($class, %conf) = @_; my ($class, %conf) = @_;
my $self = bless {}, $class; my $self = bless {}, $class;
$self->initialize(%conf); $self->initialize(%conf);
return $self; return $self;
@ -36,8 +31,7 @@ sub new {
sub initialize { sub initialize {
my ($self, %conf) = @_; my ($self, %conf) = @_;
my $pbot = delete $conf{pbot} // Carp::croak("Missing pbot reference to LagChecker"); $self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to LagChecker");
$self->{pbot} = $pbot;
$self->{lag_average} = undef; # average of entries in lag history, in seconds $self->{lag_average} = undef; # average of entries in lag history, in seconds
$self->{lag_string} = undef; # string representation of lag history and lag average $self->{lag_string} = undef; # string representation of lag history and lag average
@ -46,22 +40,21 @@ sub initialize {
$self->{ping_send_time} = undef; # when last ping was sent $self->{ping_send_time} = undef; # when last ping was sent
# maximum number of lag history entries to retain # maximum number of lag history entries to retain
$pbot->{registry}->add_default('text', 'lagchecker', 'lag_history_max', $conf{lag_history_max} // 3); $self->{pbot}->{registry}->add_default('text', 'lagchecker', 'lag_history_max', $conf{lag_history_max} // 3);
# lagging is true if lag_average reaches or exceeds this threshold, in seconds # lagging is true if lag_average reaches or exceeds this threshold, in seconds
$pbot->{registry}->add_default('text', 'lagchecker', 'lag_threshold', $conf{lag_threadhold} // 2); $self->{pbot}->{registry}->add_default('text', 'lagchecker', 'lag_threshold', $conf{lag_threadhold} // 2);
# how often to send PING, in seconds # how often to send PING, in seconds
$pbot->{registry}->add_default('text', 'lagchecker', 'lag_history_interval', $conf{lag_history_interval} // 10); $self->{pbot}->{registry}->add_default('text', 'lagchecker', 'lag_history_interval', $conf{lag_history_interval} // 10);
$pbot->{registry}->add_trigger('lagchecker', 'lag_history_interval', sub { $self->lag_history_interval_trigger(@_) }); $self->{pbot}->{registry}->add_trigger('lagchecker', 'lag_history_interval', sub { $self->lag_history_interval_trigger(@_) });
$pbot->{timer}->register( $self->{pbot}->{timer}->register(
sub { $self->send_ping }, sub { $self->send_ping },
$pbot->{registry}->get_value('lagchecker', 'lag_history_interval'), $self->{pbot}->{registry}->get_value('lagchecker', 'lag_history_interval'),
'lag_history_interval' 'lag_history_interval'
); );
$pbot->{commands}->register(sub { return $self->lagcheck(@_) }, "lagcheck", 0); $self->{pbot}->{commands}->register(sub { return $self->lagcheck(@_) }, "lagcheck", 0);
$self->{pbot}->{event_dispatcher}->register_handler('irc.pong', sub { $self->on_pong(@_) }); $self->{pbot}->{event_dispatcher}->register_handler('irc.pong', sub { $self->on_pong(@_) });
} }
@ -72,9 +65,7 @@ sub lag_history_interval_trigger {
sub send_ping { sub send_ping {
my $self = shift; my $self = shift;
return unless defined $self->{pbot}->{conn}; return unless defined $self->{pbot}->{conn};
$self->{ping_send_time} = [gettimeofday]; $self->{ping_send_time} = [gettimeofday];
$self->{pong_received} = 0; $self->{pong_received} = 0;
$self->{pbot}->{conn}->sl("PING :lagcheck"); $self->{pbot}->{conn}->sl("PING :lagcheck");
@ -86,14 +77,14 @@ sub on_pong {
$self->{pong_received} = 1; $self->{pong_received} = 1;
my $elapsed = tv_interval($self->{ping_send_time}); my $elapsed = tv_interval($self->{ping_send_time});
push @{ $self->{lag_history} }, [ $self->{ping_send_time}[0], $elapsed ]; push @{$self->{lag_history}}, [ $self->{ping_send_time}[0], $elapsed * 1000];
my $len = @{ $self->{lag_history} }; my $len = @{$self->{lag_history}};
my $lag_history_max = $self->{pbot}->{registry}->get_value('lagchecker', 'lag_history_max'); my $lag_history_max = $self->{pbot}->{registry}->get_value('lagchecker', 'lag_history_max');
while ($len > $lag_history_max) { while ($len > $lag_history_max) {
shift @{ $self->{lag_history} }; shift @{$self->{lag_history}};
$len--; $len--;
} }
@ -101,17 +92,17 @@ sub on_pong {
my $comma = ""; my $comma = "";
my $lag_total = 0; my $lag_total = 0;
foreach my $entry (@{ $self->{lag_history} }) { foreach my $entry (@{$self->{lag_history}}) {
my ($send_time, $lag_result) = @{ $entry }; my ($send_time, $lag_result) = @$entry;
$lag_total += $lag_result; $lag_total += $lag_result;
my $ago = ago(gettimeofday - $send_time); my $ago = concise ago(gettimeofday - $send_time);
$self->{lag_string} .= $comma . "[$ago] $lag_result"; $self->{lag_string} .= $comma . "[$ago] " . sprintf "%.1f ms", $lag_result;
$comma = "; "; $comma = "; ";
} }
$self->{lag_average} = $lag_total / $len; $self->{lag_average} = $lag_total / $len;
$self->{lag_string} .= "; average: $self->{lag_average}"; $self->{lag_string} .= "; average: " . sprintf "%.1f ms", $self->{lag_average};
return 0; return 0;
} }
@ -130,7 +121,6 @@ sub lagging {
sub lagstring { sub lagstring {
my $self = shift; my $self = shift;
my $lag = $self->{lag_string} || "initializing"; my $lag = $self->{lag_string} || "initializing";
return $lag; return $lag;
} }
@ -142,24 +132,23 @@ sub lagcheck {
# a ping has been sent (pong_received is not undef) and no pong has been received yet # a ping has been sent (pong_received is not undef) and no pong has been received yet
my $elapsed = tv_interval($self->{ping_send_time}); my $elapsed = tv_interval($self->{ping_send_time});
my $lag_total = $elapsed; my $lag_total = $elapsed;
my $len = @{ $self->{lag_history} }; my $len = @{$self->{lag_history}};
my $lagstring = ""; my $lagstring = "";
my $comma = ""; my $comma = "";
foreach my $entry (@{ $self->{lag_history} }) { foreach my $entry (@{$self->{lag_history}}) {
my ($send_time, $lag_result) = @{ $entry }; my ($send_time, $lag_result) = @$entry;
$lag_total += $lag_result; $lag_total += $lag_result;
my $ago = ago(gettimeofday - $send_time); my $ago = concise ago(gettimeofday - $send_time);
$lagstring .= $comma . "[$ago] $lag_result"; $lagstring .= $comma . "[$ago] " . sprintf "%.1f ms", $lag_result;
$comma = "; "; $comma = "; ";
} }
$lagstring .= $comma . "[waiting for pong] $elapsed"; $lagstring .= $comma . "[waiting for pong] $elapsed";
my $average = $lag_total / ($len + 1); my $average = $lag_total / ($len + 1);
$lagstring .= "; average: $average}"; $lagstring .= "; average: " . sprintf "%.1f ms", $average;
return $lagstring; return $lagstring;
} }