Skip to content

Instantly share code, notes, and snippets.

@beppu
Created January 6, 2010 15:52
Show Gist options
  • Select an option

  • Save beppu/270350 to your computer and use it in GitHub Desktop.

Select an option

Save beppu/270350 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use common::sense;
use EV;
use AnyEvent;
use AnyEvent::DNS;
use Coro;
use Coro::AnyEvent;
use Data::Dump 'pp';
use Getopt::Long;
my $usage = qq{A tool for benchmarking nameservers
Usage:
nb <CONCURRENCY> <COUNT> <HOSTNAME>...
Example:
Using 100 coroutines, see how long it takes to lookup the ip_addr
for slashdot.org 10000 times:
/usr/bin/time nb 100 10000 slashdot.org
};
if (!@ARGV) {
print $usage;
exit 1
}
my $c = shift || 1;
my $n = shift || 1;
my @hosts = @ARGV;
if (!@hosts) {
print $usage;
exit 2;
}
my $count_per_coro = int($n / $c);
my $remainder = $n % $c;
my $done = 0;
sub lookup {
my $count = shift;
my $i;
for ($i = 0; $i < $count; $i++) {
my $cv = AE::cv;
AnyEvent::DNS::a $hosts[int(rand(scalar(@hosts)))], $cv;
my @addrs = $cv->recv;
$done++;
#warn "$done @addrs";
exit if $done == $n;
}
}
#warn pp \@hosts;
warn "concurrency: $c, count, $n, count_per_coro: $count_per_coro, remainder: $remainder";
if ($remainder) {
for ( 1 .. ($c - $remainder) ) {
async {
lookup $count_per_coro;
};
}
for ( 1 .. $remainder ) {
async {
lookup $count_per_coro + 1;
}
}
} else {
for ( 1 .. $c ) {
async {
lookup $count_per_coro;
};
}
}
AE::cv->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment