Warm up ASP.NET applications with Powershell

Warm up ASP.NET applications with Powershell

When you start an ASP.NET application for the first time, you won’t exactly be blown away by its speed. This is because, for example, IIS has to start up the ASP.NET worker process. In addition, initialization code stored in the Application_Start event may be running, and finally, the existing assemblies still have to be converted to native code by the JITer.

All of this means that you rarely hear anything like “blazing fast” when talking about the first access to an ASP.NET application.

Fortunately, none of this is a problem after the first request for a page. The worker process is there, the initialization code has run, and the just-in-time compilation has also run.

However, the situation becomes a problem if you automatically deploy your ASP.NET application as part of the nightly build during development and also give your customers access to this daily fresh application status.

Since customers usually arrive at the office earlier than developers in the morning, they are also the first to open the web application to see what has been implemented the day before. If this first access is slow for the reasons mentioned above, negative feedback from the customer about the application’s performance—or even worse: a silent decline in trust in the application and developers—is often the result.

To get around this problem, I wrote a small Powershell script that visits the individual pages of a web application after a URL is entered.

function warmup-site( [string] $rootUrl){

    $proxy = New-Object System.Net.WebProxy("mein.firmen.proxy:8080")
    $proxy.UseDefaultCredentials = 1

    $wc = New-Object System.Net.WebClient
    $wc.Proxy = $proxy

    $pages = @("default.aspx", "seite1.aspx", "seite2.aspx", "subfolder/seite3.aspx")

    # Jede Seite 3 Mal ansurfen, um Sie "warmzuklicken"
    for ($i=0; $i -lt 3; $i++){
        foreach($page in $pages){
            trap [System.Net.WebException] {
              write-error $("TRAPPED: " + $_.Exception.ToString());
              continue;
               }
            $content = $wc.DownloadString($rootUrl+$page)
            write $("URL " + $rootUrl+$page + " angesurft");
        }
    }

}

warmup-site("http://meine.url/")

So far, I have been manually specifying the individual pages to be requested within the $pages array. In the next step, however, I would prefer to just specify the URL of the sitemap, read it out, and then request all the URLs listed in the sitemap. However, as I am still new to Powershell, it may take a while before I have automated the solution to this extent. If any of the readers are thinking, “That’s just three lines of code I could whip up during my coffee break,” then I would ask you to post those three lines of code here in the comments section of my blog ;-)

What are the benefits?

The application now starts up much faster on the first request. This reassures the customer and allows us to concentrate on the important aspects of the project.

Incidentally, I noticed that IIS 7 (or 7.5?) includes a warm-up feature. Does anyone have any experience with this? Although it is not an urgent issue for me at the moment, as we are using IIS 6, it would be beneficial to have some experience reports available for the future.