#!/usr/bin/perl

use CGI qw(:standard);
use POSIX;  # for ceil() and floor() functions

print header();
#@names=param();
#print "names=@names<br>";
#foreach $name (param()) {
#   $value=param($name);
#   print "name=$name value=$value<br>";
#}


#$gnuplot = "wgnuplot"; # use window gnuplot
$gnuplot = "/usr/bin/gnuplot"; # on linux comment the above line

#$datadir="data" # for standalone execution
$datadir="../../../html/fujitsu/abwimpact";  # for CGI web application
$imgdir="/fujitsu/abwimpact"; # directory contains PNG files
$runName="run1";


$plotScale = 5.0;  # abw changes 10 time every data points
$numberOfDataPoints = 10; # on the plot (x axis);

@documentSize = (1, 10, 100, 1000, 10000); # in kB 
$documentSize = 1; # 1kB (not 1KB)
$serverSpeed = 1;  # 1MB/s
$abwMax = 100000;  # in kbps, default Maximum Available Bandwidth 100Mb/s
$abwAvg = 20000;   # in kbps, default Average Available Bandwidth 20Mb/s
$abwMin = 1000;    # in kbps, default Minimum Available Bandwidth 1Mb/s
$noOfServers=5;
$noOfSimulationRuns=50;

$abwMaxForm=param('abwMax');
$abwMinForm=param('abwMin');
#$abwAvgForm=param('abwAvg');
$noOfServersForm=param('noOfServers');
$noOfSimulationRunsForm=param('noOfSimulationRuns');
$runNameForm=param('runName');
if ($abwMaxForm) { $abwMax=$abwMaxForm; }
if ($abwAvgForm) { $abwAvg=$abwAvgForm; }
if ($abwMinForm) { $abwMin=$abwMinForm; }
if ($noOfServersForm) { $noOfServers=$noOfServersForm; }
if ($noOfSimulationRunsForm) { $noOfSimulationRuns=$noOfSimulationRunsForm; }
if ($runNameForm) { $runName=$runNameForm; }

#print "abwMaxForm=$abwMaxForm<br>";
#print "abwMinForm=$abwMinForm<br>";
#print "noOfServersForm=$noOfServersForm<br>";

$ratioMax2Avg=$abwMax/$abwAvg;
$ratioMax2Min=$abwMax/$abwMin;

simulate();
genSimPlot();
genSimWebPage();
exit();
genPlotData($abwMax);
genPlot();
genImproveRatioPlotData();
genRatioPlot();
genDocSizeData($abwMax);
genDocSizePlot();
genServerProcessingSpeedData($abwMax);
genServerProcessingSpeedPlot();
genWebPage();

sub simulate {
   mkdir "$datadir\/$runName";
   mkdir "$datadir\/$runName\/data";
   mkdir "$datadir\/$runName\/data/acc";
   mkdir "$datadir\/$runName\/data/abs";
   mkdir "$datadir\/$runName\/data/rel";

   open(OUTaccmax, ">$datadir\/$runName\/data/acc/maxData.txt");# acc dataset
   open(OUTaccran, ">$datadir\/$runName\/data/acc/ranData.txt");# acc dataset
   open(OUTaccavg, ">$datadir\/$runName\/data/acc/avgData.txt");# acc dataset
   open(OUTaccmin, ">$datadir\/$runName\/data/acc/minData.txt");# acc dataset
   open(OUTabsmax, ">$datadir\/$runName\/data/abs/maxData.txt");# abs dataset
   open(OUTabsran, ">$datadir\/$runName\/data/abs/ranData.txt");# abs dataset
   open(OUTabsavg, ">$datadir\/$runName\/data/abs/avgData.txt");# abs dataset
   open(OUTabsmin, ">$datadir\/$runName\/data/abs/minData.txt");# abs dataset
   open(OUTrelmax, ">$datadir\/$runName\/data/rel/maxData.txt");# rel dataset
   open(OUTrelran, ">$datadir\/$runName\/data/rel/ranData.txt");# rel dataset
   open(OUTrelavg, ">$datadir\/$runName\/data/rel/avgData.txt");# rel dataset
   open(OUTrelmin, ">$datadir\/$runName\/data/rel/minData.txt");# rel dataset

   $accabwMax=0;$accabwRandom=0;$accabwAvg=0;$accabwMin=0;
   for ($i=1; $i<=$noOfSimulationRuns; $i++) {
      ($rabwMax[$i], $rabwRandom[$i], $rabwAvg[$i], $rabwMin[$i])=
          abwCurrentRun();
      $accabwMax    += $rabwMax[$i];
      $accabwRandom += $rabwRandom[$i];
      $accabwAvg    += $rabwAvg[$i];
      $accabwMin    += $rabwMin[$i];
      $avgabwMax[$i]   = $accabwMax/$i;
      $avgabwRandom[$i]= $accabwRandom/$i;
      $avgabwAvg[$i]   = $accabwAvg/$i;
      $avgabwMin[$i]   = $accabwMin/$i;
      #print "run $i<br>\n";
      #print "current $rabwMax[$i] $rabwRandom[$i] $rabwAvg[$i] $rabwMin[$i]\n";
      #print "acc $avgabwMax[$i] $avgabwRandom[$i] $avgabwAvg[$i] $avgabwMin[$i]\n";
      print OUTaccmax "$i $avgabwMax[$i]\n";
      print OUTaccran "$i $avgabwRandom[$i]\n";
      print OUTaccavg "$i $avgabwAvg[$i]\n";
      print OUTaccmin "$i $avgabwMin[$i]\n"; 

      print OUTabsmax "$i $rabwMax[$i]\n";
      print OUTabsran "$i $rabwRandom[$i]\n";
      print OUTabsavg "$i $rabwAvg[$i]\n";
      print OUTabsmin "$i $rabwMin[$i]\n";

      $relabwRandom=$rabwRandom[$i]/$rabwMax[$i];
      $relabwAvg=$rabwAvg[$i]/$rabwMax[$i];
      $relabwMin=$rabwMin[$i]/$rabwMax[$i];
      print OUTrelmax "$i 1.0\n";
      print OUTrelran "$i $relabwRandom\n";
      print OUTrelavg "$i $relabwAvg\n";
      print OUTrelmin "$i $relabwMin\n";
   }
   close(OUTaccmax); close(OUTaccran); close(OUTaccavg); close(OUTaccmin);
   close(OUTabsmax); close(OUTabsran); close(OUTabsavg); close(OUTabsmin);
   close(OUTrelmax); close(OUTrelran); close(OUTrelavg); close(OUTrelmin);
}

sub abwCurrentRun {
   $maxABW=0; $minABW=$abwMax; $accABW=0;
   for (my $i=0; $i<$noOfServers; $i++) {
      $abw[$i]=$abwMin+($abwMax-$abwMin)*rand();
      if ($abw[$i] > $maxABW){ $maxABW=$abw[$i];}
      if ($abw[$i] < $minABW){ $minABW=$abw[$i];}
      $accABW+=$abw[$i];
   }
   $random= rand $noOfServers;
   $irandom=floor($random);
   $rand=($irandom==10)?9:$irandom;
   #print "rand=$rand,  random=$random\n";
   $randomABW=$abw[$rand];
   $avgABW=$accABW/$noOfServers;
   return ($maxABW, $randomABW, $avgABW, $minABW);   
}

