Tcl代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
# Kezhiheng, experiment 4, test RNG, 3 tcp flow# Usage: ns xx.tcl flowno seedif { $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 objectset ns [new Simulator]set tracefd [open resultdata/zout-$par3-$par1-$par2.tr w]$ns trace-all $tracefdset namtracefd [open out.nam w]$ns namtrace-all $namtracefdproc finish {} { global ns tracefd namtracefd $ns flush-trace close $tracefd close $namtracefd # exec nam out.nam & exit 0}# 3 TCP flowset nflow $par1# Set router nodesset 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, linkfor {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 trafficfor {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 $par2set rvStart [new RandomVariable/Uniform]$rvStart use-rng $rng$rvStart set min_ 0.0$rvStart set max_ 1.0for {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
从tr提取吞吐量数据的awk代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
# Measure the throughput by the trace fileBEGIN{ # 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);}
求平均的awk代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
# Measure the throughput by the trace fileBEGIN{ # 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);}
综合的perl代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#!/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 oddmy $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 averageprint "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");}
绘图代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#!/bin/bashgnuplot -persist<
RED,DropTail,的对比图,不过似乎没什么区别,还应该比较队列长度和吞吐量的关系
说明:
1、Tcl可以使用下面的代码提供参数输入:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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]
2、perl代码里面可以实现tcp flow的设定,随机数的设定,queue的设定,代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
# flow max number, should be oddmy $flowmax=15;my $queue="DropTail";#my $queue=RED;
3、由于大批量的数据结果有许多文件,可以放到文件夹中,文件夹要提前建立,tcl中open。。。不能建立文件夹
当时记得笔记:
测试了3条tcp流通过一条瓶颈链路时,链路的吞吐量通过采用不同的随机启动时间,得到不同的试验结果,可以平均一下################################可完善地方:使用shell#!/bin/bashns rng_tcp.tclawk -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策略的吞吐量没什么区别,应该比较吞吐量和平均队列长度的关系