E-mail:
科研中对具体场景的模拟之后,往往要做很多的数据分析,我们可以直接对模拟后的Trace文件进行分析;也可以自己在仿真代码中编写采样代码,此时需要利用NS2为我们提供的接口,与Unix/Linux系统中为我们提供系统运行实时的镜像文件的机制很类似哦!
为此我选择对入门实例3的场景作为演示,场景比较简单,因此代码看起来会比较亲切、熟悉!
关注点:
采样过程的编写!
Xgraph、Nam在Finish proc中调用的简单命令!
实例2:
#============================================ # 对NS2入门实例3的深入研究 # 通过采样测量应用CBR和FTP的吞吐量、丢包率、时延 #============================================ # 代码注释请参考NS2入门实例3 set ns [new Simulator] $ns color 1 Blue $ns color 2 Red set nf [open out.nam w] $ns namtrace-all $nf set nd [open out.tr w] $ns trace-all $nd #用于记录采样结果的文件 set f0 [open cbr-throughput.tr w] set f1 [open cbr-loss.tr w] set f2 [open ftp-throughput.tr w]
set last_ack 0 #采样过程的代码编写 proc record {} { global ns null tcp f0 f1 f2 last_ack set time 0.5 ;#Set Sampling Time to 0.5 Sec set a [$null set bytes_] ;# CBR/UDP--Through set b [$null set nlost_] ;# CBR/UDP--Loss set c [$tcp set ack_] ;# FTP/TCP--Through set d [$tcp set packetSize_] set now [$ns now] # Record Bit Rate in Trace Files, CBR--Throughput puts $f0 "$now [expr $a*8/$time]" # Record Packet Loss Rate in File,CBR--loss puts $f1 "$now [expr $b/$time]" if { $c >0} { set e [expr $c - $last_ack] puts $f2 "$now [expr $e*$d*8/$time]" set last_ack $c ;#注意哦:更新last_ack, 以便用于进行下一时刻的采样计算 } else { puts $f2 "$now 0" } # Reset Variables $null set bytes_ 0 $null set nlost_ 0 $ns at [expr $now+$time] "record" ;# Schedule Record after $time interval sec }
proc finish {} { global ns nf nd f0 f1 f2 $ns flush-trace close $nf close $nd close $f0 close $f1 close $f2 # Plot Recorded Statistics exec xgraph cbr-throughput.tr ftp-throughput.tr -geometry 800x400 & exec xgraph cbr-loss.tr -geometry 800x400 & exec nam out.nam & exit 0 }
set s1 [$ns node] set s2 [$ns node] set r [$ns node] set d [$ns node] $ns duplex-link $s1 $r 2Mb 10ms DropTail $ns duplex-link $s2 $r 2Mb 10ms DropTail $ns duplex-link $r $d 1.7Mb 20ms DropTail $ns queue-limit $r $d 10 $ns duplex-link-op $s1 $r orient right-down $ns duplex-link-op $s2 $r orient right-up $ns duplex-link-op $r $d orient right $ns duplex-link-op $r $d queuePos 0.5
#设置FTP/TCP的部分 set tcp [new Agent/TCP] $ns attach-agent $s1 $tcp set sink [new Agent/TCPSink] $ns attach-agent $d $sink $ns connect $tcp $sink $tcp set fid_ 1
set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP
#设置CBR/UDP的部分 set udp [new Agent/UDP] $ns attach-agent $s2 $udp set null [new Agent/LossMonitor] $ns attach-agent $d $null $ns connect $udp $null $udp set fid_ 2
set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false
#模拟时间的设定 $ns at 0.0 "record" $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 4.0 "$ftp stop" $ns at 4.5 "$cbr stop" #此行代码可有可无,暂时不管啊! $ns at 4.5 "$ns detach-agent $s1 $tcp ; $ns detach-agent $d $sink"
$ns at 5.0 "finish" $ns run
|
仿真之后生成了一下文件:
out.tr out.nam cbr-loss.tr cbr-throughput.tr ftp-throughput.tr
有必要观察以下三个文件:
cbr-loss.tr 文件:(我稍作排版啦,哈哈!)
0 0.0 0.5 0.0 1 0.0 1.5 8.0 2 2.0 2.5 2.0 3 0.0 3.5 2.0 4 0.0 4.5 2.0 |
cbr-throughput.tr 文件:
0 0.0 0.5 736000.0 1 992000.0 1.5 848000.0 2 992000.0 2.5 1040000.0 3 960000.0 3.5 1040000.0 4 928000.0 4.5 1072000.0 |
ftp-throughput.tr 文件:
0 0 0.5 0 1 0 1.5 400000.0 2 496000.0 2.5 480000.0 3 720000.0 3.5 528000.0 4 720000.0 4.5 176000.0 |
运行结果: (拓扑结构图在NS2入门实例3中已给出!)
CBR的丢包率情况:
CBR和FTP的吞吐量情况:
E-mail:
阅读(5488) | 评论(3) | 转发(1) |