sub genSimPlot{
# Generate gnuplot data for accumulated simulation results

     open(GNUPLOTCMD, "> $datadir\/$runName\/data/gnuplotcmd");
     print GNUPLOTCMD "set term png small color\n";
     print GNUPLOTCMD "set size 1.20, 0.54\n"; 
     print GNUPLOTCMD "set output \"$datadir\/$runName\/accabwImpact.png\" \n";
     print GNUPLOTCMD "set title \"Analysis of Available Bandwidth Impact (Accumulated Simulation Results)\" \n";
     print GNUPLOTCMD "set grid\n";
     #print GNUPLOTCMD "set logscale xy\n";
     #print GNUPLOTCMD "show logscale\n";
     print GNUPLOTCMD "set ylabel \'Bandwidth (kbps)\' \n";
     print GNUPLOTCMD "set xlabel \'Simulation Time\' \n";
     print GNUPLOTCMD "plot \"$datadir\/$runName\/data/acc/maxData.txt\" with linesp  1, \"$datadir\/$runName\/data/acc/ranData.txt\" with linesp 2, \"$datadir\/$runName\/data/acc/avgData.txt\" with linesp 3, \"$datadir\/$runName\/data/acc/minData.txt\" with linesp 4\n";
     close GNUPLOTCMD;
     system "$gnuplot $datadir\/$runName\/data/gnuplotcmd";

#Plot absolute individual Simulation Run Results

     open(GNUPLOTCMD, "> $datadir\/$runName\/data/gnuplotcmd");
     print GNUPLOTCMD "set term png small color\n";
     print GNUPLOTCMD "set size 1.20, 0.54\n"; 
     print GNUPLOTCMD "set output \"$datadir\/$runName\/absabwImpact.png\" \n";
     print GNUPLOTCMD "set title \"Analysis of Available Bandwidth Impact (Individual Simulation Results, Absolute Scale)\" \n";
     print GNUPLOTCMD "set grid\n";
     #print GNUPLOTCMD "set logscale xy\n";
     #print GNUPLOTCMD "show logscale\n";
     print GNUPLOTCMD "set ylabel \'Bandwidth (kbps)\' \n";
     print GNUPLOTCMD "set xlabel \'Simulation Time\' \n";
     print GNUPLOTCMD "plot \"$datadir\/$runName\/data/abs/maxData.txt\" with linesp  1, \"$datadir\/$runName\/data/abs/ranData.txt\" with linesp 2, \"$datadir\/$runName\/data/abs/avgData.txt\" with linesp 3, \"$datadir\/$runName\/data/abs/minData.txt\" with linesp 4\n";
     close GNUPLOTCMD;
     system "$gnuplot $datadir/$runName\/data/gnuplotcmd";


#Plot relative individual Simulation Run Results (maximum abw as 1)

     open(GNUPLOTCMD, "> $datadir\/$runName\/data/gnuplotcmd");
     print GNUPLOTCMD "set term png small color\n";
     print GNUPLOTCMD "set size 1.20, 0.54\n"; 
     print GNUPLOTCMD "set output \"$datadir\/$runName\/relabwImpact.png\" \n";
     print GNUPLOTCMD "set title \"Analysis of Available Bandwidth Impact (Individual ABW Simulation Results, Relative Scale)\" \n";
     print GNUPLOTCMD "set grid\n";
     #print GNUPLOTCMD "set logscale xy\n";
     #print GNUPLOTCMD "show logscale\n";
     print GNUPLOTCMD "set ylabel \'Bandwidth (Max ABW as 1)\' \n";
     print GNUPLOTCMD "set xlabel \'Simulation Time\' \n";
     print GNUPLOTCMD "plot \"$datadir\/$runName\/data/rel/maxData.txt\" with linesp  1, \"$datadir\/$runName\/data/rel/ranData.txt\" with linesp 2, \"$datadir\/$runName\/data/rel/avgData.txt\" with linesp 3, \"$datadir\/$runName\/data/rel/minData.txt\" with linesp 4\n";
     close GNUPLOTCMD;
     system "$gnuplot $datadir\/$runName\/data/gnuplotcmd";

}

sub genSimWebPage{

   open(OUT, ">$datadir/$runName/index.html");
   print OUT start_html(
         -title => "Avaiable Bandwidth Impact Analysis (Simulation Results)",
         -bgcolor=>'#EDDF99');
   print OUT "<center><h3>MaximumABW=$abwMax kbps; MinimumABW=$abwMin kbps<br>\n";
   print OUT "noOfServers=$noOfServers; noOfSimulationRuns=$noOfSimulationRuns<br>\n";
   print OUT "<font color=red>red line: optimal solution using MaxABW</font>, <font color=green>green line: Randomly choose a server</font>, <br><font color=cyan>cyan line: worst case choose MinABW</font>, <font color=blue>blue line: average bandwidth</font><p>\n";
   print OUT "<img src=\"$imgdir/$runName\/accabwImpact.png\">\n";
   print OUT "<p><img src=\"$imgdir/$runName\/absabwImpact.png\">\n";
   print OUT "<p><img src=\"$imgdir/$runName\/relabwImpact.png\">\n";
   print OUT end_html;

   print start_html(
         -title => "Avaiable Bandwidth Impact Analysis (Simulation Results)",
         -bgcolor=>'#EDDF99');
   print "<center><h3>MaximumABW=$abwMax kbps; MinimumABW=$abwMin kbps<br>\n";
   print "noOfServers=$noOfServers; noOfSimulationRuns=$noOfSimulationRuns<p>\n";
   print "<font color=red>red line: optimal solution using MaxABW</font>, <font color=green>green line: Randomly choose a server</font>, <br><font color=cyan>cyan line: worst case choose MinABW</font>, <font color=blue>blue line: average bandwidth</font><p>\n";

   print "<img src=\"$imgdir/$runName\/accabwImpact.png\">\n";
   print "<p><img src=\"$imgdir/$runName\/absabwImpact.png\">\n";
   print "<p><img src=\"$imgdir/$runName\/relabwImpact.png\">\n";
   print "<p><a href=\"$imgdir/$runName/\">link to saved web page</a>\n";
   print end_html;
}

