Programmatic Performance Testing With HttpWatch

Problem this snippet solves:

This script will automate performance testing for web page load times using the HttpWatch's API. It will perform initial and then return visit measurements, archive the HttpWatch .hwl files for the requests, and build a .CSV file for importing into Excel for post processing.

How to use this snippet:

PS> # Change IP Address to VIP 1
PS> .\DC-WATest.ps1 > results.csv
PS> # Change IP address to VIP 2
PS> .\DC-WAText.ps1 -WA >> results.csv

Code :

param(
  $num_retries = 5,
  $browser = "ff",
  [switch]$WA = $false,
  [switch]$SubtractRedirects = $False
);

$script:SUBTRACT_REDIRECTS = $SubtractRedirects;
$script:LOGDIR = "$(Split-Path $MyInvocation.MyCommand.Path)\WATests";
if ( -not [System.IO.Directory]::Exists($script:LOGDIR) ) { mkdir $script:LOGDIR; }

$script:HTTPWATCH =  New-Object -ComObject "HttpWatch.Controller";

#----------------------------------------------------------------------------
function Get-IPFromHostname()
#----------------------------------------------------------------------------
{
  param($hostname);
  ([System.Net.Dns]::GetHostAddresses($hostname))[0].ToString()
}

#----------------------------------------------------------------------------
function New-Plugin()
#----------------------------------------------------------------------------
{
  param($browser = "ff");
  
  $plugin = $null;
  
  if ( $browser = "ff" )
  {
    $plugin = $script:HTTPWATCH.FireFox.New();
  }
  else
  {
    $plugin = $script:HTTPWATCH.IE.New();
  }
  $plugin;
}

#----------------------------------------------------------------------------
function Clear-BrowserCache()
#----------------------------------------------------------------------------
{
  param($plugin = "ff");
  
  $plugin = New-Plugin -browser $browser;
  if ( $null -ne $plugin )
  {
    Write-Host "Clearing browser cache...";
    $plugin.ClearCache();
    $plugin.CloseBrowser();
  }
}

#----------------------------------------------------------------------------
function Load-WebPage()
#----------------------------------------------------------------------------
{
  param(
    $plugin = $(Throw "Plugin parameter required!"),
    $url = $(Throw "url parameter required!"),
    [switch]$ClearCache = $false
  );
  
  if ( $null -ne $plugin )
  {
    if ( $ClearCache )
    {
      $plugin.ClearCache();
    }

    Write-Host "Loading page '${url}'...";
    $plugin.Record();
    $plugin.GotoURL($url);
    #$HttpWatch.Wait($plugin, -1) | Out-Null;
    $wait = $script:HTTPWATCH.WaitEx($plugin, -1, $true, $false, 1);
    $plugin.Stop();
  }
}

#----------------------------------------------------------------------------
function Process-Logs()
#----------------------------------------------------------------------------
{
  param(
    $plugin = $null,
    $note = ""
  );
  
  $page = $plugin.Log.Pages.Item(0)
  
  $summary = $page.Entries.Summary;
  $events = $page.Events;
  $entry = $page.Entries.Item(0);
  
  $url = $entry.URL;
  $redirectTime = 0;
  $redirectBytesSent = 0;
  $redirectBytesReceived = 0;
  $redirectRoundtrips = 0;
  
  if ( $entry.IsRedirect -eq $true )
  {
    $url += " -> ";
    $url += $entry.RedirectURL;
    $redirectTime = $entry.Time;
    $redirectBytesSent = $entry.BytesSent;
    $redirectBytesReceived = $entry.BytesReceived;
    $redirectRoundTrips = 1;
  }
  
  $DOMLoad = $events.DOMLoad.value;
  $PageLoad = $events.PageLoad.value;
  $HTTPLoad = $events.HTTPLoad.value;
  $RenderStart = $events.RenderStart.value;
  
  $roundTrips = $summary.RoundTrips;
  $bytesReceived = $summary.BytesReceived;
  
  if ( $script:SUBTRACT_REDIRECTS )
  {
    $PageLoad -= $redirectTime;
    $HTTPLoad -= $redirectTime;
    $roundTrips -= $redirectRoundTrips;
    $bytesReceived -= $redirectBytesReceived;
  }
  
  $compressionSavedBytes = $summary.CompressionSavedBytes
  
  Write-Host "TIMING: $note - rt: $RoundTrips; pl: $PageLoad";
  
  $o = 1 | select Note, URL, RenderStart, PageLoad, HTTPLoad, BytesReceived, CompressionSavedBytes, RoundTrips, RedirectTime
  $o.Note = $note;
  $o.URL = $url;
  $o.RenderStart = $RenderStart;
  $o.PageLoad = $PageLoad;
  $o.HTTPLoad = $HTTPLoad;
  $o.BytesReceived = $bytesReceived;
  $o.CompressionSavedBytes = $compressionSavedBytes;
  $o.RoundTrips = $roundTrips;
  $o.RedirectTime = $redirectTime;
  $o;
}

