Sunday, May 5, 2013

Use Xgraph with NS2

XGRAPH is a general purpose x-y data plotter with interactive buttons for panning, zooming, printing, and selecting display options. It will plot data from any number of files on the same graph and can handle unlimited data-set sizes and any number of data files.

How to Install:
sudo apt-get install xgraph
view raw gistfile1.txt hosted with ❤ by GitHub
This example goes with two previous posts:
1. NS2 UDP Example
2. NS2 TCP Example

You will need both source code to plot ns2 simulations in this example.

Additionally we have to use these perl scripts to extract data from output trace generated from NS2.

# type: perl throughput.pl <trace file> <required node> <granlarity> > output file
$infile=$ARGV[0];
$tonode=$ARGV[1];
$granularity=$ARGV[2];
#we compute how many bytes were transmitted during time interval specified
#by granularity parameter in seconds
$sum=0;
$clock=0;
open (DATA,"<$infile")
|| die "Can't open $infile $!";
while (<DATA>) {
@x = split(' ');
#column 1 is time
if ($x[1]-$clock <= $granularity)
{
#checking if the event corresponds to a reception
if ($x[0] eq 'r')
{
#checking if the destination corresponds to arg 1
if ($x[3] eq $tonode)
{
#checking if the packet type is TCP
if ($x[4] eq 'tcp')
{
$sum=$sum+$x[5];#number of bytes in the period
}
}
}
}
else
{ $throughput=$sum/$granularity;
print STDOUT "$x[1] $throughput\n";
$clock=$clock+$granularity;
$sum=0;
}
}#end while
$throughput=$sum/$granularity;
print STDOUT "$x[1] $throughput\n";
$clock=$clock+$granularity;
$sum=0;
close DATA;
exit(0);
view raw Perltcp.pl hosted with ❤ by GitHub
# type: perl throughput.pl <trace file> <required node> <granlarity> > output file
$infile=$ARGV[0];
$tonode=$ARGV[1];
$granularity=$ARGV[2];
#we compute how many bytes were transmitted during time interval specified
#by granularity parameter in seconds
$sum=0;
$clock=0;
open (DATA,"<$infile")
|| die "Can't open $infile $!";
while (<DATA>) {
@x = split(' ');
#column 1 is time
if ($x[1]-$clock <= $granularity)
{
#checking if the event corresponds to a reception
if ($x[0] eq 'r')
{
#checking if the destination corresponds to arg 1
if ($x[3] eq $tonode)
{
#checking if the packet type is TCP
if ($x[4] eq 'cbr')
{
$sum=$sum+$x[5];#number of bytes in the period
}
}
}
}
else
{ $throughput=$sum/$granularity;
print STDOUT "$x[1] $throughput\n";
$clock=$clock+$granularity;
$sum=0;
}
}#end while
$throughput=$sum/$granularity;
print STDOUT "$x[1] $throughput\n";
$clock=$clock+$granularity;
$sum=0;
close DATA;
exit(0);
view raw Perlcbr.pl hosted with ❤ by GitHub
I have written a shell script to do our job quickly.
#!bin/bash
perl Perltcp.pl outudp.tr 4 0.5 > tcp-udp-one.tr
perl Perlcbr.pl outudp.tr 5 0.5 > tcp-udp-two.tr
xgraph -bg white tcp-udp-one.tr tcp-udp-two.tr &
perl Perltcp.pl outtcp.tr 4 0.5 > tcp-tcp-one.tr
perl Perltcp.pl outtcp.tr 5 0.5 > tcp-tcp-two.tr
xgraph -bg white tcp-tcp-one.tr tcp-tcp-two.tr &
view raw genGraph.sh hosted with ❤ by GitHub
sh genGraph.sh
view raw gistfile1.txt hosted with ❤ by GitHub

Sample out put:
 



9 comments:

  1. sir please explain what is the meaning of this perl script
    perl Perltcp.pl outudp.tr 4 0.5 > tcp-udp-one.tr

    what is 4 and 0.5 here

    Thanks

    ReplyDelete
    Replies
    1. # type: perl throughput.pl > output file

      #we compute how many bytes were transmitted during time interval specified by granularity parameter in seconds

      go through your out put trace files.

      Delete
  2. sir please provide a perl script to calculate energy of nodes in a sensor networks

    Thanks

    ReplyDelete
  3. i am writing the code perfectly, yet it says that no file or directory is found at line 8! Please help.

    ReplyDelete
    Replies
    1. you need to provide out put files from ns 2 simulations,

      Delete
  4. hello can u tell me how to draw xgraph for 802.11 and 802.15.4 in ns2 i have trace and nam file both i dont know how to plot exatly both the curve in xgraph n write code gone thr many examples but still not getting it plz mail me @ kaveris.career@gmail.com

    ReplyDelete
  5. Hello sir
    I am using awk scripts for plotting graphs in ns2 using awk -f abc.awk abc.tr > abc.xg
    and after that i plot graph with xgraph abc.xg
    this command displays values for single input i.e abc.xg
    Now i want to plot a graph in which i can compare values for different xg files to get the graphs shown in this blog using perl scripting.
    Please tell me how to do this with awk scripts.
    Thanks

    ReplyDelete
  6. sir please explain what is the meaning of the 10 the power 3

    ReplyDelete