NS 2.35 柯志亨书-实验5笔记-置信区间

时间:2022-12-28 23:35:13

图同实验4,一条tcp,一条背景tcp,一条on-off干扰流

背景tcp流不同的rate,实验n次,求吞吐量,求平均

笔记图:

NS 2.35 柯志亨书-实验5笔记-置信区间

Tcl代码:

# Kezhiheng,experiment 5,test confidence,1 tcp flow,2 bkgrd traffic
# Usage lab5.tcl on-off_rate ex_seq
# ex: ns lab5.tcl 100 1 (rate为100,第1次实验)
if {$argc!=2} {
puts "Usage: ns lab5.tcl rate_ no_"
exit
}

set par1 [lindex $argv 0]
set par2 [lindex $argv 1]

#Create a simulator object
set ns [new Simulator]

set tracefd [open data/zout-$par1-$par2.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 router nodes
set r1 [$ns node]
set r2 [$ns node]
$ns duplex-link $r1 $r2 1Mb 10ms DropTail
#$ns queue-limit $r1 $r2 10

# Set TCP src, dest, link
for {set i 1} {$i<=3} {incr i} {
set s($i) [$ns node]
set d($i) [$ns node]

$ns duplex-link $s($i) $r1 10Mb 1ms DropTail
$ns duplex-link $r2 $d($i) 10Mb 1ms DropTail
}

# Set TCP agent and FTP traffic
for {set i 1} {$i<=2} {incr i} {
set tcp($i) [new Agent/TCP]
set sink($i) [new Agent/TCPSink]
$ns attach-agent $s($i) $tcp($i)
$ns attach-agent $d($i) $sink($i)
$ns connect $tcp($i) $sink($i)

set ftp($i) [new Application/FTP]
$ftp($i) attach-agent $tcp($i)
$ftp($i) set type_ FTP
}

# Setup a on-off noisy traffic, exponetial distr
set i 3;
set udp($i) [new Agent/UDP]
set null($i) [new Agent/Null]
$ns attach-agent $s($i) $udp($i)
$ns attach-agent $d($i) $null($i)
$ns connect $udp($i) $null($i)

set traffic [new Application/Traffic/Exponential]
$traffic set packetSize_ 1000
$traffic set burst_time_ 0.5
$traffic set idle_time_ 0
$traffic set rate_ [expr $par1*1000]
$traffic attach-agent $udp(3)

# Let rng diff
set rng [new RNG]
$rng seed 0

set rvStart [new RandomVariable/Uniform]
$rvStart use-rng $rng
$rvStart set min_ 3
$rvStart set max_ 4

# 随机决定第一条flow的开始时刻3~4s
set startT [expr [$rvStart value]]
#puts "startT $startT sec"

# 先让干扰的数据流消耗网络资源
$ns at 0.0 "$ftp(2) start"
$ns at 0.0 "$traffic start"
$ns at $startT "$ftp(1) start"

$ns at 11.0 "$ftp(1) stop"
$ns at 11.5 "$ftp(2) stop"
$ns at 11.5 "$traffic stop"

$ns at 12.0 "finish"

$ns run

View Code

提取throughput的awk代码:

# Measure the throughput by the trace file

BEGIN{
# program initialize
init = 0;
startT=0;
endT=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 s1->d1 recv byte cnt, time 5s~10s
if(action=="r" && type=="tcp" && time>=5.0 && time<=10.0 && \
( (from==1 && to==3) ) )
{
if(init==0)
{
startT = time;
init = 1;
}
pkt_byte_sum += pktsize;
endT=time;
}

}

END {
# When read over, start to calculate
#printf("startT:%f,endT:%f\n",startT,endT);
#printf("pkt_byte_sum:%d\n",pkt_byte_sum);
time = endT-startT;
throughput=pkt_byte_sum * 8 / time / 1000;
printf("%f\n", throughput);
}

View Code

求不同实验平均值的awk代码:

BEGIN{
cnt=0;
sum=0;
}
{
cnt++
sum=sum+$1
}
END{
printf("%d %f\n", rate, sum/cnt);
}

View Code

ns+提取吞吐量数据+求平均的 perl代码:

#!/usr/bin/perl

#1. on-off add step is 100kbps
#2. in every step, run 30 times, record to resultxxx
#3. rerun, remember to delete resultxxx
#3. awk > 每次新建文件 >> 每次追加内容

my $ratemax=500;
my $expertime=10;

# 执行ns and awk提取数据
for ($i=100; $i<=$ratemax; $i=$i+100)
{
print " i=$i\n";
for ($j=1; $j<=$expertime; $j++)
{
system("ns lab5.tcl $i $j");
$f1="data/zout-$i-$j.tr";
$f2="data/zresult-$i";
system("awk -f lab5_th.awk $f1 >> $f2");
print "j=$j\n";
}
}


# awk计算平均值
print "Start to calc average...\n";
for ($i=100; $i<=$ratemax; $i=$i+100)
{
print " i=$i\n";
$f1="data/zresult-$i";
$f2="data/zresult-ave";
system("awk -v rate=$i -f avg.awk $f1 >> $f2");
}

View Code

求置信区间的awk代码:

BEGIN{
ln=0
s2=0
}
{

ln++
d=$1-t
s2=s2+d*d

}
END{
s=sqrt(s2/(ln-1))
# print "sample variance:"s"\nConf.Int.95%:"t"+/-"1.96*s/sqrt(ln)
printf("%f\n", 1.96*s/sqrt(ln))
}

View Code

求置信区间的shell:

#!/bin/bash

awk -v t=107.095215 -f confint95.awk data/zresult-500

最后的数据结果:

100 459.092 2.01183
200 409.815 2.59063
300 321.663 24.3013
400 190.156 3.37999
500 104.476 2.65695

绘图的shell:

#!/bin/bash

gnuplot -persist<<EOF

set terminal gif
set output "throughput.gif"
set title "throughput"
set xlabel "on-off flow rate/kbps"
set ylabel "s1-d1 throughput/kbps"
set xrange [0:600]
set xtics 0,100,600
unset key

plot "data/zresult-ave" using 1:2:3 with errorbars, "data/zresult-ave" with linespoints

EOF

View Code

图形:

NS 2.35 柯志亨书-实验5笔记-置信区间

本来想在perl中读取平均值,输入给求置信区间的awk,后来perl中字符串和浮点数之间可能有问题,一直报错,当时的代码:

#!/usr/bin/perl

$a=457.802985;
system("awk -v t=$a -f confint95.awk data/zresult-100");

my $a=1.0;
# awk计算方差
print "Start to calc variance...\n";
# 读如平均值文件
open(IN, "data/zresult-ave");
for ($i=100; $i<=100; $i=$i+100)
{
print " i=$i\n";

$line=<IN>;
@ele=split(/ /, $line);
$a=$ele[1];
print "$a";
# 如果没有上面这句话,会出错,-f not found,应该是$ele[1]是字符串,而t要求是数值,产生错误
# 没找到字符串转浮点数的方法。咳,弱类型语言很迷茫啊!~~~还是好好学习shell吧!!!
# $a=457.802985;
print "$a";
$f1="data/zresult-$i";
$f2="data/zresult-var";
system("awk -v t=$a -f confint95.awk data/zresult-100");
}
close(IN);

View Code