sub genWebPage{
   print start_html(
         -title => "Impact of Response Time when use Avaiable Bandwidth",
         -bgcolor=>'#EDDF99');
   print "<center><h3>DocumentSize=$documentSize kB<br>ServerSpeed=$serverSpeed MB/s<br>MaximumABW=$abwMax kbps<br>Max2AvgABWRatio=$ratioMax2Avg<br>Max2MinABWRatio=$ratioMax2Min<p></h3>\n";
   print "<img src=\"$imgdir/abwImpactRatio.png\">\n";
   print "<p><img src=\"$imgdir/abwImpact.png\">\n";
   print "<p>In the following table, we list the response time of selecting server based on available bandwidth, round robin (average case), round robin (worst case). We also list the performance improvement ratio using the average case of Round Robin selection as the base line. The performance ratio numbers are wrapped around with ().<p>\n"; 
   print <<EOT; 
<table bgcolor="#EDDFC9" border="1" cellspacing="2">
<tr bgcolor="#93EEFF">
  <th>Max. Available Bandwidth (kbps)</th>
  <th>SelectServerWithABWInfo (sec) </th>
  <th>SelectServerUsingRoundRobin Avg Case(sec)</th>
  <th>SelectServerUsingRoundRobin Worst Case (sec)</th>
</tr>
EOT
  open(PlotData, "$datadir/ServerWithMaxAbw.dat");
  open(PlotDataAvg, "$datadir/ServerWithAvgAbw.dat");
  open(PlotDataMin, "$datadir/ServerWithMinAbw.dat");
  for ($i=0; $i<$numberOfDataPoints; $i++) {
     @field=split(/\s/,<PlotData>);
     $bw=$field[0]; 
     $timeMax=$field[1];
     @field=split(/\s/,<PlotDataAvg>);
     $timeAvg=$field[1];
     @field=split(/\s/,<PlotDataMin>);
     $timeMin=$field[1];
     $ratioMax=$timeAvg/$timeMax;
     $ratioMin=$timeAvg/$timeMin;
     if ($i%2==0) {
        print "<tr align=\"right\" bgcolor=\"#93FFAA\">\n";
     } else {
        print "<tr align=\"right\" bgcolor=\"#93EEFF\">\n";
     }
     printf "<td>%10.5f</td><td>%10.5f (%10.5f)</td><td>%10.5f(1)</td><td>%10.5f (%7.5f)</td>\n", 
            $bw, $timeMax, $ratioMax, $timeAvg, $timeMin, $ratioMin;
  }
  print "</table></center>\n";
  print "<h2>Impact of Document Size on Performance Improvement Ratio</h2>\n";
  print "<img src=\"$imgdir/docSizeImpactRatio.png\">\n";
  print "<h2>Impact of Server Processing Speed on Performance Improvement Ratio</h2>\n";
  print "<img src=\"$imgdir/serverSpeedImpactRatio.png\">\n";
  print end_html;
  close(PlotData);
  close(PlotDataAvg);
  close(PlotDataMin);
}

sub genDocSizeData{
  my ($abw)=@_; 
  my $docSize;
  my $bw, $i, $j, $ratio;
  for ($j=1; $j<=$numberOfDataPoints; $j++) {
   if ($j%2 == 1) {
     $docSize=$documentSize*(10**(($j-1)/2));
   } else {
     $docSize=$documentSize*5*(10**(($j-2)/2));
   }
   open(PlotData, ">$datadir/DocSize$docSize\KB.dat");
   for ($i=0, $bw=$abw; $i<$numberOfDataPoints; $i++) {
      $time = responseTime($docSize, $serverSpeed, $bw);
      $timeAvg = responseTime($docSize, $serverSpeed, $bw/$ratioMax2Avg);
      $ratio = $timeAvg/$time;
      print PlotData "$bw $ratio\n";
      $bw /= $plotScale;    
   }
   close(PlotData);
  }

}

