NS 2.35 柯志亨书实验4笔记随机数产生参数化批处理

Tcl代码:

# Kezhiheng, experiment 4, test RNG, 3 tcp flow

# Usage: ns xx.tcl flowno seed
if {$argc!=3} {
    puts "Usage: ns xx.tcl flowno_ seed_ queue_"
    puts "queue_ DropTail or RED"
    exit
}

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

#Create a simulator object
set ns [new Simulator]

set tracefd [open resultdata/zout-$par3-$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
}

# 3 TCP flow
set nflow $par1

# Set router nodes
set r1 [$ns node]
set r2 [$ns node]
$ns duplex-link $r1 $r2 1Mb 10ms $par3
$ns queue-limit $r1 $r2 10

# Set TCP src, dest, link
for {set i 1} {$i<=$nflow} {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<=$nflow} {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
}

set rng [new RNG]
$rng seed $par2

set rvStart [new RandomVariable/Uniform]
$rvStart use-rng $rng
$rvStart set min_ 0.0
$rvStart set max_ 1.0

for {set i 1} {$i<=$nflow} {incr i} {
    set startT($i) [expr [$rvStart value]]
#    puts "startT($i) $startT($i) sec"
    set endT($i) [expr ($startT($i)+5)]
#    puts "endT($i) $endT($i) sec"
    
    $ns at $startT($i) "$ftp($i) start"
    $ns at $endT($i)   "$ftp($i) stop"
}

$ns at 7.0 "finish"

$ns run
View Code

从tr提取吞吐量数据的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 pkttype=tcp, action=r, time 1s~5s
    if(action=="r" && type=="tcp" && time>=1.0 && time<=5.0 && \
        ( (from==1 && to==3)||(from==1 && to==5)||(from==1 && to==7) ) )
    {
        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 / 1000000;
#printf("throughput:%.3f Mbps\n", throughput);
printf("%.3f\n", throughput);
}
View Code

求平均的awk代码:

# Measure the throughput by the trace file

BEGIN{
    # program initialize
    sum = 0;
    cnt = 0;
}

{

    thrpt = $1;

    cnt = cnt + 1;
    sum = sum + thrpt;
}

END {
    # When read over, start to calculate
    ave = sum/cnt;
    printf("%d %.3f\n", flowno, ave);
}
View Code

综合的perl代码:

#!/usr/bin/perl

#1. flowno, 1,3,5,7,9
#2. seed, 1,2,3,4,5
#3. zresult-flowno-seed, 5*5=25 files

# flow max number, should be odd
my $flowmax=15;
my $queue="DropTail";
#my $queue=RED;

for ($i=1; $i<=$flowmax; $i=$i+2)
{
    print "    i=$i\n";
    for ($j=1; $j<=5; $j++)
    {
        system("ns rng_tcp.tcl $i $j $queue");
        $f1="resultdata/zout-$queue-$i-$j.tr";
        $f2="resultdata/zresult-$queue-$i";
        system("awk -f rng_tcp.awk $f1 >> $f2");
        print "j=$j\n";
    }
}


#1. flowno, 1,3,5,7,9
#2. calc average
print "Start to calc average...\n";
for ($i=1; $i<=$flowmax; $i=$i+2)
{
    print "i=$i\n";

    $f1="resultdata/zresult-$queue-$i";
    system("awk -v flowno=$i -f ave.awk $f1 >> zres-$queue");
}
View Code

绘图代码:

#!/bin/bash

gnuplot -persist<<EOF

set terminal gif
set output "thrpt.gif"
set title "throughput RED vs DropTail"
set xlabel "FTP flow number"
set ylabel "throughput/kbps"
#unset key

plot "zres-DropTail" with linespoints, "zres-RED" with linespoints

EOF
View Code

RED,DropTail,的对比图,不过似乎没什么区别,还应该比较队列长度和吞吐量的关系

说明:

1、Tcl可以使用下面的代码提供参数输入:

if {$argc!=3} {
    puts "Usage: ns xx.tcl flowno_ seed_ queue_"
    puts "queue_ DropTail or RED"
    exit
}

set par1 [lindex $argv 0]
set par2 [lindex $argv 1]
set par3 [lindex $argv 2]
View Code

2、perl代码里面可以实现tcp flow的设定,随机数的设定,queue的设定,代码:

# flow max number, should be odd
my $flowmax=15;
my $queue="DropTail";
#my $queue=RED;
View Code

3、由于大批量的数据结果有许多文件,可以放到文件夹中,文件夹要提前建立,tcl中open。。。不能建立文件夹

当时记得笔记:

测试了3条tcp流通过一条瓶颈链路时,链路的吞吐量
通过采用不同的随机启动时间,得到不同的试验结果,可以平均一下

################################
可完善地方:
使用shell
#!/bin/bash

ns rng_tcp.tcl

awk -f rng_tcp.awk out.tr

如果ns xxx.tcl
这句可以带参数,那么就可以在shell中,写循环测试不同数量ftp流的吞吐率了
外层循环使用ftp流的数量
内层循环使用随机数种子


###################################
xx.tcl 后面可以带参数,tcl文件里还是$argc, $argv获取

使用perl可以实现ns命令的参数变化
system("ns rng_tcp.tcl $i $j");

> < 是输入输出重定向,创建新文件
>> << 也是输入输出重定向,追加旧文件

执行流程:
ns_awk.pl : ns + awk(提取tr文件数据)
total.pl  : awk(求平均)
plot.sh   : 绘图

初步感受到了linux批处理的强大

######################################
下一步测试:
1.将结果文件放入文件夹中,tcl路径语法
要提前建立好文件夹,下面语句不能建立文件夹
set tracefd [open resultdata/zout-$par3-$par1-$par2.tr w]

2.将中间瓶颈链路的Queue机制改变,测试效果
RED vs DropTail
结果:针对不同tcp flow,两种queue策略的吞吐量没什么区别,应该比较吞吐量和平均队列长度的关系
原文地址:https://www.cnblogs.com/yanhc/p/3076648.html