计算CBR抖动的awk代码:
# Measure the end to end delay jitter by the trace file
# 计算方法:抖动率由相邻数据包延迟时间差除以数据包序号差得到;
# jitter = ( (trecvj-tsndj) - (trecvi-tsndi) ) / (j-i), j>i
BEGIN{
# program initialize
highest_packet_id = 0;
}
{
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;
# Record the current max packet ID
if ( packet_id > highest_packet_id )
highest_packet_id = packet_id;
# Record the tx time of packet
if ( start_time[packet_id] == 0 )
{
pkt_seqno[packet_id] = seq_no;
start_time[packet_id] = time;
}
# Record CBR flow_id=2 rx time
if ( flow_id == 2 && action != "d" )
{
if (action == "r")
{
end_time[packet_id] = time;
}
}
else
end_time[packet_id] = -1;
}
END {
# When read over, start to calculate
last_seqno = 0;
last_delay = 0;
seqno_diff = 0;
for ( packet_id=0; packet_id<=highest_packet_id; packet_id++ )
{
start = start_time[packet_id];
end = end_time[packet_id];
duration = start-end;
if (start<end)
{
# calc jitter
seqno_diff = pkt_seqno[packet_id]-last_seqno;
delay_diff = duration - last_delay;
if(seqno_diff == 0)
jitter = 0;
else
jitter = delay_diff/seqno_diff;
printf("%f %f\n", start, jitter);
last_seqno = pkt_seqno[packet_id];
last_delay = duration;
}
}
}
计算吞吐率的代码:
# Measure the CBR average throughput by the trace file
# throughput = byte cnt / time interval
# time interval = cur_time - start_time
BEGIN{
# program initialize
init = 0;
i = 0;
}
{
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;
if(action=="r" && from==2 && to==3 && flow_id==2)
{
pkt_byte_sum[i+1]=pkt_byte_sum[i] + pktsize;
if(init==0)
{
start_time=time;
init=1;
}
end_time[i]=time;
i=i+1;
}
}
END {
# When read over, start to calculate
printf("%.2f\t%.2f\n", end_time[0], 0);
for(j=1;j<i;j++)
{
# 从开始到当前的平均吞吐率,而不是现在的瞬时吞吐率
# 这样计算似乎有失准确
th=pkt_byte_sum[j] / (end_time[j]-start_time) *8 / 1000;
printf("%.2f\t%.2f\n", end_time[j], th);
}
# 书中说,为了画图好看,一开始和最后都设为0,最后的0似乎有点不妥
#printf("%.2f\t%.2f\n", end_time[0], 0);
}
计算丢包率:
# Measure the CBR packet loss rate by the trace file
BEGIN{
# program initialize
fsDrops = 0;
numFs = 0;
}
{
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;
# Record how many packets sent from n1
if ( from==1 && to==2 && action=="+" )
numFs++;
# Record flow_id is 2 and dropped
if ( flow_id==2 && action=="d" )
fsDrops++;
}
END {
# When read over, start to calculate
printf("number of pkts sent:%d, lost:%d, loss rate:%f\n", numFs,fsDrops,fsDrops/numFs);
}
number of pkts sent:550, lost:8, loss rate:0.014545