sub genDocSizePlot{
# Generate gnuplot data

     open(GNUPLOTCMD, "> $datadir/gnuDocSizePlotcmd");
     print GNUPLOTCMD "set term png small color\n";
     print GNUPLOTCMD "set size 0.72, 0.54\n"; 
     print GNUPLOTCMD "set output \"$datadir/docSizeImpactRatio.png\" \n";
     print GNUPLOTCMD "set title \"Impact of Document Size\" \n";
     print GNUPLOTCMD "set grid\n";
     print GNUPLOTCMD "set logscale xy\n";
     print GNUPLOTCMD "show logscale\n";
     print GNUPLOTCMD "set ylabel \'Improvement Ratio\' \n";
     print GNUPLOTCMD "set xlabel \'Available Bandwidth (kbps)\' \n";

  for ($j=1; $j<=$numberOfDataPoints; $j++) {
   if ($j%2 == 1) {
     $docSize=$documentSize*(10**(($j-1)/2));
   } else {
     $docSize=$documentSize*5*(10**(($j-2)/2));
   }
   if ($j==1) {
     $pl = "\"$datadir/DocSize$docSize\KB.dat\" with linesp $j";
   } else {
     $pl .= ", \"$datadir/DocSize$docSize\KB.dat\" with linesp $j";
   }
  }
     print GNUPLOTCMD "plot $pl\n";

     close GNUPLOTCMD;
     system "$gnuplot $datadir/gnuDocSizePlotcmd";
    
}


sub genServerProcessingSpeedData{
  my ($abw)=@_; 
  my $sSpeed;
  my $bw, $i, $j, $ratio, $time, $timeAvg;
  for ($j=1; $j<=$numberOfDataPoints; $j++) {
   if ($j%2 == 1) {
     $sSpeed=$serverSpeed*(10**(($j-1)/2));
   } else {
     $sSpeed=$serverSpeed*5*(10**(($j-2)/2));
   }
   open(PlotData, ">$datadir/ServerSpeed$sSpeed\MBps.dat");
   for ($i=0, $bw=$abw; $i<$numberOfDataPoints; $i++) {
      $time = responseTime($documentSize, $sSpeed, $bw);
      $timeAvg = responseTime($documentSize, $sSpeed, $bw/$ratioMax2Avg);
      $ratio = $timeAvg/$time;
      print PlotData "$bw $ratio\n";
      $bw /= $plotScale;    
   }
   close(PlotData);
  }

}

sub genServerProcessingSpeedPlot{
# Generate gnuplot data

     open(GNUPLOTCMD, "> $datadir/gnuServerSpeedPlotcmd");
     print GNUPLOTCMD "set term png small color\n";
     print GNUPLOTCMD "set size 0.72, 0.54\n"; 
     print GNUPLOTCMD "set output \"$datadir/serverSpeedImpactRatio.png\" \n";
     print GNUPLOTCMD "set title \"Impact of Server Processing Speed\" \n";
     print GNUPLOTCMD "set grid\n";
     print GNUPLOTCMD "set logscale xy\n";
     print GNUPLOTCMD "show logscale\n";
     print GNUPLOTCMD "set ylabel \'Improvement Ratio\' \n";
     print GNUPLOTCMD "set xlabel \'Available Bandwidth (kbps)\' \n";

  for ($j=1; $j<=$numberOfDataPoints; $j++) {
   if ($j%2 == 1) {
     $sSpeed=$serverSpeed*(10**(($j-1)/2));
   } else {
     $sSpeed=$serverSpeed*5*(10**(($j-2)/2));
   }
   if ($j==1) {
     $pl = "\"$datadir/ServerSpeed$sSpeed\MBps.dat\" with linesp $j";
   } else {
     $pl .= ", \"$datadir/ServerSpeed$sSpeed\MBps.dat\" with linesp $j";
   }
  }
     print GNUPLOTCMD "plot $pl\n";

     close GNUPLOTCMD;
     system "$gnuplot $datadir/gnuServerSpeedPlotcmd";
    
}


sub genImproveRatioPlotData{
  open(PlotData, "$datadir/ServerWithMaxAbw.dat");
  open(PlotDataAvg, "$datadir/ServerWithAvgAbw.dat");
  open(PlotDataMin, "$datadir/ServerWithMinAbw.dat");
  open(PlotRatio, ">$datadir/RatioWithMaxAbw.dat");
  open(PlotRatioAvg, ">$datadir/RatioWithAvgAbw.dat");
  open(PlotRatioMin, ">$datadir/RatioWithMinAbw.dat");

  for ($i=0; $i<$numberOfDataPoints; $i++) {
     @field=split(/\s/,<PlotData>);
     $bw=$field[0]; 
     $timeMax=$field[1];
     @field=split(/\s/,<PlotDataAvg>);
     $timeAvg=$field[1];
     @field=split(/\s/,<PlotDataMin>);
     $timeMin=$field[1];
     $abwRatio=$timeAvg/$timeMax;
     $worstRatio=$timeAvg/$timeMin;
     print PlotRatio "$bw $abwRatio\n";
     print PlotRatioAvg "$bw 1\n";
     print PlotRatioMin "$bw $worstRatio\n";
  }
  close(PlotData);
  close(PlotDataAvg);
  close(PlotDataMin);
  close(PlotRatio);
  close(PlotRatioAvg);
  close(PlotRatioMin);
}

sub genRatioPlot{
# Generate gnuplot data

     open(GNUPLOTCMD, "> $datadir/gnuRatioPlotcmd");
     print GNUPLOTCMD "set term png small color\n";
     print GNUPLOTCMD "set size 0.72, 0.54\n"; 
     print GNUPLOTCMD "set output \"$datadir/abwImpactRatio.png\" \n";
     print GNUPLOTCMD "set title \"Impact on Response Time When use Available Bandwidth\" \n";
     print GNUPLOTCMD "set grid\n";
     print GNUPLOTCMD "set logscale xy\n";
     print GNUPLOTCMD "show logscale\n";
     print GNUPLOTCMD "set ylabel \'Improvement Ratio\' \n";
     print GNUPLOTCMD "set xlabel \'Available Bandwidth (kbps)\' \n";
     print GNUPLOTCMD "plot '$datadir/RatioWithMaxAbw.dat' with linesp 1, '$datadir/RatioWithAvgAbw.dat' with linesp 2,  '$datadir/RatioWithMinAbw.dat' with linesp 3\n";
     close GNUPLOTCMD;
     system "$gnuplot $datadir/gnuRatioPlotcmd";
    
}

sub genPlotData{
   my ($abw)=@_;
   $abwAvg = $abw/$ratioMax2Avg; #=50 Average Available Bandwidth 50Mb/s
   $abwMin = $abw/$ratioMax2Min; #=1  Minimum Available Bandwidth 1Mb/s
   open(PlotData, ">$datadir/ServerWithMaxAbw.dat");
   for ($i=0, $bw=$abw; $i<$numberOfDataPoints; $i++) {
      $time = responseTime($documentSize, $serverSpeed, $bw);
      print PlotData "$bw $time\n";
      $bw /= $plotScale;    
   }
   close(PlotData);

   open(PlotData, ">$datadir/ServerWithAvgAbw.dat");
   for ($i=0, $bw=$abwAvg; $i<$numberOfDataPoints; $i++) {
      $time = responseTime($documentSize, $serverSpeed, $bw);
      print PlotData "$bw $time\n";
      $bw /= $plotScale;    
   }
   close(PlotData);

   open(PlotData, ">$datadir/ServerWithMinAbw.dat");
   for ($i=0, $bw=$abwMin; $i<$numberOfDataPoints; $i++) {
      $time = responseTime($documentSize, $serverSpeed, $bw);
      print PlotData "$bw $time\n";
      $bw /= $plotScale;    
   }
   close(PlotData);
}

sub genPlot{
# Generate gnuplot data

     open(GNUPLOTCMD, "> $datadir/gnuplotcmd");
     print GNUPLOTCMD "set term png small color\n";
     print GNUPLOTCMD "set size 0.72, 0.54\n"; 
     print GNUPLOTCMD "set output \"$datadir/abwImpact.png\" \n";
     print GNUPLOTCMD "set title \"Impact of Response Time When use Available Bandwidth\" \n";
     print GNUPLOTCMD "set grid\n";
     print GNUPLOTCMD "set logscale xy\n";
     print GNUPLOTCMD "show logscale\n";
     print GNUPLOTCMD "set ylabel \'ResponseTime (seconds)\' \n";
     print GNUPLOTCMD "set xlabel \'Available Bandwidth (kbps)\' \n";
     print GNUPLOTCMD "plot \"$datadir/ServerWithMaxAbw.dat\" with linesp  1, \"$datadir/ServerWithAvgAbw.dat\" with linesp 2, \"$datadir/ServerWithMinAbw.dat\" with linesp 3\n";
     close GNUPLOTCMD;
     system "$gnuplot $datadir/gnuplotcmd";
    
}

sub responseTime {
   my ($documentSize, $serverSpeed, $bw) = @_;
   my $time;
   $time = $documentSize*1E3/($serverSpeed*1E6)+ # file retrieval time
           $documentSize*1E3*8/($bw*1E3);        # transmissoin time
   return $time;
}
