#!/usr/bin/perl

use LWP::Simple;
use LWP::UserAgent;
use Time::HiRes qw(usleep gettimeofday tv_interval);


my $num_items;

$num_items = 1;

my @item_arr = (2, 6, 10, 20, 30, 40, 60, 65, 70, 75, 80, 90, 100, 120, 150, 170, 180, 200, 220, 250, 270, 280, 300, 310, 320, 330, 340, 350, 370);

#my @item_arr = (2, 20, 150);

my $idx;
my $item_len = @item_arr;

print "item_len = $item_len\n";

my $s1;
my $usec1;
my $s2;
my $usec2;
my $time_diff;
my $flag = 0;
for ($idx = 0; $idx < $item_len ; $idx++)
{
$num_items = $item_arr[$idx];

print "numitems=$num_items flag=$flag\n";

$ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(POST => 'http://fladnag.uccs.edu/cgi-bin/cs622/purchase.pl');
$req->content_type('text/xml');


$xmldoc = "
<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>
<!--======= purhcase request example =======-->
<purchase>
  <customerName>CCL</customerName>
  <customerID>111222333</customerID>
  <item>
     <productID>309121544</productID>
     <productName>IBM Thinkpad T21</productName>
     <unitPrice>5000</unitPrice>
     <noOfUnits>10</noOfUnits>
     <subTotal>2000</subTotal>
  </item>
  <item>
     <productID>309121538</productID>
     <productName>Intel wireless LAN PC Card</productName>
     <unitPrice>200</unitPrice>
     <noOfUnits>10</noOfUnits>";


#pick servers alternatively
if ($flag >= 0)
{
  print  "vinci\n"; 
  $xmldoc = $xmldoc . "<subTotal>2000</subTotal>";
  $flag = -1; 
}
else
{
  print  "gandalf\n"; 
  $xmldoc = $xmldoc . "<subTotal>70000</subTotal>";
  $flag = 0; 
} 
#     <subTotal>2000</subTotal>
  
  $xmldoc = $xmldoc . "</item>";

  for($i = 0; $i < $num_items; $i++)
  {
     $xmldoc = $xmldoc .
      "<item>
        <productID>309121538</productID>
        <productName>Intel wireless LAN PC Card</productName>
        <unitPrice>200</unitPrice>
        <noOfUnits>10</noOfUnits>
        <subTotal>2000</subTotal>
       </item>";
  }

  $xmldoc = $xmldoc . "<totalAmount>2000</totalAmount>
</purchase>";

#print $xmldoc;
$len = length $xmldoc;
$req->content($xmldoc);

#$time3=time;

($s1, $usec1) = gettimeofday;

my $res = $ua->request($req);

#$time4=time;

($s2, $usec2) = gettimeofday;

#$periodxml = $time4 -$time3;

$time_diff = ($s2 - $s1) * 1000000 + ($usec2 - $usec1);

print $res->as_string;
print "get response after submitting purchase.xml doclen=$len in usecs = $time_diff\n";

sleep(3);
}