zoukankan      html  css  js  c++  java
  • NS 2.35 柯志亨书实验5笔记置信区间

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

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

    笔记图:

    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
    View Code

    最后的数据结果:

    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
    View Code

    绘图的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

    图形:

    本来想在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
  • 相关阅读:
    AngularJS ui-router (嵌套路由)
    解决Can't connect to MySQL server on 'localhost' (10048)
    PHP获取一年有几周以及每周开始日期和结束日期
    MySQL(八)之DML
    MySQL(七)MySQL常用函数
    MySQL(六)之MySQL常用操作符
    MySQL(五)之DDL(数据定义语言)与六大约束
    MySQL(四)之MySQL数据类型
    MySQL(三)之SQL语句分类、基本操作、三大范式
    linux命令详解之netstat
  • 原文地址:https://www.cnblogs.com/yanhc/p/3076718.html
Copyright © 2011-2022 走看看