#----------------------------------------------------------------------------
function Run-TimingTest()
#----------------------------------------------------------------------------
{
  param(
    $plugin = $null,
    $domain = $(Throw "domain required!"),
    $url = $(Throw "url required!"),
    $index = "",
    $note = "",
    $is_redirect = $null
  );
  
  $results = @();
  if ( $null -ne $plugin )
  {
    Write-Host "Clearing HttpWatch Log...";
    $plugin.Clear();
  
    # Load the url
    Load-WebPage -plugin $plugin -url "https://${domain}${url}";

    # save logs
    $ip = Get-IPFromHostname -hostname $domain;
    
    $logname = "${ip}-${domain}-${url}-${note}-${index}".Replace("/", "");
    $log = "$($script:LOGDIR)\${logname}.hwl";
    Write-Host "Saving log '$logname'";
    $plugin.Log.Save($log);

    # Process logs
    $results += Process-Logs -plugin $plugin -note "${logname}";

    Write-Host "Clearing HttpWatch Log...";
    $plugin.Clear();
  }
  $results;
}

#----------------------------------------------------------------------------
function Do-DCPerfTest()
#----------------------------------------------------------------------------
{
  param(
    $num_retries = 5,
    $browser = "ff",
    $tests = @("/")
  );
  
  $results = @();
  
  foreach( $test in $tests )
  {
    # Baseline: devcentral.f5.com
    
    # Baseline with empty cache
    
    Write-Host "------------------------------------------------------------";
    Write-Host "- Testing https://devcentral.f5.com/s${test} initial visit";
    Write-Host "------------------------------------------------------------";
    
    for($i=0; $i -lt $num_retries; $i++)
    {
      Clear-BrowserCache -browser $browser;
      
      $plugin = New-Plugin;
      $results += Run-TimingTest -plugin $plugin -domain "devcentral.f5.com" -url $test -index $i -note "I";
      $plugin.CloseBrowser();
    }

    Write-Host "------------------------------------------------------------";
    Write-Host "- Testing https://devcentral.f5.com/s${test} return visitor";
    Write-Host "------------------------------------------------------------";
    
    for($i=0; $i -lt $num_retries; $i++)
    {
      $plugin = New-Plugin;
      $results += Run-TimingTest -plugin $plugin -domain "devcentral.f5.com" -url $test -index $i -note "R";
      $plugin.CloseBrowser();
    }
  }
  
  $results;
}

#----------------------------------------------------------------------------
function Run-Report.CSV()
#----------------------------------------------------------------------------
{
  param(
    $results = $null
  );
  if ( $null -ne $results )
  {
    $fields = $null;
    for($i=0; $i -lt $results.Length; $i++)
    {
      $result = $results[$i];
      if ( $i -eq 0 )
      {
        $row = "";
        $fields = $result | gm -MemberType NoteProperty | Select Name;
        for($j=0; $j -lt $fields.Length; $j++)
        {
          $name = $fields[$j].Name;
          $value = $result.$name;
          
          if ( $j -gt 0 ) { $row += ","; }
          $row += $name;
        }
        
        "$row";
      }
      
      for($j=0; $j -lt $fields.Length; $j++)
      {
        $row = "";
        #$fields = $result | gm -MemberType NoteProperty | Select Name;
        for($j=0; $j -lt $fields.Length; $j++)
        {
          $name = $fields[$j].Name;
          $value = $result.$name;
          
          if ( $j -gt 0 ) { $row += ","; }
          $row += $value;
        }
        
        "$row";
      }
    }
  }
}

#----------------------------------------------------------------------------
# Main App Logic
#----------------------------------------------------------------------------

$tests = @("/");
if ( $WA )
{
  $tests = @("/tcp", "/compress", "/ibr", "/img");
}
$results = Do-DCPerfTest -num_retries $num_retries -browser $browser -tests $tests;
Run-Report.CSV -results $results;
Published Mar 12, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment