mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-20 02:49:49 +01:00
LagChecker: convert to ms and be more concise
This commit is contained in:
parent
202ae50c36
commit
cdb7ac1901
@ -14,7 +14,6 @@ use warnings;
|
||||
use strict;
|
||||
|
||||
use feature 'unicode_strings';
|
||||
|
||||
use feature 'switch';
|
||||
|
||||
use Time::HiRes qw(gettimeofday tv_interval);
|
||||
@ -22,12 +21,8 @@ use Time::Duration;
|
||||
use Carp ();
|
||||
|
||||
sub new {
|
||||
if (ref($_[1]) eq 'HASH') {
|
||||
Carp::croak("Options to LagChecker should be key/value pairs, not hash reference");
|
||||
}
|
||||
|
||||
Carp::croak("Options to LagChecker should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH';
|
||||
my ($class, %conf) = @_;
|
||||
|
||||
my $self = bless {}, $class;
|
||||
$self->initialize(%conf);
|
||||
return $self;
|
||||
@ -36,8 +31,7 @@ sub new {
|
||||
sub initialize {
|
||||
my ($self, %conf) = @_;
|
||||
|
||||
my $pbot = delete $conf{pbot} // Carp::croak("Missing pbot reference to LagChecker");
|
||||
$self->{pbot} = $pbot;
|
||||
$self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to LagChecker");
|
||||
|
||||
$self->{lag_average} = undef; # average of entries in lag history, in seconds
|
||||
$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
|
||||
|
||||
# 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
|
||||
$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
|
||||
$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 },
|
||||
$pbot->{registry}->get_value('lagchecker', 'lag_history_interval'),
|
||||
$self->{pbot}->{registry}->get_value('lagchecker', '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(@_) });
|
||||
}
|
||||
|
||||
@ -72,9 +65,7 @@ sub lag_history_interval_trigger {
|
||||
|
||||
sub send_ping {
|
||||
my $self = shift;
|
||||
|
||||
return unless defined $self->{pbot}->{conn};
|
||||
|
||||
$self->{ping_send_time} = [gettimeofday];
|
||||
$self->{pong_received} = 0;
|
||||
$self->{pbot}->{conn}->sl("PING :lagcheck");
|
||||
@ -86,14 +77,14 @@ sub on_pong {
|
||||
$self->{pong_received} = 1;
|
||||
|
||||
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');
|
||||
|
||||
while ($len > $lag_history_max) {
|
||||
shift @{ $self->{lag_history} };
|
||||
shift @{$self->{lag_history}};
|
||||
$len--;
|
||||
}
|
||||
|
||||
@ -101,17 +92,17 @@ sub on_pong {
|
||||
my $comma = "";
|
||||
|
||||
my $lag_total = 0;
|
||||
foreach my $entry (@{ $self->{lag_history} }) {
|
||||
my ($send_time, $lag_result) = @{ $entry };
|
||||
foreach my $entry (@{$self->{lag_history}}) {
|
||||
my ($send_time, $lag_result) = @$entry;
|
||||
|
||||
$lag_total += $lag_result;
|
||||
my $ago = ago(gettimeofday - $send_time);
|
||||
$self->{lag_string} .= $comma . "[$ago] $lag_result";
|
||||
my $ago = concise ago(gettimeofday - $send_time);
|
||||
$self->{lag_string} .= $comma . "[$ago] " . sprintf "%.1f ms", $lag_result;
|
||||
$comma = "; ";
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
@ -130,7 +121,6 @@ sub lagging {
|
||||
|
||||
sub lagstring {
|
||||
my $self = shift;
|
||||
|
||||
my $lag = $self->{lag_string} || "initializing";
|
||||
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
|
||||
my $elapsed = tv_interval($self->{ping_send_time});
|
||||
my $lag_total = $elapsed;
|
||||
my $len = @{ $self->{lag_history} };
|
||||
my $len = @{$self->{lag_history}};
|
||||
|
||||
my $lagstring = "";
|
||||
my $comma = "";
|
||||
|
||||
foreach my $entry (@{ $self->{lag_history} }) {
|
||||
my ($send_time, $lag_result) = @{ $entry };
|
||||
|
||||
foreach my $entry (@{$self->{lag_history}}) {
|
||||
my ($send_time, $lag_result) = @$entry;
|
||||
$lag_total += $lag_result;
|
||||
my $ago = ago(gettimeofday - $send_time);
|
||||
$lagstring .= $comma . "[$ago] $lag_result";
|
||||
my $ago = concise ago(gettimeofday - $send_time);
|
||||
$lagstring .= $comma . "[$ago] " . sprintf "%.1f ms", $lag_result;
|
||||
$comma = "; ";
|
||||
}
|
||||
|
||||
$lagstring .= $comma . "[waiting for pong] $elapsed";
|
||||
|
||||
my $average = $lag_total / ($len + 1);
|
||||
$lagstring .= "; average: $average}";
|
||||
$lagstring .= "; average: " . sprintf "%.1f ms", $average;
|
||||
return $lagstring;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user