• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 20. Web Performance > Monitoring Web Performance Using Perl

20.6. Monitoring Web Performance Using Perl

You can set up an automated system to monitor web performance using Perl and gnuplot. It uses the LWP library to grab a web page and then deals with proxies, handling cookies, handling SSL, and handling login forms. Here's the basic code for getting the home page, logging in, logging out, and graphing all the times. Try to run monitoring and load testing from a machine that sits on the same LAN as the web server. This way, you know that network latency is not the bottleneck.

#!/usr/local/bin/perl -w

use LWP::UserAgent; 
use Crypt::SSLeay; 
use HTTP::Cookies; 
use HTTP::Headers; 
use HTTP::Request; 
use HTTP::Response; 
use Time::HiRes 'time','sleep';

# constants:

$DEBUG       = 0; 
$browser     = 'Mozilla/4.04 [en] (X11; I; Patrix 0.0.0 i586)'; 
$rooturl     = 'https://patrick.net'; 
$user        = "pk"; 
$password    = "pw"; 
$gnuplot     = "/usr/local/bin/gnuplot";

# global objects:

$cookie_jar  = HTTP::Cookies->new; 
$ua          = LWP::UserAgent->new;

MAIN: { 
  $ua->agent($browser); # This sets browser for all uses of $ua.

  # home page 
  $latency = &get("/home.html"); 
  # verify that we got the page 
  $latency = -1 unless index "<title>login page</title>" > -1;
  &log("home.log", $latency); 
  sleep 2;

  $content = "user=$user&passwd=$password";

  # log in 
  $latency = &post("/login.cgi", $content); 
  $latency = -1 unless m|<title>welcome</title>|; 
  &log("login.log", $latency); 
  sleep 2;

  # content page 
  $latency = &get("/content.html"); 
  $latency = -1 unless m|<title>the goodies</title>|; 
  &log("content.log", $latency); 
  sleep 2;

  # logout 
  $latency = &get("/logout.cgi"); 
  $latency = -1 unless m|<title>bye</title>|; 
  &log("logout.log", $latency);

  # plot it all 
  `$gnuplot /home/httpd/public_html/demo.gp`; 
}

sub get { 
  local ($path) = @_;

  $request = new HTTP::Request('GET', "$rooturl$path");

  # If we have a previous response, put its cookies in the new request. 
  if ($response) { 
      $cookie_jar->extract_cookies($response); 
      $cookie_jar->add_cookie_header($request); 
  }

  if ($DEBUG) { 
      print $request->as_string(); 
  }

  # Do it. 
  $start    = time(); 
  $response = $ua->request($request); 
  $end      = time(); 
  $latency  = $end - $start;

  if (!$response->is_success) { 
      print $request->as_string(), " failed: ",
      $response->error_as_HTML; 
  }

  if ($DEBUG) { 
      print "\n## Got $path and result was:\n"; 
      print $response->content; 
      print   "## $path took $latency seconds.\n"; 
  }

  $latency; 
}

sub post {

  local ($path, $content) = @_;

  $header  = new HTTP::Headers; 
  $header->content_type('application/x-www-form-urlencoded'); 
  $header->content_length(length($content));

  $request = new HTTP::Request('POST', 
                               "$rooturl$path", 
                               $header, 
                               $content);

  # If we have a previous response, put its cookies in the new request. 
  if ($response) { 
      $cookie_jar->extract_cookies($response); 
      $cookie_jar->add_cookie_header($request); 
  }

  if ($DEBUG) { 
      print $request->as_string(); 
  }

  # Do it. 
  $start    = time(); 
  $response = $ua->request($request); 
  $end      = time(); 
  $latency  = $end - $start;

  if (!$response->is_success) { 
      print $request->as_string(), " failed: ", $response->error_as_HTML; 
  }

  if ($DEBUG) { 
      print "\n## Got $path and result was:\n"; 
      print $response->content; 
      print   "## $path took $latency seconds.\n"; 
  }

  $latency; 
}

# Write log entry in format that gnuplot can use to create an image. 
sub log {

  local ($file, $latency) = @_; 
  $date = `date +'%Y %m %d %H %M %S'`; 
  chop $date; 
  # Corresponding to gnuplot command: set timefmt "%m %d %H %M %S %y"

  open(FH, ">>$file") || die "Could not open $file\n";

  # Format printing so that we get only 4 decimal places. 
  printf FH "%s %2.4f\n", $date, $latency;

  close(FH); 
}

					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint