关注点:
有线场景中,Trace文件的格式;(此处不细说,因为很多Tutorial中很详尽!哈哈!)
利用awk工具计算端到端延迟、抖动率、丢包率、吞吐量;
利用gnuplot的基本命令,并将awk工具的分析结果用曲线图绘制出来!
首先,下面的tcl会产生out.tr
# 场景描述:
# 主要用于观察数据包在节点Agent上的排队和丢包情况
# 有线场景,四个节点,FTT+TCP用蓝色表示, CBR+UDP用红色表示
set ns [new Simulator]
#设置颜色
$ns color 1 Blue
$ns color 2 Red
set tracefd [open out.tr w]
$ns trace-all $tracefd
set namtracefd [open out.nam w]
$ns namtrace-all $namtracefd
proc finish {} {
global ns tracefd namtracefd
$ns flush-trace
close $tracefd
close $namtracefd
exec nam out.nam &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 10ms DropTail
$ns queue-limit $n2 $n3 10
#用于设置NAM显示时,各节点的相对位置
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n2 $n3 queuePos 0.5
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $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.5 "$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 $n0 $tcp ; $ns detach-agent $n3 $sink"
$ns at 5.0 "finish"
#会在命令窗口中输出, 因为默认的stdout是命令窗口啊!
puts "CBR packet size=[$cbr set packet_size_]"
puts "CBR interval=[$cbr set interval_]"
$ns run
# 主要用于观察数据包在节点Agent上的排队和丢包情况
# 有线场景,四个节点,FTT+TCP用蓝色表示, CBR+UDP用红色表示
set ns [new Simulator]
#设置颜色
$ns color 1 Blue
$ns color 2 Red
set tracefd [open out.tr w]
$ns trace-all $tracefd
set namtracefd [open out.nam w]
$ns namtrace-all $namtracefd
proc finish {} {
global ns tracefd namtracefd
$ns flush-trace
close $tracefd
close $namtracefd
exec nam out.nam &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 10ms DropTail
$ns queue-limit $n2 $n3 10
#用于设置NAM显示时,各节点的相对位置
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n2 $n3 queuePos 0.5
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $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.5 "$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 $n0 $tcp ; $ns detach-agent $n3 $sink"
$ns at 5.0 "finish"
#会在命令窗口中输出, 因为默认的stdout是命令窗口啊!
puts "CBR packet size=[$cbr set packet_size_]"
puts "CBR interval=[$cbr set interval_]"
$ns run
之后,再写awk:
端点到端点的延迟: measure-delay.awk文件
#测量CBR数据包端点到端点间延迟时间的awk代码
#针对out.tr的分析
BEGIN {
#初始化, 设置变量以记录目前已处理数据包的最大ID号
highest_packet_id=0;
}
{
#将out.tr文件的相应字段赋值给变量
action=$1;
time=$2;
from=$3;
to=$4;
type=$5;
pktsize=$6;
flow_id=$8;
src=$9;
dst=$10;
seq_no=$11;
packet_id=$12;
#记录目前已处理数据包的最大ID号
if (packet_id>highest_packet_id)
highest_packet_id=packet_id;
#记录数据包的发送的时间
if (start_time[packet_id]==0)
start_time[packet_id]=time;
#记录 CBR (其flow_id=2, 与具体情况有关哦!) 的接收时间
if (flow_id==2 && action!="d") {
if (action=="r") {
end_time[packet_id]=time;
}
}else {
#把不是CBR数据包或者被DROP掉的CBR数据包的接收时间设置为 -1;
end_time[packet_id]=-1;
}
}
END {
# 当out.tr中数据行全部读取完后,开始计算有效数据包的端到端的时间延迟
for ( packet_id=0; packet_id <= highest_packet_id; packet_id++) {
start=start_time[packet_id];
end=end_time[packet_id];
packet_duration=end-start;
#显然,只把接收时间晚于发送时间的记录列出来哦!
if (start<end) printf("%f %f/n", start, packet_duration);
}
}
#针对out.tr的分析
BEGIN {
#初始化, 设置变量以记录目前已处理数据包的最大ID号
highest_packet_id=0;
}
{
#将out.tr文件的相应字段赋值给变量
action=$1;
time=$2;
from=$3;
to=$4;
type=$5;
pktsize=$6;
flow_id=$8;
src=$9;
dst=$10;
seq_no=$11;
packet_id=$12;
#记录目前已处理数据包的最大ID号
if (packet_id>highest_packet_id)
highest_packet_id=packet_id;
#记录数据包的发送的时间
if (start_time[packet_id]==0)
start_time[packet_id]=time;
#记录 CBR (其flow_id=2, 与具体情况有关哦!) 的接收时间
if (flow_id==2 && action!="d") {
if (action=="r") {
end_time[packet_id]=time;
}
}else {
#把不是CBR数据包或者被DROP掉的CBR数据包的接收时间设置为 -1;
end_time[packet_id]=-1;
}
}
END {
# 当out.tr中数据行全部读取完后,开始计算有效数据包的端到端的时间延迟
for ( packet_id=0; packet_id <= highest_packet_id; packet_id++) {
start=start_time[packet_id];
end=end_time[packet_id];
packet_duration=end-start;
#显然,只把接收时间晚于发送时间的记录列出来哦!
if (start<end) printf("%f %f/n", start, packet_duration);
}
}
运行一下命令: awk或gawk
$gawk -f measure-delay.awk out.tr //直接在命令窗口显示
或
$gawk -f measure-delay.awk out.tr > cbr_delay // 将结果存储到文件中
gnuplot> plot "cbr_delay"
在图示结果上做标识:
#运行以下命令:
gnuplot> set title "cbr_delay"
gnuplot> set xlabel "simulation time"
gnuplot> set ylabel "delay time"
gnuplot> unset key
gnuplot> set label "constant delay=0.038706 sec"at 0.1,0.05
gnuplot> set arrow from 0.5,0.05 to 0.5,0.04
gnuplot> plot "cbr_delay"
#进一步改进!
gnuplot> plot "cbr_delay" with linespoints