计算链路的吞吐量

计算吞吐率

下面的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

绘制的图形如下

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值