awk 是一种程序语言,对于资料的处理具有很强的功能,可以使用很短的代码轻易地完成对文本档案做修改、分析、提取和比较等处理。 相对比,如果用C或Pascal等语言编写程序完成上述功能,需要花费较多的时间编写很长的代码。
安装gawk:
sudo apt-get install gawk
在ns3中生成tr格式文件,需要在simulator::run前面加如下语句:
AsciiTraceHelper ascii;
csma.EnableAsciiAll (ascii.CreateFileStream ("文件名.tr"));
下面是计算时延的完整gwak代码:
BEGIN {
highest_packet_id=0;
FS="[() \t]";#field seperator is ')' or ‘(’or ' '
myScrIP = "10.1.1.1";#This is the link that we pay attention to
myDstIP = "10.1.7.1"
}
{
action = $1;
time = $2;
namespace=$3;
for (i=1;i<=NF;i++)#find packet ID
{
if ($i ~ /id/) #if $i field matches "id"
myPacketID = $(i+1);#record the id of the packet for future use
else if ($i ~ />/) #if $i field matches ">"
{
srcIP = $(i-1);
dstIP = $(i+1);
if(match(srcIP, myScrIP) && match(dstIP, myDstIP) )#link matches
{
#printf("%s:%s %s > %s %s\n",$1,$2,srcIP,dstIP, myPacketID);
packet_id = myPacketID;
#start to record the information of the packet
if (packet_id>highest_packet_id)
{
highest_packet_id=packet_id;
#printf("*****highest_packet_id: %s\n",highest_packet_id);
}
#record send time of the packet
if (start_time[packet_id]==0)
{
start_time[packet_id]=time;
#printf("*****packetID: %s,start_time: %s\n",packet_id,time);
}
if (action=="r")
{
end_time[packet_id]=time;
#printf("*****packetID: %s,end_time: %s\n",packet_id,time);
}
}#if
}#else if
}#for
}
END {
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);
}
}
代码写入一个文本命名delay.awk
执行 gawk -f delay.awk 文件名.tr > myDelay //保存结果到myDelay文件
然后将处理时延结果生成png图片执行如下命令即可生成时延图片:
gnuplot //进入gnuplot
set terminal png size 640,480 //设置输出图片格式宽度,高度
set xlabel "x轴名称"
set ylable "y轴名称"
set output "文件名.png" //设置保存图片名称
plot "myDelay" using 1:2 title "delay" with linespoints pointtype 2
吞吐量代码如下,使用方法和时延脚本类似。
BEGIN {
init=0;
cnt = 0;
FS="[() \t]";#field seperator is ')' or'('or ' '
myScrIP = "10.1.1.1";#This is the link that we pay attention to
myDstIP = "10.1.7.1"
}
{
action = $1;
time = $2;
namespace=$3;
for (i=1;i<=NF;i++)#find packet ID
if ($i ~ /id/) #if $i field matches "id"
myPacketID = $(i+1);#record the id of the packet for future use
else if ($i ~ /length:/) #if $i field matches "length:"
myLength = $(i+1);#record the length of the packet for future use
else if ($i ~ />/) #if $i field matches ">"
{
srcIP = $(i-1);
dstIP = $(i+1);
if(match(srcIP, myScrIP) && match(dstIP, myDstIP) )#link matches
{
packet_id = myPacketID;
pktsize = myLength;
#record send time of the packet
if (start_time[packet_id]==0)
{
start_time[packet_id]=time;
}
if (action=="r")
{
if(end_time_packet[packet_id] ==0 )#the first time we receive this packet
{
end_time_packet[packet_id] = time;#record time according to id
packetCNT[packet_id]=cnt;
pkt_byte_sum[cnt+1]=pkt_byte_sum[cnt]+ pktsize;
end_time[cnt]=time;
cnt++;
}#if(end_time_packet[packet_id] ==0 ) else#not the 1st time we receive this packet,so we update receive time
{
#printf("*****duplicate packetID: %s,cnt=%s,end_time_old=%s,end_time new: %s\n",packet_id,cnt,end_time[packetCNT[packet_id]], time);
end_time[packetCNT[packet_id]]=time;
}
#if (action=="r")
}#if match(srcIP, myScrIP)
}#else if ($i ~ />/) #if $i field matches ">"
}#for (i=1;i<=NF;i++)#find packet ID
}
END {
printf("%s\t%s\n", end_time[0], 0);
for(j=1 ; j