Files
perl-anyevent-nsq/examples/consumer.pl
Pedro Melo d807d361f3 Add --stats support for consumer:
Gives stats over the last 10 seconds, plus global rate.

Use with http_publisher.pl to test your consumer performance.

Signed-off-by: Pedro Melo <melo@simplicidade.org>
2014-07-21 05:47:33 +01:00

88 lines
2.4 KiB
Perl
Executable File

#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use AnyEvent;
use AnyEvent::NSQ::Reader;
use Getopt::Long;
use Time::HiRes qw( gettimeofday tv_interval );
my ($topic, $channel, $help, $verbose, $print, $stats);
GetOptions('help' => \$help, 'verbose' => \$verbose, 'print' => \$print, 'stats' => \$stats) or usage();
usage() if $help;
($topic, $channel) = @ARGV;
usage("topic and channel are required parameters") unless $topic and $channel;
my $cv = AE::cv;
## return undef => mark_as_done_msg()
my $message_cb = $print ? sub { print "$_[1]{message}\n"; return } : sub {return};
my $t = my $p = 0;
my $r = AnyEvent::NSQ::Reader->new(
topic => $topic,
channel => $channel,
nsqd_tcp_addresses => '127.0.0.1',
client_id => "${channel}_consumer/pid_$$",
message_cb => sub { $t++; $p++; $message_cb->(@_) },
error_cb => sub { warn "$_[1]\n" if $verbose },
disconnect_cb => sub { warn "Disconnected after $t total messages... exiting...\n" if $verbose; $cv->send },
);
if ($stats) {
warn "Stats enabled, printed every 10 seconds\n";
my $t0 = my $p0 = [gettimeofday];
$stats = AE::timer 10, 10, sub {
my $now = [gettimeofday];
my $elapsed_t = tv_interval($t0, $now);
my $elapsed_p = tv_interval($p0, $now);
my $uptime = '';
my ($h, $m);
if ($h = int($elapsed_t / 3600)) { $uptime .= "${h}h" }
if ($m = int(($elapsed_t - $h * 3600) / 60)) { $uptime .= "${m}m" }
$uptime .= int($elapsed_t - $h * 3600 - $m * 60) . 's';
warn sprintf(
'Stats: %0.3f mesgs/sec for the past %0.3f seconds, total messages %d, global rate %0.3f, uptime %s%s',
$p / $elapsed_p,
$elapsed_p, $t, $t / $elapsed_t, $uptime,"\n"
);
$p = 0;
$p0 = $now;
};
}
my $term_sgn = AE::signal TERM => sub { $r->disconnect; undef $stats };
my $int_sgn = AE::signal INT => sub { $r->disconnect; undef $stats };
$cv->recv;
sub usage {
print "Error: @_\n" if @_;
print <<" EOU";
Usage: consumer.pl [--help|-h] [--print|-p] topic channel
Consume messages from channel <channel> linked to topic <topic>.
Topic and channel parameters are mandatory.
Options:
--help or -h Prints this message and exits
--verbose or -v Writes debug information about connections,
disconnections and errors
--print or -p Prints each received message
EOU
exit(1);
}