Calculate throughput in NS-2

Following awk file will calculate the throughput with running time (throughput versus time).
The following code will count all the received application packets in a network such that we can calculated the network throughput. If a throughput of a specific node has to be calculated, then we can simply add the node_id in the if condition.

The code simply prints the observed throughput during the time_interval through out the simulation time. We can change the time_interval variable according to our requirements.

In the following code:
  • packet_size * recv * 8.0 gives the total number of bits received. Packet size is the size of packed used in Application layer.
  • diving the value by 1000 gives us the throughput in kbps.


#============================= throughput.awk ========================

BEGIN {
recv=0;
gotime = 1;
time = 0;
packet_size = 50;
time_interval=2;
}
#body
{
       event = $1
             time = $2
             node_id = $3
             level = $4
             pktType = $7

if(time>gotime) {

print gotime, (packet_size * recv * 8.0)/1000; #packet size * ... gives results in kbps
gotime+= time_interval;
recv=0;
}

#============= CALCULATE throughput=================

if (( event == "r") && ( pktType == "cbr" ) && ( level=="AGT" ))
{
recv++;
}

} #body


END {
;
}
#============================= Ends ============================

Graph generated by above awk script.


P.S: feel free to contact me for any help.



56 comments:

  1. hi sir.

    what is the syntax to count the throughput from .tr and .awk file in NS2 ?

    pradeep.vanparia@gmail.com

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. hi sir
    where I need to put this code after which step in my scenario ? and the result will be in the trace file??

    rroaa86@yahoo.com

    ReplyDelete
  4. Good day sir, please how can i convert my trace file to matlab format, as i am trying to get my input into NN toolbox for training
    regards
    leye

    ReplyDelete
  5. How to calculate the end -to -end throughput for wired network from source-destination??

    ReplyDelete
    Replies
    1. let t1 be the send time of first packet by source
      let t2 be the send time of last packet by source
      let n by the number of packets sent
      let s be the size of a packet

      Now, throughput =
      8*n*s
      ------------ (in Kbits/s)
      (t2-t1)*1000

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Thanks for the reply. I have two groups of nodes. group1 has a flowID=1 and group2 has a flowID= 2. I have a dumbbell topology:

    group1

    ---Node 1------------Node 2----------- Node 3 (receiver)
    group 2

    I want to calculate the the throughput for group 1 and group 2 ,,,,, I would appreciate your reply.

    ReplyDelete
    Replies
    1. Dear

      just filter your result through Flow Id , use $7....

      Delete
  8. nice blog for researcher and for all IT student.

    ReplyDelete
  9. Hello sir,

    If I have the throughput value from the awk script, then after that how should I plot a graph throughput vs time. what will be the next step.
    Plz help.

    Thanks

    ReplyDelete
    Replies
    1. u may use any graph plotting software such as ms excel, origin, sigma plot etc. as your wish...

      Delete
    2. Try using gnuplot if you are using linux.
      here is link for tutorials
      http://www.youtube.com/watch?v=9k-l_ol9jok
      Otherwise excel can be fine for beginners.

      Delete
  10. Hello sir,
    After plotting the graph, how can i conclude that. I can't understand the conclusion for this results.

    ReplyDelete
  11. The graph shows throughput w.r.t time. Increase in throughput means decrease in packet loss whereas, decrease in throughput means increase in packet loss may be due to collision or errrors..

    ReplyDelete
  12. Hi sir,

    I am grateful to you for the code above to generate a graph file from the ns2 tracefile, however, there are few things to note about your code above,

    The code above calculates a fixed packetsize of 50 bytes, not based on user defined on the simulation file, so I modified your code, the calculation to be almost same as this in his tcl code
    puts $f0 "$now [expr $bw0/$time*8/1000000]" # in MB
    from
    http://www.isi.edu/nsnam/ns/tutorial/ns.html, and compared the initial results, most of the data are the same although some of them are not by about 1-5 offsets I think.

    where
    $bw0 = bytes received
    $time = interval

    where I modified the calculation part is
    print gotime, ((packetsize * recv)/ interval * 8/1000) #for KB

    where the packetsize on above calculations are taken from ns2 tracefile records

    Currently I am working on modifying this to accommodate the new tracefile format

    Thank you so much for this code.

    -asraf

    ReplyDelete
  13. sir can u provide me all the awk script for ns2... plz sir sir help me
    regards
    davenda
    devsa.devra@gmail.com

    ReplyDelete
  14. Sir i want to implement 802.15.4 in NS2 ....can you please help me....?
    I just want to measure performance parameters.....

    ReplyDelete
    Replies
    1. ok I will try to make new blog on IEEE 802.15.4 soon. I hope it will be helpful for others also....

      Delete
  15. Sir what is the maximum value of throughput in a network ..

    ReplyDelete
    Replies
    1. throughput = number of bits received / time to receive those bits
      however, the number of bits received should be the pure data bits excluding control packets. If you continuously transmit data for 1 sec and measure the throughput for that 1 sec then the throughput is exactly equal to data rate. But in practice, throughput is always less than data rate. The maximum theoretical value of throughput can be close to employed data rate.

      Delete
  16. Hi sir,

    I'm sorry in going for off-topic a little bit, do you know how to use AODV-UU properly for simulation scripts?

    -asraf

    ReplyDelete
  17. hello Sir,
    I am working with simulation of wired cum wireless topology in ns2. Please tell how to set the bandwidth of the link from the base station to the terminal is
    1Mbps, and the delay is 0.01ms.

    ReplyDelete
  18. Hi sir!!

    how do i get a graph for throughput? can you please tell me the format for calling a graph

    ReplyDelete
    Replies
    1. you can use any graph plotting tool such as excel or you can also use xgraph command
      link for xgraph is http://www.xgraph.org/

      Delete
  19. hi sir ! I'm student from indonesia.

    sir what a difference Old and New trace files ? i have some trouble when i calculate awk from trace files

    this is an error : " awk : awk_throuhput.awk:312:(FILENAME:wpanc.tr FNR:52523) fatal:division by zero attemped "

    may i get your sugestion ?

    ReplyDelete
  20. try to check the value of divisor..the error is because you are trying to divide by zero..

    ReplyDelete
  21. Hi Pranesh
    please tell how to make the graph after getting throughput fro different time duration
    thanks

    ReplyDelete
    Replies
    1. u may use any graph plotting software such as ms excel, origin, sigma plot etc. as your wish...

      Delete
  22. it means start time is always 0 for values given in graph in research papers
    because as time increases throughput increases
    means for time duration 20-30
    start time 0 stop time 20
    start time 0 stop time 30

    ReplyDelete
  23. After parsing the trace file I am getting -nan in the output. how to fix this ?

    ReplyDelete
  24. Hi,
    I am trying to calculate throughput ,delay and packet loss for a wired cum wireless network. Please, can you help me in this because the trace file is mixed between the wired and wireless format.

    ReplyDelete
  25. Hi,
    I am trying to calculate throughput ,delay and packet loss for a wired cum wireless network. Please, can you help me in this because the trace file is mixed between the wired and wireless format.

    ReplyDelete
  26. hi,
    I tried this code and I get throughput= -0.0 and start time =400 , end time =0
    What should I do to get the actual throughput?

    ReplyDelete
    Replies
    1. Did you supply trace file name??
      eg. throughput.awk trace.tr

      Delete
  27. Sir,

    I had a doubt in the formula you used in calculating throughput. Aren't the total number of bit supposed to be divided by the time interval, which is two in this case?

    Thank You

    ReplyDelete
  28. i want correct formula for calculating throughput for TCP protocol for wired Network.. pls help me

    ReplyDelete
  29. hello sir,
    i do not understand how the coding was done, please can you explain it line by line.

    ReplyDelete
  30. hello sir,
    i do not understand how the coding was done, please can you explain it line by line.

    ReplyDelete
  31. hello,
    could you help me to calculate throughput and e2edelay for wire networking
    kind regareds

    ReplyDelete
  32. Hi Sir,

    I am new to NS2. I want to plot graph using xgraph tool. I have a simple tcl code of two nodes that send packets to each other. I have an awk script to extract the time and packet size. When i run the awk script i get only a single row from which i cannot plot the graph. Please help me in this code.

    trace file format:

    + 0.51 0 1 cbr 100 ------- 0 0.0 1.0 2 4
    - 0.51 0 1 cbr 100 ------- 0 0.0 1.0 2 4
    + 0.51 1 0 cbr 100 ------- 0 1.1 0.1 2 5
    - 0.51 1 0 cbr 100 ------- 0 1.1 0.1 2 5
    r 0.5108 0 1 cbr 100 ------- 0 0.0 1.0 0 0
    ....
    .....


    AWK script:

    BEGIN {
    recvdSize = 0
    startTime = 0
    stopTime = 500
    }
    {
    event = $1
    time = $2
    node_id = $3
    node_id = $4
    pkt_size = $6
    level = $5
    # Store start time
    if ($1 == "r" && $5 == "cbr" && pkt_size = 100) {
    if (time < startTime) {
    startTime = time
    }
    }
    # Update total received packets' size and store packets arrival time
    if ($1 == "r" && $5 == "cbr" && pkt_size = 100) {
    if (time < stopTime) {
    stopTime = time
    }
    # Rip off the header
    #hdr_size = pkt_size % 512
    #pkt_size -= hdr_size
    # Store received packet's size
    recvdSize += pkt_size
    }
    #printf("Value of the number of packets",recvdSize)
    }

    END {
    printf("%10s %10g",event, time)
    printf("Value of the number of packets %d",recvdSize)
    printf("Average Throughput[kbps] = %.2f\t\t StartTime=%.2f\tStopTime=%.2f\n",(recvdSize/(stopTime-startTime))*(8/1000),startTime,stopTime)
    }

    Ouput after running the AWK script:

    r 4.5108 Value of the number of packets 160200 Average Throughput[kbps] = 2509.01 StartTime=0.00 StopTime=0.51


    Please help me understanding the code and how to plot the graph.

    Thanks & Regards

    ReplyDelete
  33. kindly help me in understanding where to put the code and how ro observe the results sir

    ReplyDelete
  34. kindly help me in understanding where to put the code and how ro observe the results sir

    ReplyDelete
  35. its a awk file so please read about awk in linux. thanks

    ReplyDelete
  36. hello sir
    can you please provide me awk script for wired network thanks in advance!!!!!!!!!!!!!!!!!!

    ReplyDelete
  37. hello sir.. i want to know how to execute this throughput code. where to place this code in tcl file?

    ReplyDelete
  38. Hello sir,
    I want to find out packet loss for AODV and also want to plot graph for that,Please guide me .

    ReplyDelete
  39. hello,
    how to calculate throughput for each and every node in wireless networks.so that plotting of graph with numerous values will be accurate

    ReplyDelete
  40. Hello sir?
    I have seen some of your blogs.Please guide me regarding how to integrate packet loss differentiation algorithms for any scenario taken on NS2?
    Thank you sir

    ReplyDelete
  41. hello sir,
    can u help us in the ollowing matter.
    1)using this awk file in our program can we plot xgraph???
    and if yes what is the command for that

    ReplyDelete
    Replies
    1. You can use bash script for that. Create a bash script, call awk script from bash file and then dump the result into a temp file using '>' operation and then call xgraph.
      You bash file would look like as follows
      e.g.
      ns xyz.tcl
      awk -f throghput.awk xyz.tr > th.txt
      xgraph th.txt



      Delete
  42. Hi

    Do you have scripts for new trace format?

    ReplyDelete
  43. Sir: How we can change the packet size and time interval between packets in running time. pleas help me Sir!

    ReplyDelete
  44. Sir Awk file not run in ns2
    error -f command not found how?

    ReplyDelete
  45. Sir, can you please explain how to calculate throughput for wireless network using trace and awk files.

    ReplyDelete