计算吞吐率
下面的perl脚本可以计算ns2的跟踪文件out.tr中某个链路的吞吐量(Throughput)。
使用方法
ns2脚本
#number of connections
set n_con 1
set ns [new Simulator]
# Trace all packets event
set tf [open out.tr w]
$ns trace-all $tf
#Define a 'finish' procedure
proc finish {output_ } {
upvar $output_ output
global ns n_con
for {set i 0} {$i< $n_con } {incr i} {
close $output($i)
}
$ns flush-trace
exit 0
}
proc record { tcp_ output_ } {
upvar $tcp_ tcp
upvar $output_ output
global ns n_con
#set the time after which the procedure should be called again.
set time 0.02
#get the current time
set now [$ns now]
for {set i 0} {$i < $n_con } {incr i} {
puts $output($i) "$now [$tcp($i) set cwnd_]"
}
$ns at [expr $now+$time ] "record tcp output"
}
# Create four node
set r0 [$ns node]
set r1 [$ns node]
#Create links between the nodes
$ns duplex-link $r0 $r1 2Mb 1ms RED
#set the queue size of link(r0-r1) to 20
$ns queue-limit $r0 $r1 20
#trace queue to file queue.tr
set qf [open queue.tr w]
$ns trace-queue $r0 $r1 $qf
for {set i 0} {$i < $n_con} {incr i} {
set s($i) [$ns node]
$ns duplex-link $s($i) $r0 10Mb 0.4ms DropTail
set output($i) [open cwd$i.tr w]
}
set sink [new Agent/TCPSink]
$ns attach-agent $r1 $sink
for {set i 0} {$i < $n_con} {incr i} {
set tcp($i) [new Agent/TCP]
$ns attach-agent $s($i) $tcp($i)
$ns connect $tcp($i) $sink
$tcp($i) set fid_ $i
$tcp($i) set window_ 128
$tcp($i) set packetSize 512
#Setup a FTP over TCP Connection
set ftp($i) [new Application/FTP]
$ftp($i) attach-agent $tcp($i)
$ftp($i) set type_ FTP
}
# Schedule events for the FTP agents
$ns at 0.0 "record tcp output"
#$ns at 0.0 "$ftp(0) start"
for {set i 0} {$i < $n_con} {incr i} {
$ns at 0.0 "$ftp($i) start"
}
$ns at 30.0 "finish output"
#Run the simulation
$ns run
perl脚本
用来计算输出out.tr文件对应的吞吐量的脚本
# file name throughput.pl
$infile = $ARGV[0];
$tonode = $ARGV[1];
$granularity = $ARGV[2];
# we compute how many bytes were transmmitted during time interval specified by granularity parameter in seconds
$sum = 0;
$clock = 0;
open (DATA,"<$infile")
|| die "Can't open $infile $!";
while (<DATA>) {
@x = split(' ');
#column 1 is time
if ( $x[1] - $clock <= $granularity )
{
#checking if the event corresponds to a reception
if( $x[0] eq 'r'){
# checking if the destination corresponds to 1st argument
if( $x[3] eq $tonode ) {
#checking if the packet type is TCP
if ( $x[4] eq 'tcp') {
$sum = $sum + $x[5];
}
}
}
}
else
{
$throughput = $sum / $granularity ;
print STDOUT "$x[1] $throughput\n";
$clock = $clock + $granularity;
$sum = 0;
}
}
$throughput = $sum / $granularity;
print STDOUT "$x[1] $throughput\n";
$clock = $clock + $granularity;
$sum = 0;
close DATA;
exit(0);
调用脚本生成thp文件,并绘制图形
使用如下命令调用throughput.pl方法从out.tr中的0号和1号节点链路的吞吐量,存放到thp中。
perl throughput.pl out.tr 0 1 > thp
gnuplot
plot 'thp' w lines 1