Performance: generate web traffic load using a powershell script

The other day, I found myself wanting a quick and dirty way to throw a controlled amount of load against a server for the purpose of tuning some thread settings.  What I wanted was to simulate varying numbers of concurrent requests to see at what point ASP.NET started to queue up connections (the “Requests in application queue” counter) instead of directly assigning them to a worker thread (the “Requests executing” counter). 

I felt a little bit like a MacGuyver, the television character famous for doing things like building a laser out of a flashlight and a magnifying glass.   In this environment, I did not have the luxury of downloading and installing one of the many load testing tools that were available, and I didn’t have a Cygwin console, which is my generally preferred scripting language.  I had to build my load generator out of the bits and pieces already available on the server. 

Time to break out powershell, the Microsoft scripting technology.  I’ve always known you could do fancy stuff with Powershell, but I’m more familiar with Cygwin and usually can use it as an alternative.  Since it wasn’t available here, I had to learn.

Powershell more or less allows you to access the .NET framework from a command-line or script, so I was able to use the HttpWebRequest object to open a connect to my server and request a specific page.  All I needed was a consistent request volume, so I didn’t really care about things like sessions or logins.  I chose a url to hit and then set up my script in a loop to hit it over and over and print out the time it took.

$url = "http://someserver/someapp/home.aspx"
while ($true) {
try {
$req = [net.webRequest]::create($url)
$req.method = "GET"
$req.ContentType = "application/x-www-form-urlencoded"
$req.TimeOut = 60000

$start = get-date
[net.httpWebResponse] $res = $req.getResponse()
$timetaken = ((get-date) - $start).TotalMilliseconds

Write-Output $res.Content
Write-Output ("{0} {1} {2}" -f (get-date), $res.StatusCode.value__, $timetaken)
$req = $null
$res = $null
} catch [Exception] {
Write-Output ("{0} {1}" -f (get-date), $_.ToString())
$req = $null

# uncomment the line below and change the wait time to add a pause between requests
#Start-Sleep -Seconds 1

I opened up a powershell console and kicked off the script.  It immediately started generating the load, and I could start watching on my web server.  Now, this script is single threaded, so that means that it will just be sending one request after another, but without any real concurrency.  That’s okay – just start opening up more powershell windows.  I opened up around 20 powershell windows and all set them executing the script over and over.  Since the scripts all print their request time out to the screen, I could also watch the consoles and see how request time shifted up or down as I opened up more and more windows in addition watching the performance counters.

With this framework in place, I could precisely control the number of threads executing against the server and then observe how adjusting ASP.NET’s settings affected request queueing at various volumes.  Just what I needed.

Obviously, this is not a load testing tool.  There are many great products out there.  However, when I need something quick and dirty, this does the trick.

This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

1 Response to Performance: generate web traffic load using a powershell script

  1. Pingback: Latency Test | ivannexus

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s