Share

Perl Script for calculating throughput, latency, delay, jitter, delivery ratio for ns2 Trace file

NS2 is discrete event simulator used to simulate real time network traffic and topology for analysis. For researcher it is proven to be great tool. NS2 has been developed in C++ and TCL. Otcl which is object oriented TCL, also used in NS2. For simulation purpose TCL programming is used and for adding new module C++ is used. Perl script can be used to easily analyze ns2 trace files.




NS2 Tutorials

 

Researchers working in the area of networking often use ns2 to test their proposed algorithms. Ns2 provides output in the form of trace files. These files later analyzed to compute metrics i.e. throughput, latency, delay.

Perl Script

#!/usr/bin/perl -w
$line=0;
$send=0;
$receive=0;

open(File,$ARGV[0]) or die "Can't Open File";
#open(File,"../analyzer/upload/one.tr") or die "Can't Open File";
$starttime=0;
$stoptime=0;
$byte=0;
$count=0;
while(chomp($line=<File>))
 {
    @arg=split(' ',$line);
    
  
  if($arg[1] eq "-t")
   {
   
    if($arg[0] eq "s")
     {
       if($arg[18] eq "AGT")
        {
          
          $send++;
      #    print "Packet Id:".$arg[40];
       #   print "  send time:".$arg[2];
          $sendtime{$arg[40]}=$arg[2];
          
          if($count==0)
            {
             $starttime=$arg[2];
             $count++;
            }
          
        }
     }
     if($arg[0] eq "r")
      {
        if($arg[18] eq "AGT")
         {
            $receivetime{$arg[40]}=$arg[2];
            $receive++;
            $byte+=$arg[36];
       #    print "\nPacket Id:".$arg[40];
       #    print "  Receive time:".$arg[2];
           if($stoptime<$arg[2])
            {
             $stoptime=$arg[2];
             }
           
         }
      }
    }
# For old Wireless Format    
  else
   {
       if($arg[0] eq "s")
        {
         if( $arg[3] eq "RTR")
          {
          $send++;
          $sendtime{$arg[5]}=$arg[1];
          
          if($count==0)
            {
             $starttime=$arg[1];
             $count++;
            }
          
          }
         }
       if($arg[0] eq "r")
        {
         if( $arg[3] eq "RTR")
          {
           $receivetime{$arg[5]}=$arg[1];
           $receive++;
           $byte+=$arg[7];
       #    print "\nPacket Id:".$arg[40];
       #   print "  Receive time:".$arg[2];
           if($stoptime<$arg[1])
            {
             $stoptime=$arg[1];
             }
           
          }
        }  
           
   }
 
 }
$delay=0;
$s=0;
@difference=();
 foreach $pkid (sort {$a<=>$b} keys %sendtime)
  {
   # print "\n".$pkid." Send Time:".$sendtime{$pkid};
   # print "\n".$pkid." Receive Time:".$receivetime{$pkid};
    
    if( defined($receivetime{$pkid}))
       {
        $delay+=$receivetime{$pkid}-$sendtime{$pkid};
        $difference[$s]=$delay;
       # print "\n".$pkid." Delay:".$delay;
        $s++;
      }
 #  <STDIN>;
  }
# Jitter Calculation  
$totaldiff=0;  
$jittersample=0;
  for($i=0;$i<$s-1;$i++)
   {
      $totaldiff += $difference[$i+1]-$difference[$i];
      $jittersample++;
   }
  





 print "\n\n===========================================================";


 print  "\n\nPacket Sent:".$send;
 print  "\n\nPacket Received:".$receive;
 print  "\n\nDropped Packets:".($send-$receive);
 print  "\n\nPacket Delivery Ratio:".($receive/$send);
 print  "\n\nStart Time:".$starttime;
 print  "\n\nStop Time:".$stoptime;
 print  "\n\nTotal bytes:".$byte;
 print  "\n\nThroughput : ".($byte/($stoptime-$starttime))*(8/1024)." kbps";
 print  "\n\nEnd to End Delay:".$delay/$s;
 print  "\n\nJitter :".$totaldiff/$jittersample;
 print "\n\n============================================================";

 

  • Shashi Kant

    Using the above perl script whenever simulation is done packet received is always greater than packet sent.
    for example:

    Packet Sent:23

    Packet Received:683

    Dropped Packets:-660

    Packet Delivery Ratio:29.695652173913

    Start Time:10.007236149

    Stop Time:30.396221893

    Total bytes:83004

    Throughput : 31.8048557266184 kbps

    End to End Delay:-0.504280644619047

    Jitter :-0.53072160525

    please provide me the suitable perl script.