由于种种原因没有用NET::DNS包,而且动能比较简单,只是为了证明1s时间内可以处理上千条消息。
#simple query from ENUM server
#parameters: ENUM server, from_dn, end_dn
my $nameserver = 'xxx.xxx.xxx.xxx';
my $prefix = '1534401';
my $from = '0000';
my $end = '3999';
print "Benchmark configure:\n";
print "\tENUM server: $nameserver\n";
print "\tStart DN : $prefix$from\n";
print "\tEnd DN : $prefix$end\n";
print "\nIs the above data is correct for Benckmarking? (y/n) : ";
my $ans = <>;
if ($ans =~ /NO|no|N|n/) {
print "\n\nPlease change the line 6 ~ 9 for your configuration.\n\n";
exit 255;
}
#$| = 1;
$/ = '\012\015';
use strict;
use IO::Socket::INET;
$SIG{ALRM} = sub { die "no message recieved" };
print "\nSeting up E164 data - ";
my @e164dn_list;
for my $last ($from..$end){
my $dn = "$prefix$last";
push @e164dn_list, to_e164($dn);
}
print "done.";
print "\nSeting up E164 body for ENUM query - ";
my @messages;
foreach my $i (@e164dn_list) {
my @ele = split /\./, $i;
my $message = "";
foreach my $e (@ele) {
$message .= pack("C a*", length($e), $e);
}
$message .= pack("C", 0);
push @messages, $message;
}
print "done.";
print "\nGenerating ENUM Query data - ";
my $id = 1;
my $qr = 0;
my $opcode = 0;
my $aa = 0;
my $tc = 0;
my $rd = 1;
my $ra = 0;
my $ad = 0;
my $cd = 0;
my $rcode = 0;
my $byte1 = $qr?0x80:0 | $opcode << 3 | $aa?0x04:0 | $tc?0x02:0 | $rd?0x01:0 ;
my $byte2 = $ra?0x80:0 | $ad?0x20:0 | $cd?0x10:0 | $rcode;
my $ques = 1;
my $answ = 0;
my $auth = 0;
my $addt = 0;
my $type = 35;
my $class = 1;
my $tail = pack("n2", $type, $class);
my $data;
print "done.";
print "\nSetting up the socket - ";
my $enum_client = IO::Socket::INET->new(
PeerAddr => '135.252.30.251',
PeerPort => '53',
Proto => 'udp',
);
binmode $enum_client;
print "done.";
use Benchmark;
open FH, ">", "test.dat";
print "\nStarting BenchMark - \n";
my $tt1 = new Benchmark;
foreach my $msgs (@messages) {
#Construct the headr with unique ID
my $header = pack("n C2 n4", $id, $byte1, $byte2, $ques, $answ, $auth, $addt);
$enum_client->send($header.$msgs.$tail, 0);
#ALRM if the response could not be received in 1s
alarm(1); # Alarm after 1 second
my $peer_addr = $enum_client->recv($data, 1024);
alarm(0); # Cancel the Alarm
print "."; #Show the progress
print "\n" if $id % 80 == 0;
#Write to file for post analysis
print FH "$data\n\n";
$id++;
}
my $tt2 = new Benchmark;
close(FH);
print "\n- Done of BenchMark\n\n";
#BenchMark counting
my $td = Benchmark::timediff($tt2, $tt1);
$td = Benchmark::timestr($td);
print "-" x 58; print "\n"; print "Benchmark result\n$td\n\n";
$td =~ /(\d+)\s*wallclock secs \(\s*?(\d*?\.\d*?)\s*usr\s*\+\s*(\d*?\.\d*?)\s*sys/i;
my $alltimes=($2+$3)*1000;
# Generate report
print "-" x 25, " REPORT ", "-" x 25;
print "\nENUM query send : ", $id-1;
print "\nENUM answer recieved : ", $id-1;
print "\nTotal time : $alltimes ms\n";
print "-" x 58; print "\n";
######## Support Func #########
sub to_e164{
my $dn = shift;
my $e164_num = join(".", reverse(split(//, $dn)));
return "${e164_num}.e164.arpa";
}