Monday, 20 April 2015

awk script for throughput -energy- end-to-end-delay in ns-2 explained with example

In Brief :

Awk utility is powerful data manipulation scripting programming language (In fact based
on the C programming Language).One can use awk to handle complex task such as calculation, database handling, report creation etc.


Trace File in NS-2
          The trace file will be generated for every execution of your routing TCL script. There are mainly two types of formats available 
  • Old trace File and 
  • New trace File 
 Old trace file format will be default and that got generated on execution of TCL script ,you can enable the New Trace file by placaing this code in TCL script 

Generation of New trace file format in NS-2

   $ns use-newtrace    // This specifies the new trace file.



Generated Trace file -- Old 

  s     2.556838879    _0_     RTR      ---   0 AODV    48    [0 0 0 0] -------    [0:255 -1:255 30 0] 

[0x2 1 1 [1 0] [0 4]  ]    (REQUEST) 

Generated Trace file --New 

s -t 0.100000000 -Hs 0 -Hd -2 -Ni 0 -Nx 1.00 -Ny 50.00 -Nz 0.00 -Ne -1.000000 -Nl AGT -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 0.0 -Id 2.0 -It tcp -Il 40 -If 0 -Ii 0 -Iv 32 -Pn tcp -Ps 0 -Pa 0 -Pf 0 -Po 0 


AWK Script will parse the above file and generates the Network metrics such as
Packet delivery ratio,Through put, Delay , Jitter  and Network Routing Load.

Command to execute:

>> awk -f   network_metric_filename.awk    generated_tracefilename.tr

The above command parses the trace file(generated_tracefilename.tr) and calculate results based on the logic in awk script(network_metric_filename.awk ).

Trace file explanation and awk script explanation

Please check here :   Trace File Explanation

Throughput Calculation for above new trace file format in ns-2:

BEGIN {
       recvdSize = 0
       startTime = 0    # begin block used for initializing variables 
       stopTime = 400
  }
   
  {
             event = $1       # $1 indicates the first column in the row -> event = s (send ) which is first in trac file
             time = $3       # $3 indicates the 3rd column -> time = 0.100000000 
             node_id = $9       # this block mainly parsing the trace file and getting the data frm the trace fiel 
             pkt_size = $37
             level = $19    # $19 is AGT which is 19th column(position) in the row. 
   
  # Store start time
  if (level == "AGT" && event == "s" && pkt_size > 512) {
    if (time < startTime) {
             startTime = time        # check event as send and its application layer with size > 512 bytes
             }
       }
   
  # Update total received packets' size and store packets arrival time
  if (level == "AGT" && event == "r" && pkt_size > 512) {
       if (time > stopTime) {
             stopTime = time     # check event as receive and its application layer with size > 512 bytes
             }
       # Rip off the header
       hdr_size = pkt_size % 512
       pkt_size -= hdr_size
       # Store received packet's size
       recvdSize += pkt_size
       }
  }
   
  END {
       printf("Average Throughput[kbps] = %.2f\t\t StartTime=%.2f\tStopTime=%.2f\n",(recvdSize/(stopTime-startTime))*(8/1000),startTime,stopTime)
  }

Throughput calculation easy and core part :


start time=  Packet transfer started time
stop time =  Packet transfer stopped time
recvdSize = Total packet received size
8/1000 = Converting in to kbps (kilo bits per second)

Throuhput =      (recvdSize)                       8
                            ___________        *     _______
                         (stopTime-startTime)      1000


2 comments: