Oct
02

Roundup: SharePoint Warm-Up Scripts

by wahidsaleemi

VN:F [1.9.22_1171]
Rat­ing: 3.8/5 (4 votes cast)

This is a cross-post of my arti­cle on nbsp.

Harry Chen did a com­par­i­son of Share­Point Warm-Up scripts in Octo­ber 2010, this is essen­tially a fol­low up to his find­ings. At the end, I’ll share the script that I’m using. Do we really need another Share­Point Warm-Up Script? No, but I found some use­ful, oth­ers not so much and decided to mod­ify one for my needs.

First, lets describe briefly what these scripts are and their ben­e­fit. Dur­ing nor­mal oper­a­tion, IIS will recy­cle appli­ca­tion pools, this is nor­mally done once per day. An IISRESET or com­puter restart will recy­cle appli­ca­tion pools as well. When this hap­pens, there is a sig­nif­i­cant per­for­mance hit when load­ing a web site for the first time because every­thing needs to be compiled.

A Warm-Up Script essen­tially loads one or more web­pages so that Just-In-Time (JIT) com­pi­la­tion is per­formed. The idea is to “warm up” the site before users start request­ing it so they don’t suf­fer that first time hit. More info here.

With that said, this isn’t use­ful for Share­Point Farms with large num­bers of users access Share­Point all day and night. It’s prob­a­ble a user will get that first hit before your script does. How­ever, for smaller Farms or where there is a usage pro­file (e.g., 9am to 5pm), this can be use­ful. It could also be use­ful for devel­op­ment envi­ron­ments or demo machines where you just need to warm up ahead of time.

Now, let’s take a look at some of the options I found. Code­Plex had a few solutions:

  • SPWakeUp – This is an exe file that takes a few para­me­ters. It can exclude site col­lec­tions you spec­ify, send an email log of results and you can spec­ify a username/password. As an IT Pro, I just don’t like com­piled programs/scripts even if the source code is avail­able because its hard to mod­ify if I need to.
  • SPSite­WakeUp – This is sim­i­lar to SPWakeUp but what’s cool here is that it can run as a Win­dows Service.
  • PSWarmUp – This is a bun­dle of Pow­er­Shell files, a batch file to start up and some con­fig­u­ra­tion files. For me, this one just seems unnec­es­sar­ily com­plex. There’s a con­fig­u­ra­tion file with not much in there and a few temp files get cre­ated for the script. It’s also unnec­es­sar­ily hit­ting all sites in all zones. I like that is based on Pow­er­Shell but it still uses good ‘ole stsadm.exe.
  • Share­Point WarmUp Tool – This last one on Code­Plex seems unfin­ished. The source code is there but there is no com­piled ver­sion so you’ll have to com­pile your­self. How­ever, this is the only script I’ve found so far that claims (it will) work with Claims and FBA. Still, I’m not too sure I like com­piled scripts for this pur­pose. Alter­na­tively for FBA, here’s a sam­ple.

There are also sev­eral on the blo­gos­phere. You may find vari­a­tions of these scripts on other blogs (besides the ones listed here). How­ever, I tried to make sure I cap­tured the one which was the source (there’s so much pla­gia­rism going on!!).

  • WarmupServer (Joel Ole­son 2006) – Joel does not take credit for writ­ing these scripts but does a good job of explain­ing what they do. This is a set of vbs and batch files. This one is really old (from 2006) and requires a lot of cus­tomiza­tion. I wouldn’t touch it.
  • Daniel Bug­day Share­Point 2010 Warmup Server Script – Also vbs based and uses stsadm.
  • Kirk Hofer’s Share­Point Warm Up Script (2008) – This was orig­i­nally cre­ated for MOSS 2007 but should work on Share­Point 2010 as well. It enu­mer­ates all zones and sites and loads each one. It still uses stsadm.exe. What I didn’t like about this one is that it wasn’t easy to con­fig­ure. In MOST cases, there’s no use “warm­ing up” dif­fer­ent zones because they are all con­tained in the same appli­ca­tion pool. How­ever, this Pow­er­Shell based script is small and easy to use! (Another vari­a­tion here to allow spec­i­fy­ing addi­tional sites and here for a sim­pler version).
  • Ingo Karsteins Share­Point Warm Up (2011) – This is a Pow­er­Shell based script, sim­i­lar to Kirk Hofer’s. It includes the abil­ity to set a time­out and does some Win­dows Event Log­ging (which I thought was cool). About half way down, there is a para­me­ter called $urls that needs to be defined. This is really the only thing I didn’t like about this script – where the para­me­ter is located and the fact that you have to define it.
  • Andrew Con­nells Site Col­lec­tion Keep-Alive Job – This is a solu­tion file (WSP) for Share­Point. Frankly, this is prob­a­bly the best imple­men­ta­tions of warm­ing up sites. You can spec­ify how often to ‘refresh,’ which web appli­ca­tions, sites, and sub-site as well. All in a friendly Cen­tral Admin­is­tra­tion GUI.

Finally, there used to be an IIS Appli­ca­tion Warm-Up mod­ule. It’s offi­cially retired, if you see it some­where else, don’t use it. I never did get it to work for Share­Point 2010 anyway.

OK, at long last, what am I propos­ing? I like com­po­nents of Ingo Karsteins and Kirk Hofer’s script, so I’m going to com­bine them. I wanted to avoid using stsadm and I wanted to keep it sim­ple (one file). I also did some test­ing on warm­ing up indi­vid­ual site col­lec­tions and found that it really was unnec­es­sary. Once the appli­ca­tion pool is warmed up (by hit­ting the web appli­ca­tion URL), there’s no sig­nif­i­cant ben­e­fit to hit­ting the site col­lec­tions within it.

Mod­i­fi­ca­tions:

  • Put the vari­ables on top
  • Loads the Share­Point Snapin so you can run it from a reg­u­lar Pow­er­Shell window
  • Auto­mat­i­cally retrieves all of the Web Appli­ca­tions by default and loads them. Option­ally, you can uncom­ment the $urls vari­able at the top and spec­ify only the ones you want.
  • Uses Get-SP* cmdlets instead of stsadm.exe

Here is the script, a slightly mod­i­fied ver­sion of Ingo Karstein’s script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#############################################
# Title: Warmup-SPSites.ps1		
# Author: Ingo Karstein: http://ikarstein.wordpress.com/2011/08/03/sharepoint-warm-up-now-with-timeout/					
# Modified by: Wahid Saleemi
# Twitter: @wahidsaleemi					
# Reference: http://www.wahidsaleemi.com	
#############################################
 
#Region Define Variables
### Setup your variables here
$timeout = 60000 #=60 seconds
# Leave the line below commented if you want all of the Web Apps. Uncomment and set for only specific ones.
#$urls= @("http://finweb", "http://another.sharepoint.local")
#EndRegion
 
#Region Load SharePoint Snapin
$ver = $host | select version
if ($ver.Version.Major -gt 1)  {$Host.Runspace.ThreadOptions = "ReuseThread"}
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#EndRegion
 
 
#Region MyWebClient
    Add-Type -ReferencedAssemblies "System.Net" -TypeDefinition @"
    using System.Net;
    public class MyWebClient : WebClient
    {
        private int timeout = 60000;
         public MyWebClient(int timeout)
        {
            this.timeout = timeout;
        }
         public int Timeout
        {
            get
            {
                return timeout;
            }
            set
            {
                timeout = value;
            }
        }
         protected override WebRequest GetWebRequest(System.Uri webUrl)
        {
            WebRequest retVal = base.GetWebRequest(webUrl);
            retVal.Timeout = this.timeout;
            return retVal;
        }
    }
"@
#EndRegion
 
#Region Function to get Site List
Function Get-SiteList {
$script:sitelist = "$env:temp\siteURLs.txt"
New-Item $script:sitelist -itemType File -Force | Out-Null
# To WarmUp, we really just need the load the Web Apps
$sites = Get-SPWebApplication -IncludeCentralAdministration | Select Url
# If we want to try some caching too, get all the site collections, comment above and uncomment below
# $sites=Get-SPSite -Limit ALL
foreach ($site in $sites) 
	{
		#write-host $site.Url;
		$site.Url | Out-File $script:sitelist -append
	}
}
#EndRegion
 
#Region Set URLs to WarmUp
# check to see if a variable $urls is set.
if (!(test-path variable:\urls))
	{
	Get-SiteList
	$urls = (Get-Content $script:sitelist)
	}
#EndRegion
 
#Region Perform the WarmUp
New-EventLog -LogName "Application" -Source "SharePoint Warmup Script" -ErrorAction SilentlyContinue | Out-Null
 
$urls | % {
    $url = $_
    Write-Host "Warming up $_"
    try {
        $wc = New-Object MyWebClient($timeout)
        $wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials
        $ret = $wc.DownloadString($url)
        if( $ret.Length -gt 0 ) {
            $s = "Last run successful for url ""$($url)"": $([DateTime]::Now.ToString('yyyy.dd.MM HH:mm:ss'))"
            $filename=((Split-Path ($MyInvocation.MyCommand.Path))+"\SPWarmUp.log")
            if( Test-Path $filename -PathType Leaf ) {
                $c = Get-Content $filename
                $cl = $c -split '`n'
                $s = ((@($s) + $cl) | select -First 200)
            }
            Out-File -InputObject ($s -join "`r`n") -FilePath $filename
        }
    } catch {
          Write-EventLog -Source "SharePoint Warmup Script"  -Category 0 -ComputerName "." -EntryType Error -LogName "Application" `
            -Message "SharePoint Warmup failed for url ""$($url)""." -EventId 1001
 
        $s = "Last run failed for url ""$($url)"": $([DateTime]::Now.ToString('yyyy.dd.MM HH:mm:ss')) : $($_.Exception.Message)"
        $filename=((Split-Path ($MyInvocation.MyCommand.Path))+"\lastrunlog.txt")
        if( Test-Path $filename -PathType Leaf ) {
          $c = Get-Content $filename
          $cl = $c -split '`n'
          $s = ((@($s) + $cl) | select -First 200)
        }
        Out-File -InputObject ($s -join "`r`n") -FilePath $filename
    }
}
#EndRegion
 
$script:sitelist | Remove-Item -force -ErrorAction SilentlyContinue

Keep in mind, you should run this on all Web Servers that users will access (needs to run on each WFE) and the account run­ning the script should have appro­pri­ate permissions.

If I missed a Warm Up script that’s really dif­fer­ent from the ones in this arti­cle, please leave a comment!

Roundup: Share­Point Warm-Up Scripts, 3.8 out of 5 based on 4 ratings

Previous post:

Next post: